@@ -2,6 +2,7 @@ package types
22
33import (
44 "encoding/json"
5+ "fmt"
56 "github.com/stretchr/testify/assert"
67 "github.com/stretchr/testify/require"
78 "testing"
@@ -349,3 +350,58 @@ func TestMixed(t *testing.T) {
349350 })
350351 }
351352}
353+
354+ func TestNullable_MarshalJSON (t * testing.T ) {
355+ type testCase struct {
356+ name string
357+ obj SimpleInt
358+ want []byte
359+ }
360+ tests := []testCase {
361+ {
362+ name : "when obj is not set" ,
363+ want : []byte (`{"replicaCount":0}` ),
364+ },
365+ {
366+ name : "when obj is explicitly set to a specific value" ,
367+ obj : SimpleInt {
368+ ReplicaCount : Nullable [int ]{
369+ Value : 5 ,
370+ Set : true ,
371+ Null : false ,
372+ },
373+ },
374+ want : []byte (`{"replicaCount":5}` ),
375+ },
376+ {
377+ name : "when obj is explicitly set to zero value" ,
378+ obj : SimpleInt {
379+ ReplicaCount : Nullable [int ]{
380+ Value : 0 ,
381+ Set : true ,
382+ Null : false ,
383+ },
384+ },
385+ want : []byte (`{"replicaCount":0}` ),
386+ },
387+ {
388+ name : "when obj is explicitly set to null value" ,
389+ obj : SimpleInt {
390+ ReplicaCount : Nullable [int ]{
391+ Value : 0 ,
392+ Set : true ,
393+ Null : true ,
394+ },
395+ },
396+ want : []byte (`{"replicaCount":null}` ),
397+ },
398+ }
399+ for _ , tt := range tests {
400+ t .Run (tt .name , func (t * testing.T ) {
401+ got , err := json .Marshal (tt .obj )
402+ fmt .Println (string (got ))
403+ require .NoError (t , err )
404+ assert .Equalf (t , tt .want , got , "MarshalJSON()" )
405+ })
406+ }
407+ }
0 commit comments