Skip to content

Commit

Permalink
add omitempty for apigroups
Browse files Browse the repository at this point in the history
  • Loading branch information
bakito committed Mar 5, 2021
1 parent 3f5aba0 commit a92bc2a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/v1/eventlogger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions api/v1/eventlogger_types_test.go
Original file line number Diff line number Diff line change
@@ -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"}`))
})
})
})

0 comments on commit a92bc2a

Please sign in to comment.