0

Api Response Image link

type PolicySpec struct {
    Description      string       `json:"description" yaml:"description"`
    EndpointSelector Selector     `json:"endpointSelector,omitempty" yaml:"ingress,omitempty"`
    Severity         int          `json:"severity,omitempty" yaml:"severity,omitempty"`
    Tags             []string     `json:"tags,omitempty" yaml:"tags,omitempty"`
    Message          string       `json:"message,omitempty" yaml:"message,omitempty"`
    Process          KubeArmorSys `json:"process,omitempty" yaml:"process,omitempty"`
    File             KubeArmorSys `json:"file,omitempty" yaml:"network,omitempty"`
    Action           string       `json:"action,omitempty" yaml:"action,omitempty"`
}

I even though added omitempty in the fields yet getting the empty struct in the yaml and json, how to remove those empty structs from the api response body?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 2
    Change them to pointer types, and if `nil` they ought to be omitted. At least that's how it works with the encoding/json package, not sure about yaml. [`encoding/json`](https://pkg.go.dev/encoding/json#Marshal): *"The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as **false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string**."* -- You see? No mention of "empty struct" there. – mkopriva Aug 05 '21 at 06:26
  • got your point, thanks @mkopriva – Abhishek Ratnam Aug 05 '21 at 06:40
  • @mkopriva I think as yaml library documentation says, Zero structs are omitted. – nipuna Aug 05 '21 at 06:59
  • Hi @mkopriva for json would it also be the same, since i'm getting this issue now in the json but yaml is working great.. – Abhishek Ratnam Aug 05 '21 at 07:22
  • 1
    @AbhishekRatnam the top comment still holds as far as `encoding/json` is concerned, i.e. `omitempty` has no effect on emtpy structs and the package does not honor the "IsZeroer" interface. You'll have to use pointers. – mkopriva Aug 05 '21 at 07:26

1 Answers1

2

As documentation of yaml library in Go described, empty structs should be omitted with omitempty label. Link - pkg.go.dev/gopkg.in/yaml.v3

omitempty

         Only include the field if it's not set to the zero
         value for the type or to empty slices or maps.
         Zero valued structs will be omitted if all their public
         fields are zero, unless they implement an IsZero
         method (see the IsZeroer interface type), in which
         case the field will be excluded if IsZero returns true.

Here is the sample prove code for that.

package main

import (
    "fmt"
    "gopkg.in/yaml.v3"
    "log"
)

type A struct {
    A int `yaml:"a"`
}

type B struct {
    B int `yaml:"b"`
    A A `yaml:"a,omitempty"`
}

func main() {
    b := B{
        B:5,
    }
    encoded, err := yaml.Marshal(b)
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(`encoded - `,string(encoded)) //encoded -  b: 5
}

you can run code here

nipuna
  • 3,697
  • 11
  • 24
  • Hi @nipuna for json would it also be the same, since i'm getting this issue now in the json but yaml is working great.. – Abhishek Ratnam Aug 05 '21 at 07:23
  • For json, you need to use pointers and pass empty struct as nil. see [here](https://stackoverflow.com/questions/18088294/how-to-not-marshal-an-empty-struct-into-json-with-go) – nipuna Aug 05 '21 at 07:29