From a92bc2aab01bc997d7d0ce37ef4c7d09bd8b53a5 Mon Sep 17 00:00:00 2001 From: bakito Date: Fri, 5 Mar 2021 11:24:26 +0100 Subject: [PATCH] add omitempty for apigroups --- api/v1/eventlogger_types.go | 2 +- api/v1/eventlogger_types_test.go | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 api/v1/eventlogger_types_test.go diff --git a/api/v1/eventlogger_types.go b/api/v1/eventlogger_types.go index 222f883..f7638d1 100644 --- a/api/v1/eventlogger_types.go +++ b/api/v1/eventlogger_types.go @@ -66,7 +66,7 @@ type Kind struct { // +optional // +nullable - ApiGroup *string `json:"apiGroup"` + ApiGroup *string `json:"apiGroup,omitempty"` // EventTypes the event types to log. If empty events are logged as defined in spec. // +kubebuilder:validation:MinItems=0 diff --git a/api/v1/eventlogger_types_test.go b/api/v1/eventlogger_types_test.go new file mode 100644 index 0000000..b61d560 --- /dev/null +++ b/api/v1/eventlogger_types_test.go @@ -0,0 +1,42 @@ +package v1_test + +import ( + "encoding/json" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + v1 "github.com/bakito/k8s-event-logger-operator/api/v1" + "k8s.io/utils/pointer" +) + +var _ = Describe("V1", func() { + Context("ApiGroup serialisation", func() { + It("should serialize an empty string", func() { + k := &v1.Kind{ + Name: "a", + ApiGroup: pointer.StringPtr(""), + } + b, err := json.Marshal(k) + Ω(err).ShouldNot(HaveOccurred()) + Ω(string(b)).Should(Equal(`{"name":"a","apiGroup":""}`)) + }) + It("not add apiGroups if nil", func() { + k := &v1.Kind{ + Name: "a", + } + b, err := json.Marshal(k) + Ω(err).ShouldNot(HaveOccurred()) + Ω(string(b)).Should(Equal(`{"name":"a"}`)) + }) + It("should serialize an the apiGroup value", func() { + k := &v1.Kind{ + Name: "a", + ApiGroup: pointer.StringPtr("b"), + } + b, err := json.Marshal(k) + Ω(err).ShouldNot(HaveOccurred()) + Ω(string(b)).Should(Equal(`{"name":"a","apiGroup":"b"}`)) + }) + }) +})