search_params.go raw

   1  package api
   2  
   3  import (
   4  	"net/url"
   5  	"strconv"
   6  	"time"
   7  
   8  	"github.com/google/go-querystring/query"
   9  	"github.com/mimuret/golang-iij-dpf/pkg/types"
  10  )
  11  
  12  type SearchParams interface {
  13  	GetValues() (url.Values, error)
  14  	GetOffset() int32
  15  	SetOffset(int32)
  16  	GetLimit() int32
  17  	SetLimit(int32)
  18  }
  19  
  20  type RowSearchParams struct {
  21  	url.Values
  22  }
  23  
  24  func NewRowSearchParams(queryString string) (*RowSearchParams, error) {
  25  	values, err := url.ParseQuery(queryString)
  26  	if err != nil {
  27  		return nil, err
  28  	}
  29  	return &RowSearchParams{values}, nil
  30  }
  31  
  32  func (r RowSearchParams) GetOffset() int32 {
  33  	v := r.Values.Get("offset")
  34  	if v == "" {
  35  		return 0
  36  	}
  37  	i, err := strconv.ParseInt(v, 10, 32)
  38  	if err != nil {
  39  		return 0
  40  	}
  41  	return int32(i)
  42  }
  43  
  44  func (r RowSearchParams) SetOffset(offset int32) {
  45  	str := strconv.FormatInt(int64(offset), 10)
  46  	r.Set("offset", str)
  47  }
  48  
  49  func (r RowSearchParams) GetLimit() int32 {
  50  	v := r.Values.Get("limit")
  51  	if v == "" {
  52  		return 100
  53  	}
  54  	i, err := strconv.ParseInt(v, 10, 32)
  55  	if err != nil {
  56  		return 100
  57  	}
  58  	return int32(i)
  59  }
  60  
  61  func (r RowSearchParams) SetLimit(limit int32) {
  62  	str := strconv.FormatInt(int64(limit), 10)
  63  	r.Set("limit", str)
  64  }
  65  
  66  func (r *RowSearchParams) GetValues() (url.Values, error) { return r.Values, nil }
  67  
  68  type CommonSearchParams struct {
  69  	Type   SearchType `url:"type,omitempty"`
  70  	Offset int32      `url:"offset,omitempty"`
  71  	Limit  int32      `url:"limit,omitempty"`
  72  }
  73  
  74  func (s *CommonSearchParams) GetValues() (url.Values, error) { return query.Values(s) }
  75  
  76  func (k *CommonSearchParams) GetType() SearchType    { return k.Type }
  77  func (k *CommonSearchParams) SetType(t SearchType)   { k.Type = t }
  78  func (k *CommonSearchParams) GetOffset() int32       { return k.Offset }
  79  func (k *CommonSearchParams) SetOffset(offset int32) { k.Offset = offset }
  80  func (k *CommonSearchParams) GetLimit() int32 {
  81  	if k.Limit == 0 {
  82  		return 100
  83  	}
  84  	return k.Limit
  85  }
  86  func (k *CommonSearchParams) SetLimit(limit int32) { k.Limit = limit }
  87  
  88  // +k8s:deepcopy-gen=false
  89  type SearchType string
  90  
  91  const (
  92  	SearchTypeAND SearchType = "AND"
  93  	SearchTypeOR  SearchType = "OR"
  94  )
  95  
  96  func (s SearchType) Validate() bool {
  97  	switch s {
  98  	case SearchTypeAND, SearchTypeOR:
  99  	default:
 100  		return false
 101  	}
 102  	return true
 103  }
 104  
 105  // +k8s:deepcopy-gen=false
 106  type SearchOffset int32
 107  
 108  func (s SearchOffset) Validate() bool {
 109  	if s < 0 || s > 10000000 {
 110  		return false
 111  	}
 112  	return true
 113  }
 114  
 115  // +k8s:deepcopy-gen=false
 116  type SearchLimit int32
 117  
 118  func (s SearchLimit) Validate() bool {
 119  	if s < 1 || s > 10000 {
 120  		return false
 121  	}
 122  	return true
 123  }
 124  
 125  // +k8s:deepcopy-gen=false
 126  type SearchDate time.Time
 127  
 128  // +k8s:deepcopy-gen=false
 129  type SearchOrder string
 130  
 131  const (
 132  	SearchOrderASC  SearchOrder = "ASC"
 133  	SearchOrderDESC SearchOrder = "DESC"
 134  )
 135  
 136  func (s SearchOrder) Validate() bool {
 137  	switch s {
 138  	case SearchOrderASC, SearchOrderDESC:
 139  	default:
 140  		return false
 141  	}
 142  	return true
 143  }
 144  
 145  // +k8s:deepcopy-gen=false
 146  type KeywordsString []string
 147  
 148  func (s KeywordsString) Validate() bool {
 149  	for _, v := range s {
 150  		if len(v) > 255 {
 151  			return false
 152  		}
 153  	}
 154  	return true
 155  }
 156  
 157  // +k8s:deepcopy-gen=false
 158  type KeywordsID []int64
 159  
 160  func (s KeywordsID) Validate() bool {
 161  	for _, v := range s {
 162  		if v < 0 {
 163  			return false
 164  		}
 165  	}
 166  	return true
 167  }
 168  
 169  // +k8s:deepcopy-gen=false
 170  type KeywordsBoolean []types.Boolean
 171  
 172  func (c KeywordsBoolean) EncodeValues(key string, v *url.Values) error {
 173  	for _, plan := range c {
 174  		v.Add(key, strconv.Itoa(int(plan)))
 175  	}
 176  	return nil
 177  }
 178  
 179  // +k8s:deepcopy-gen=false
 180  type KeywordsState []types.State
 181  
 182  func (c KeywordsState) EncodeValues(key string, v *url.Values) error {
 183  	for _, plan := range c {
 184  		v.Add(key, strconv.Itoa(int(plan)))
 185  	}
 186  	return nil
 187  }
 188  
 189  // +k8s:deepcopy-gen=false
 190  type KeywordsFavorite []types.Favorite
 191  
 192  func (c KeywordsFavorite) EncodeValues(key string, v *url.Values) error {
 193  	for _, plan := range c {
 194  		v.Add(key, strconv.Itoa(int(plan)))
 195  	}
 196  	return nil
 197  }
 198  
 199  // +k8s:deepcopy-gen=false
 200  type KeywordsLabels []Label
 201  
 202  func (c KeywordsLabels) EncodeValues(key string, v *url.Values) error {
 203  	for _, plan := range c {
 204  		v.Add(key, plan.String())
 205  	}
 206  	return nil
 207  }
 208  
 209  type Label struct {
 210  	Key   string
 211  	Value string
 212  }
 213  
 214  func (l Label) String() string {
 215  	return l.Key + "=" + l.Value
 216  }
 217