> 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/8e265f02-99ed-4f5e-8c76-9dc807230db5n%40googlegroups.com.

Reply via email to