序
本文主要研究一下gost的Pager
Pagergost/hash/page/pager.go
// Pager is the abstraction for pagination usage.type Pager interface { // GetOffset will return the offset GetOffset() int // GetPageSize will return the page size GetPageSize() int // GetTotalPages will return the number of total pages GetTotalPages() int // GetData will return the data GetData() []interface{} // GetDataSize will return the size of data. // Usually it's len(GetData()) GetDataSize() int // HasNext will return whether has next page HasNext() bool // HasData will return whether this page has data. HasData() bool}
Pager介面定義了GetOffset、GetPageSize、GetTotalPages、GetData、GetDataSize、HasNext、HasData方法
Pagegost/hash/page/page.go
// Page is the default implementation of Pager interfacetype Page struct { requestOffset int pageSize int totalSize int data []interface{} totalPages int hasNext bool}// GetOffSet will return the offsetfunc (d *Page) GetOffset() int { return d.requestOffset}// GetPageSize will return the page sizefunc (d *Page) GetPageSize() int { return d.pageSize}// GetTotalPages will return the number of total pagesfunc (d *Page) GetTotalPages() int { return d.totalPages}// GetData will return the datafunc (d *Page) GetData() []interface{} { return d.data}// GetDataSize will return the size of data.// it's len(GetData())func (d *Page) GetDataSize() int { return len(d.GetData())}// HasNext will return whether has next pagefunc (d *Page) HasNext() bool { return d.hasNext}// HasData will return whether this page has data.func (d *Page) HasData() bool { return d.GetDataSize() > 0}
Page定義了requestOffset、pageSize、totalSize、data、totalPages、hasNext屬性;它實現了Pager介面,其GetDataSize方法返回的是len(d.GetData());其HasData方法使用d.GetDataSize() > 0來判斷
NewPagegost/hash/page/page.go
// NewPage will create an instancefunc NewPage(requestOffset int, pageSize int, data []interface{}, totalSize int) *Page { remain := totalSize % pageSize totalPages := totalSize / pageSize if remain > 0 { totalPages++ } hasNext := totalSize-requestOffset-pageSize > 0 return &Page{ requestOffset: requestOffset, pageSize: pageSize, data: data, totalSize: totalSize, totalPages: totalPages, hasNext: hasNext, }}
NewPage方法根據requestOffset、pageSize、data、totalSize來構造Page;它會計算totalPages、hasNext
例項gost/hash/page/page_test.go
func TestNewDefaultPage(t *testing.T) { data := make([]interface{}, 10) page := NewPage(121, 10, data, 499) assert.Equal(t, 10, page.GetDataSize()) assert.Equal(t, 121, page.GetOffset()) assert.Equal(t, 10, page.GetPageSize()) assert.Equal(t, 50, page.GetTotalPages()) assert.Equal(t, data, page.GetData()) assert.True(t, page.HasNext()) assert.True(t, page.HasData()) page = NewPage(492, 10, data, 499) assert.False(t, page.HasNext())}
小結gost的Pager介面定義了GetOffset、GetPageSize、GetTotalPages、GetData、GetDataSize、HasNext、HasData方法;Page定義了requestOffset、pageSize、totalSize、data、totalPages、hasNext屬性;它實現了Pager介面。
docgost
最新評論