object.go (2332B)
1 package query 2 3 import ( 4 "fmt" 5 "net/url" 6 ) 7 8 // Object represents the encoding of Query structures and unions. A Query 9 // object is a representation of a mapping of string keys to arbitrary 10 // values where there is a fixed set of keys whose values each have their 11 // own known type. A serialized object might look like the following: 12 // 13 // ObjectName.Foo=value 14 // &ObjectName.Bar=5 15 type Object struct { 16 // The query values to add the object to. 17 values url.Values 18 // The object's prefix, which includes the names of all parent structures 19 // and ends with the name of the object. For example, the prefix might be 20 // "ParentStructure.ObjectName". This prefix will be used to form the full 21 // keys for each member of the object. For example, a member might have the 22 // key "ParentStructure.ObjectName.MemberName". 23 // 24 // While this is currently represented as a string that gets added to, it 25 // could also be represented as a stack that only gets condensed into a 26 // string when a finalized key is created. This could potentially reduce 27 // allocations. 28 prefix string 29 } 30 31 func newObject(values url.Values, prefix string) *Object { 32 return &Object{ 33 values: values, 34 prefix: prefix, 35 } 36 } 37 38 // Key adds the given named key to the Query object. 39 // Returns a Value encoder that should be used to encode a Query value type. 40 func (o *Object) Key(name string) Value { 41 return o.key(name, false) 42 } 43 44 // KeyWithValues adds the given named key to the Query object. 45 // Returns a Value encoder that should be used to encode a Query list of values. 46 func (o *Object) KeyWithValues(name string) Value { 47 return o.keyWithValues(name, false) 48 } 49 50 // FlatKey adds the given named key to the Query object. 51 // Returns a Value encoder that should be used to encode a Query value type. The 52 // value will be flattened if it is a map or array. 53 func (o *Object) FlatKey(name string) Value { 54 return o.key(name, true) 55 } 56 57 func (o *Object) key(name string, flatValue bool) Value { 58 if o.prefix != "" { 59 return newValue(o.values, fmt.Sprintf("%s.%s", o.prefix, name), flatValue) 60 } 61 return newValue(o.values, name, flatValue) 62 } 63 64 func (o *Object) keyWithValues(name string, flatValue bool) Value { 65 if o.prefix != "" { 66 return newAppendValue(o.values, fmt.Sprintf("%s.%s", o.prefix, name), flatValue) 67 } 68 return newAppendValue(o.values, name, flatValue) 69 }