From bfccb7c43c80a6a0f21ec228bcca42390230e515 Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Wed, 12 Jan 2022 10:59:16 +0200 Subject: [PATCH] Move functions and struct types to the folders they belong to --- utils/apitpdata.go | 81 +++------------------------------------- utils/coreutils.go | 93 ++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 91 insertions(+), 83 deletions(-) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 135501a17..0c885add3 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -19,87 +19,16 @@ along with this program. If not, see package utils import ( - "fmt" "strconv" "strings" "time" ) -type PaginatorWithSearch struct { - *Paginator - Search string // Global matching pattern in items returned, partially used in some APIs -} - -// Paginate stuff around items returned -type Paginator struct { - Limit *int // Limit the number of items returned - Offset *int // Offset of the first item returned (eg: use Limit*Page in case of PerPage items) - MaxItems *int -} - -func GetPaginateOpts(opts map[string]interface{}) (limit, offset, maxItems int, err error) { - if limitIface, has := opts[PageLimitOpt]; has { - if limit, err = IfaceAsTInt(limitIface); err != nil { - return - } - } - if offsetIface, has := opts[PageOffsetOpt]; has { - if offset, err = IfaceAsTInt(offsetIface); err != nil { - return - } - } - if maxItemsIface, has := opts[PageMaxItemsOpt]; has { - if maxItems, err = IfaceAsTInt(maxItemsIface); err != nil { - return - } - } - return -} - -func Paginate(in []string, limit, offset, maxItems int) (out []string, err error) { - if len(in) == 0 { - return - } - if maxItems != 0 && maxItems < limit+offset { - return nil, fmt.Errorf("SERVER_ERROR: maximum number of items exceeded") - } - if offset > len(in) { - return - } - if limit == 0 && offset == 0 { - out = in - } else { - if offset != 0 && limit != 0 { - limit = limit + offset - } - if limit == 0 || limit > len(in) { - limit = len(in) - } - out = in[offset:limit] - } - if maxItems != 0 && maxItems < len(out)+offset { - return nil, fmt.Errorf("SERVER_ERROR: maximum number of items exceeded") - } - return -} - -// Clone creates a clone of the object -func (pgnt Paginator) Clone() Paginator { - var limit *int - if pgnt.Limit != nil { - limit = new(int) - *limit = *pgnt.Limit - } - - var offset *int - if pgnt.Offset != nil { - offset = new(int) - *offset = *pgnt.Offset - } - return Paginator{ - Limit: limit, - Offset: offset, - } +type ArgsItemIDs struct { + Tenant string + ID string + APIOpts map[string]interface{} + ItemsPrefix string } type AttrGetCdrs struct { diff --git a/utils/coreutils.go b/utils/coreutils.go index 1d7eaf9ac..df19b4ba1 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -692,13 +692,6 @@ func (tID *TenantID) Equal(tID2 *TenantID) bool { tID.ID == tID2.ID) } -type ArgsItemIDs struct { - Tenant string - ID string - APIOpts map[string]interface{} - ItemsPrefix string -} - type TenantWithAPIOpts struct { Tenant string APIOpts map[string]interface{} @@ -929,3 +922,89 @@ func SplitPath(rule string, sep byte, n int) (splt []string) { splt = append(splt, rule[st:]) return } + +type PaginatorWithSearch struct { + *Paginator + Search string // Global matching pattern in items returned, partially used in some APIs +} + +// Paginate stuff around items returned +type Paginator struct { + Limit *int // Limit the number of items returned + Offset *int // Offset of the first item returned (eg: use Limit*Page in case of PerPage items) + MaxItems *int +} + +// Clone creates a clone of the object +func (pgnt Paginator) Clone() Paginator { + var limit *int + if pgnt.Limit != nil { + limit = new(int) + *limit = *pgnt.Limit + } + + var offset *int + if pgnt.Offset != nil { + offset = new(int) + *offset = *pgnt.Offset + } + + var maxItems *int + if pgnt.MaxItems != nil { + maxItems = new(int) + *maxItems = *pgnt.MaxItems + } + return Paginator{ + Limit: limit, + Offset: offset, + MaxItems: maxItems, + } +} + +// GetPaginateOpts retrieves paginate options from the APIOpts map +func GetPaginateOpts(opts map[string]interface{}) (limit, offset, maxItems int, err error) { + if limitIface, has := opts[PageLimitOpt]; has { + if limit, err = IfaceAsTInt(limitIface); err != nil { + return + } + } + if offsetIface, has := opts[PageOffsetOpt]; has { + if offset, err = IfaceAsTInt(offsetIface); err != nil { + return + } + } + if maxItemsIface, has := opts[PageMaxItemsOpt]; has { + if maxItems, err = IfaceAsTInt(maxItemsIface); err != nil { + return + } + } + return +} + +// Paginate returns a modified input sting based on the paginate options provided +func Paginate(in []string, limit, offset, maxItems int) (out []string, err error) { + if len(in) == 0 { + return + } + if maxItems != 0 && maxItems < limit+offset { + return nil, fmt.Errorf("SERVER_ERROR: maximum number of items exceeded") + } + if offset > len(in) { + return + } + if limit == 0 && offset == 0 { + out = in + } else { + if offset != 0 && limit != 0 { + limit = limit + offset + } + if limit == 0 || limit > len(in) { + limit = len(in) + } + out = in[offset:limit] + } + if maxItems != 0 && maxItems < len(out)+offset { + return nil, fmt.Errorf("SERVER_ERROR: maximum number of items exceeded") + } + return +}