Omitempty字段不起作用,如何从响应体中删除空的结构体?

huangapple go评论76阅读模式
英文:

Omitempty field is not working, how to remove empty structs from the response body?

问题

以下是翻译好的内容:

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"`
}

即使我在字段中添加了omitempty,但仍然在yaml和json中得到了空的结构体,如何从api响应体中删除这些空的结构体?

英文:

Omitempty字段不起作用,如何从响应体中删除空的结构体?

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?

答案1

得分: 2

根据Go中yaml库的文档描述,可以使用omitempty标签来省略空结构体。

这是一个关于此的示例代码:

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
}

你可以在这里运行代码。

英文:

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

huangapple
  • 本文由 发表于 2021年8月5日 14:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/68661418.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定