On Thu, Sep 3, 2020 at 2:32 PM Alexander Mills
<alexander.d.mi...@gmail.com> wrote:
>
> I was under the impression it wouldn't omit the array if it had zero length, 
> hence my question..
> i mean at some point we want an empty array in JSON, so I assume it's 
> included by default

If you define boolField as a type, then you can have a custom
marshaler that checks array sizes, something like this:

func (b boolField) MarshalJSON() ([]byte,error) {
   data:=map[string]interface{}{}
  if len(b.Must)>0 {
      data["must"]=b.Must
  }
  ...
  return json.Marshal(data)
}

>
> On Thursday, September 3, 2020 at 3:35:05 AM UTC-7 nishanth...@gmail.com 
> wrote:
>>
>> > how do I omit an array on boolField if the array is empty?
>>
>> Do you mean how to omit an empty array's field in the serialized JSON? If 
>> so, you can use "omitempty" like you did in the other type.
>>
>> var boolField struct {
>>    Must []HasTermOrMatch `json:"must,omitempty"`
>>    MustNot []HasTermOrMatch `json:"must_not,omitempty"`
>>    Filter []HasTermOrMatch `json:"filter,omitempty"`
>>    Should []HasTermOrMatch `json:"should,omitempty"`
>> }
>>
>> This should omit the slice field in the serialized JSON if the slice is nil 
>> or zero length.
>> On Thursday, September 3, 2020 at 1:08:00 AM UTC+5:30 Alexander Mills wrote:
>>>
>>> I have this code used to construct an ElasticSearch json query:
>>>
>>> ```
>>> type Term = map[string]interface{}
>>> type QueryString = map[string]interface{}
>>> type Range = map[string]interface{}
>>> type Match = map[string]string
>>>
>>> type HasTermOrMatch = struct {
>>> Term `json:"term,omitempty"`
>>> QueryString `json:"query_string,omitempty"`
>>> Match `json:"match,omitempty"`
>>> Range `json:"range,omitempty"`
>>> }
>>>
>>> var boolField struct {
>>> Must []HasTermOrMatch `json:"must"`
>>> MustNot []HasTermOrMatch `json:"must_not"`
>>> Filter []HasTermOrMatch `json:"filter"`
>>> Should []HasTermOrMatch `json:"should"`
>>> }
>>> ```
>>>
>>> my question is - how do I omit an array on boolField if the array is empty?
>>>
>>> I hear there might be an interface I can add something like this:
>>>
>>>
>>> func (v Match) IsZero() bool {
>>>    return len(v) < 1
>>> }
>>>
>>> func (v Term) IsZero() bool {
>>>    return len(v) < 1
>>> }
>>>
>>> func (v Range) IsZero() bool {
>>>    return len(v) < 1
>>> }
>>>
>>> func (v QueryString) IsZero() bool {
>>>    return len(v) < 1
>>> }
>>>
>>>
>>> anyone know how to do this?
>>> thanks
>>> -alex
>>>
>>>
>>>
>>>
> --
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/7df1c875-ed2e-46c4-a6f3-2064485de805n%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2RqrkCLJiFZojKe%2BzX6HomfVvO4aZ-NRCdr%3D9c0SfRP2wHQ%40mail.gmail.com.

Reply via email to