Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.14 KB | None | 0 0
  1. type pagination struct {
  2.         from int32
  3.         to   int32
  4. }
  5.  
  6. func (p pagination) prepare() (string, error) {
  7.         if p.to < p.from {
  8.                 return "", operations.ErrMalformedPaginationLimits
  9.         } else if p.to == 0 {
  10.                 return "", nil
  11.         }
  12.  
  13.         return fmt.Sprintf(`"from":%d,"size":%d`, p.from, p.to-p.from), nil
  14. }
  15.  
  16. type stringValueOrQuery struct {
  17.         pagination pagination
  18.         fieldName  string
  19.         termValues []string
  20. }
  21.  
  22. func (s stringValueOrQuery) prepare() (string, error) {
  23.         var buff bytes.Buffer
  24.  
  25.         for i, value := range s.termValues {
  26.                 buff.WriteString("{")
  27.                 buff.WriteString(fmt.Sprintf(`"term":{"%s": "%s"}`, s.fieldName, value))
  28.                 buff.WriteString("}")
  29.                 if i != len(s.termValues)-1 {
  30.                         buff.WriteString(",")
  31.                 }
  32.         }
  33.  
  34.         paging, err := s.pagination.prepare()
  35.         if err != nil {
  36.                 return "", err
  37.         }
  38.         query := fmt.Sprintf(`{"query":{%s,"bool":{"should": [%s]}}}`, paging, buff.String())
  39.  
  40.         return query, nil
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement