Skip to content

Commit 49e11fe

Browse files
authored
Merge pull request #22 from hashicorp/feat/nullable
fix: make Nullable handle significant null marshaling properly
2 parents 0ee74e5 + f45a873 commit 49e11fe

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

response.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,13 @@ func visitModelNode(model interface{}, included *map[string]*Node,
340340

341341
// handle null
342342
if fieldValue.MapIndex(reflect.ValueOf(false)).IsValid() {
343+
node.Attributes[args[1]] = json.RawMessage("null")
343344
continue
344-
}
345+
} else {
345346

346-
// handle value
347-
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true))
347+
// handle value
348+
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true))
349+
}
348350
}
349351

350352
if fieldValue.Type() == reflect.TypeOf(time.Time{}) {

response_test.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,9 @@ func TestNullableAttr_Time(t *testing.T) {
835835
RFC3339Time: nil,
836836
},
837837
verification: func(root map[string]interface{}) error {
838-
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
839-
if got, want := v, (interface{})(nil); got != want {
838+
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
839+
840+
if got, want := ok, false; got != want {
840841
return fmt.Errorf("got %v, want %v", got, want)
841842
}
842843
return nil
@@ -849,8 +850,9 @@ func TestNullableAttr_Time(t *testing.T) {
849850
RFC3339Time: NewNullNullableAttr[time.Time](),
850851
},
851852
verification: func(root map[string]interface{}) error {
852-
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
853-
if got, want := v, (interface{})(nil); got != want {
853+
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
854+
855+
if got, want := ok, true; got != want {
854856
return fmt.Errorf("got %v, want %v", got, want)
855857
}
856858
return nil
@@ -930,8 +932,9 @@ func TestNullableAttr_Bool(t *testing.T) {
930932
Bool: nil,
931933
},
932934
verification: func(root map[string]interface{}) error {
933-
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
934-
if got, want := v, (interface{})(nil); got != want {
935+
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
936+
937+
if got, want := ok, false; got != want {
935938
return fmt.Errorf("got %v, want %v", got, want)
936939
}
937940
return nil
@@ -944,8 +947,9 @@ func TestNullableAttr_Bool(t *testing.T) {
944947
Bool: NewNullNullableAttr[bool](),
945948
},
946949
verification: func(root map[string]interface{}) error {
947-
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
948-
if got, want := v, (interface{})(nil); got != want {
950+
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
951+
952+
if got, want := ok, true; got != want {
949953
return fmt.Errorf("got %v, want %v", got, want)
950954
}
951955
return nil

0 commit comments

Comments
 (0)