| 1 | package rethinkdb |
| 2 | |
| 3 | import p "gopkg.in/rethinkdb/rethinkdb-go.v6/ql2" |
| 4 | |
| 5 | // Map transform each element of the sequence by applying the given mapping |
| 6 | // function. It takes two arguments, a sequence and a function of type |
| 7 | // `func (r.Term) interface{}`. |
| 8 | // |
| 9 | // For example this query doubles each element in an array: |
| 10 | // |
| 11 | // r.Map([]int{1,3,6}, func (row r.Term) interface{} { |
| 12 | // return row.Mul(2) |
| 13 | // }) |
| 14 | func Map(args ...interface{}) Term { |
| 15 | if len(args) > 0 { |
| 16 | args = append(args[:len(args)-1], funcWrap(args[len(args)-1])) |
| 17 | } |
| 18 | |
| 19 | return constructRootTerm("Map", p.Term_MAP, args, map[string]interface{}{}) |
| 20 | } |
| 21 | |
| 22 | // Map transforms each element of the sequence by applying the given mapping |
| 23 | // function. It takes one argument of type `func (r.Term) interface{}`. |
| 24 | // |
| 25 | // For example this query doubles each element in an array: |
| 26 | // |
| 27 | // r.Expr([]int{1,3,6}).Map(func (row r.Term) interface{} { |
| 28 | // return row.Mul(2) |
| 29 | // }) |
| 30 | func (t Term) Map(args ...interface{}) Term { |
| 31 | if len(args) > 0 { |
| 32 | args = append(args[:len(args)-1], funcWrap(args[len(args)-1])) |
| 33 | } |
| 34 | |
| 35 | return constructMethodTerm(t, "Map", p.Term_MAP, args, map[string]interface{}{}) |
| 36 | } |
| 37 | |
| 38 | // WithFields takes a sequence of objects and a list of fields. If any objects in the |
| 39 | // sequence don't have all of the specified fields, they're dropped from the |
| 40 | // sequence. The remaining objects have the specified fields plucked out. |
| 41 | // (This is identical to `HasFields` followed by `Pluck` on a sequence.) |
| 42 | func (t Term) WithFields(args ...interface{}) Term { |
| 43 | return constructMethodTerm(t, "WithFields", p.Term_WITH_FIELDS, args, map[string]interface{}{}) |
| 44 | } |
| 45 | |
| 46 | // ConcatMap concatenates one or more elements into a single sequence using a |
| 47 | // mapping function. ConcatMap works in a similar fashion to Map, applying the |
| 48 | // given function to each element in a sequence, but it will always return a |
| 49 | // single sequence. |
| 50 | func (t Term) ConcatMap(args ...interface{}) Term { |
| 51 | return constructMethodTerm(t, "ConcatMap", p.Term_CONCAT_MAP, funcWrapArgs(args), map[string]interface{}{}) |
| 52 | } |
| 53 | |
| 54 | // OrderByOpts contains the optional arguments for the OrderBy term |
| 55 | type OrderByOpts struct { |
| 56 | Index interface{} `rethinkdb:"index,omitempty"` |
| 57 | } |
| 58 | |
| 59 | func (o OrderByOpts) toMap() map[string]interface{} { |
| 60 | return optArgsToMap(o) |
| 61 | } |
| 62 | |
| 63 | // OrderBy sorts the sequence by document values of the given key(s). To specify |
| 64 | // the ordering, wrap the attribute with either r.Asc or r.Desc (defaults to |
| 65 | // ascending). |
| 66 | // |
| 67 | // Sorting without an index requires the server to hold the sequence in memory, |
| 68 | // and is limited to 100,000 documents (or the setting of the ArrayLimit option |
| 69 | // for run). Sorting with an index can be done on arbitrarily large tables, or |
| 70 | // after a between command using the same index. |
| 71 | func (t Term) OrderBy(args ...interface{}) Term { |
| 72 | var opts = map[string]interface{}{} |
| 73 | |
| 74 | // Look for options map |
| 75 | if len(args) > 0 { |
| 76 | if possibleOpts, ok := args[len(args)-1].(OrderByOpts); ok { |
| 77 | opts = possibleOpts.toMap() |
| 78 | args = args[:len(args)-1] |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | for k, arg := range args { |
| 83 | if t, ok := arg.(Term); !(ok && (t.termType == p.Term_DESC || t.termType == p.Term_ASC)) { |
| 84 | args[k] = funcWrap(arg) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return constructMethodTerm(t, "OrderBy", p.Term_ORDER_BY, args, opts) |
| 89 | } |
| 90 | |
| 91 | // Desc is used by the OrderBy term to specify the ordering to be descending. |
| 92 | func Desc(args ...interface{}) Term { |
| 93 | return constructRootTerm("Desc", p.Term_DESC, funcWrapArgs(args), map[string]interface{}{}) |
| 94 | } |
| 95 | |
| 96 | // Asc is used by the OrderBy term to specify that the ordering be ascending (the |
| 97 | // default). |
| 98 | func Asc(args ...interface{}) Term { |
| 99 | return constructRootTerm("Asc", p.Term_ASC, funcWrapArgs(args), map[string]interface{}{}) |
| 100 | } |
| 101 | |
| 102 | // Skip skips a number of elements from the head of the sequence. |
| 103 | func (t Term) Skip(args ...interface{}) Term { |
| 104 | return constructMethodTerm(t, "Skip", p.Term_SKIP, args, map[string]interface{}{}) |
| 105 | } |
| 106 | |
| 107 | // Limit ends the sequence after the given number of elements. |
| 108 | func (t Term) Limit(args ...interface{}) Term { |
| 109 | return constructMethodTerm(t, "Limit", p.Term_LIMIT, args, map[string]interface{}{}) |
| 110 | } |
| 111 | |
| 112 | // SliceOpts contains the optional arguments for the Slice term |
| 113 | type SliceOpts struct { |
| 114 | LeftBound interface{} `rethinkdb:"left_bound,omitempty"` |
| 115 | RightBound interface{} `rethinkdb:"right_bound,omitempty"` |
| 116 | } |
| 117 | |
| 118 | func (o SliceOpts) toMap() map[string]interface{} { |
| 119 | return optArgsToMap(o) |
| 120 | } |
| 121 | |
| 122 | // Slice trims the sequence to within the bounds provided. |
| 123 | func (t Term) Slice(args ...interface{}) Term { |
| 124 | var opts = map[string]interface{}{} |
| 125 | |
| 126 | // Look for options map |
| 127 | if len(args) > 0 { |
| 128 | if possibleOpts, ok := args[len(args)-1].(SliceOpts); ok { |
| 129 | opts = possibleOpts.toMap() |
| 130 | args = args[:len(args)-1] |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return constructMethodTerm(t, "Slice", p.Term_SLICE, args, opts) |
| 135 | } |
| 136 | |
| 137 | // AtIndex gets a single field from an object or the nth element from a sequence. |
| 138 | func (t Term) AtIndex(args ...interface{}) Term { |
| 139 | return constructMethodTerm(t, "AtIndex", p.Term_BRACKET, args, map[string]interface{}{}) |
| 140 | } |
| 141 | |
| 142 | // Nth gets the nth element from a sequence. |
| 143 | func (t Term) Nth(args ...interface{}) Term { |
| 144 | return constructMethodTerm(t, "Nth", p.Term_NTH, args, map[string]interface{}{}) |
| 145 | } |
| 146 | |
| 147 | // OffsetsOf gets the indexes of an element in a sequence. If the argument is a |
| 148 | // predicate, get the indexes of all elements matching it. |
| 149 | func (t Term) OffsetsOf(args ...interface{}) Term { |
| 150 | return constructMethodTerm(t, "OffsetsOf", p.Term_OFFSETS_OF, funcWrapArgs(args), map[string]interface{}{}) |
| 151 | } |
| 152 | |
| 153 | // IsEmpty tests if a sequence is empty. |
| 154 | func (t Term) IsEmpty(args ...interface{}) Term { |
| 155 | return constructMethodTerm(t, "IsEmpty", p.Term_IS_EMPTY, args, map[string]interface{}{}) |
| 156 | } |
| 157 | |
| 158 | // UnionOpts contains the optional arguments for the Slice term |
| 159 | type UnionOpts struct { |
| 160 | Interleave interface{} `rethinkdb:"interleave,omitempty"` |
| 161 | } |
| 162 | |
| 163 | func (o UnionOpts) toMap() map[string]interface{} { |
| 164 | return optArgsToMap(o) |
| 165 | } |
| 166 | |
| 167 | // Union concatenates two sequences. |
| 168 | func Union(args ...interface{}) Term { |
| 169 | return constructRootTerm("Union", p.Term_UNION, args, map[string]interface{}{}) |
| 170 | } |
| 171 | |
| 172 | // Union concatenates two sequences. |
| 173 | func (t Term) Union(args ...interface{}) Term { |
| 174 | return constructMethodTerm(t, "Union", p.Term_UNION, args, map[string]interface{}{}) |
| 175 | } |
| 176 | |
| 177 | // UnionWithOpts like Union concatenates two sequences however allows for optional |
| 178 | // arguments to be passed. |
| 179 | func UnionWithOpts(optArgs UnionOpts, args ...interface{}) Term { |
| 180 | return constructRootTerm("Union", p.Term_UNION, args, optArgs.toMap()) |
| 181 | } |
| 182 | |
| 183 | // UnionWithOpts like Union concatenates two sequences however allows for optional |
| 184 | // arguments to be passed. |
| 185 | func (t Term) UnionWithOpts(optArgs UnionOpts, args ...interface{}) Term { |
| 186 | return constructMethodTerm(t, "Union", p.Term_UNION, args, optArgs.toMap()) |
| 187 | } |
| 188 | |
| 189 | // Sample selects a given number of elements from a sequence with uniform random |
| 190 | // distribution. Selection is done without replacement. |
| 191 | func (t Term) Sample(args ...interface{}) Term { |
| 192 | return constructMethodTerm(t, "Sample", p.Term_SAMPLE, args, map[string]interface{}{}) |
| 193 | } |