|
| 1 | +package {{ spec.title | caseLower }} |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | +) |
| 7 | + |
| 8 | +type Query struct {} |
| 9 | + |
| 10 | +func NewQuery() *Query { |
| 11 | + return &Query{} |
| 12 | +} |
| 13 | + |
| 14 | +func (q *Query) parseValue(value interface{}) string { |
| 15 | + if val, ok := value.(string); ok { |
| 16 | + return fmt.Sprintf("\"%s\"", val) |
| 17 | + } |
| 18 | + return toString(value) |
| 19 | +} |
| 20 | + |
| 21 | +func (q *Query) parseQuery(attribute string, method string, value interface{}) string { |
| 22 | + stringValue := "" |
| 23 | + switch reflect.TypeOf(value).Kind() { |
| 24 | + case reflect.Array, reflect.Slice: |
| 25 | + arr := reflect.Indirect(reflect.ValueOf(value)) |
| 26 | + for i := 0; i < arr.Len(); i++ { |
| 27 | + stringValue += q.parseValue(arr.Index(i).Interface()) |
| 28 | + if i < arr.Len()-1 { |
| 29 | + stringValue += "," |
| 30 | + } |
| 31 | + } |
| 32 | + default: |
| 33 | + stringValue = q.parseValue(value) |
| 34 | + } |
| 35 | + return fmt.Sprintf("%s(\"%s\", [%s])", method, attribute, stringValue) |
| 36 | +} |
| 37 | + |
| 38 | +func (q *Query) Equal(attribute string, value interface{}) string { |
| 39 | + return q.parseQuery(attribute, "equal", value) |
| 40 | +} |
| 41 | + |
| 42 | +func (q *Query) NotEqual(attribute string, value interface{}) string { |
| 43 | + return q.parseQuery(attribute, "notEqual", value) |
| 44 | +} |
| 45 | + |
| 46 | +func (q *Query) LessThan(attribute string, value interface{}) string { |
| 47 | + return q.parseQuery(attribute, "lessThan", value) |
| 48 | +} |
| 49 | + |
| 50 | +func (q *Query) LessThanEqual(attribute string, value interface{}) string { |
| 51 | + return q.parseQuery(attribute, "lessThanEqual", value) |
| 52 | +} |
| 53 | + |
| 54 | +func (q *Query) GreaterThan(attribute string, value interface{}) string { |
| 55 | + return q.parseQuery(attribute, "greaterThan", value) |
| 56 | +} |
| 57 | + |
| 58 | +func (q *Query) GreaterThanEqual(attribute string, value interface{}) string { |
| 59 | + return q.parseQuery(attribute, "greaterThanEqual", value) |
| 60 | +} |
| 61 | + |
| 62 | +func (q *Query) Search(attribute string, value string) string { |
| 63 | + return q.parseQuery(attribute, "search", value) |
| 64 | +} |
| 65 | + |
| 66 | +func (q *Query) IsNull(attribute string) string { |
| 67 | + return fmt.Sprintf("isNull(\"%s\")", attribute) |
| 68 | +} |
| 69 | + |
| 70 | +func (q *Query) IsNotNull(attribute string) string { |
| 71 | + return fmt.Sprintf("isNotNull(\"%s\")", attribute) |
| 72 | +} |
| 73 | + |
| 74 | +func (q *Query) Between(attribute string, start, end interface{}) string { |
| 75 | + return q.parseQuery(attribute, "between", []interface{}{start, end}) |
| 76 | +} |
| 77 | + |
| 78 | +func (q *Query) StartsWith(attribute string, value interface{}) string { |
| 79 | + return q.parseQuery(attribute, "startsWith", value) |
| 80 | +} |
| 81 | + |
| 82 | +func (q *Query) EndsWith(attribute string, value interface{}) string { |
| 83 | + return q.parseQuery(attribute, "endsWith", value) |
| 84 | +} |
| 85 | + |
| 86 | +func (q *Query) Select(attributes []string) string { |
| 87 | + stringValue := "" |
| 88 | + for i, attribute := range attributes { |
| 89 | + stringValue += fmt.Sprintf("\"%s\"", attribute) |
| 90 | + if i < len(attributes)-1 { |
| 91 | + stringValue += "," |
| 92 | + } |
| 93 | + } |
| 94 | + return fmt.Sprintf("select([%s])", stringValue) |
| 95 | +} |
| 96 | + |
| 97 | +func (q *Query) OrderAsc(attribute string) string { |
| 98 | + return fmt.Sprintf("orderAsc(\"%s\")", attribute) |
| 99 | +} |
| 100 | + |
| 101 | +func (q *Query) OrderDesc(attribute string) string { |
| 102 | + return fmt.Sprintf("orderDesc(\"%s\")", attribute) |
| 103 | +} |
| 104 | + |
| 105 | +func (q *Query) CursorBefore(documentId string) string { |
| 106 | + return fmt.Sprintf("cursorBefore(\"%s\")", documentId) |
| 107 | +} |
| 108 | + |
| 109 | +func (q *Query) CursorAfter(documentId string) string { |
| 110 | + return fmt.Sprintf("cursorAfter(\"%s\")", documentId) |
| 111 | +} |
| 112 | + |
| 113 | +func (q *Query) Limit(limit int) string { |
| 114 | + return fmt.Sprintf("limit(%d)", limit) |
| 115 | +} |
| 116 | + |
| 117 | +func (q *Query) Offset(offset int) string { |
| 118 | + return fmt.Sprintf("offset(%d)", offset) |
| 119 | +} |
0 commit comments