Skip to content

Commit a92bc2a

Browse files
committed
add omitempty for apigroups
1 parent 3f5aba0 commit a92bc2a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

api/v1/eventlogger_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type Kind struct {
6666

6767
// +optional
6868
// +nullable
69-
ApiGroup *string `json:"apiGroup"`
69+
ApiGroup *string `json:"apiGroup,omitempty"`
7070

7171
// EventTypes the event types to log. If empty events are logged as defined in spec.
7272
// +kubebuilder:validation:MinItems=0

api/v1/eventlogger_types_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package v1_test
2+
3+
import (
4+
"encoding/json"
5+
6+
. "github.com/onsi/ginkgo"
7+
. "github.com/onsi/gomega"
8+
9+
v1 "github.com/bakito/k8s-event-logger-operator/api/v1"
10+
"k8s.io/utils/pointer"
11+
)
12+
13+
var _ = Describe("V1", func() {
14+
Context("ApiGroup serialisation", func() {
15+
It("should serialize an empty string", func() {
16+
k := &v1.Kind{
17+
Name: "a",
18+
ApiGroup: pointer.StringPtr(""),
19+
}
20+
b, err := json.Marshal(k)
21+
Ω(err).ShouldNot(HaveOccurred())
22+
Ω(string(b)).Should(Equal(`{"name":"a","apiGroup":""}`))
23+
})
24+
It("not add apiGroups if nil", func() {
25+
k := &v1.Kind{
26+
Name: "a",
27+
}
28+
b, err := json.Marshal(k)
29+
Ω(err).ShouldNot(HaveOccurred())
30+
Ω(string(b)).Should(Equal(`{"name":"a"}`))
31+
})
32+
It("should serialize an the apiGroup value", func() {
33+
k := &v1.Kind{
34+
Name: "a",
35+
ApiGroup: pointer.StringPtr("b"),
36+
}
37+
b, err := json.Marshal(k)
38+
Ω(err).ShouldNot(HaveOccurred())
39+
Ω(string(b)).Should(Equal(`{"name":"a","apiGroup":"b"}`))
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)