Skip to content

Commit

Permalink
Merge pull request #22 from hashicorp/feat/nullable
Browse files Browse the repository at this point in the history
fix: make Nullable handle significant null marshaling properly
  • Loading branch information
ctrombley authored Jan 31, 2024
2 parents 0ee74e5 + f45a873 commit 49e11fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 5 additions & 3 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,13 @@ func visitModelNode(model interface{}, included *map[string]*Node,

// handle null
if fieldValue.MapIndex(reflect.ValueOf(false)).IsValid() {
node.Attributes[args[1]] = json.RawMessage("null")
continue
}
} else {

// handle value
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true))
// handle value
fieldValue = fieldValue.MapIndex(reflect.ValueOf(true))
}
}

if fieldValue.Type() == reflect.TypeOf(time.Time{}) {
Expand Down
20 changes: 12 additions & 8 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,9 @@ func TestNullableAttr_Time(t *testing.T) {
RFC3339Time: nil,
},
verification: func(root map[string]interface{}) error {
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
if got, want := v, (interface{})(nil); got != want {
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]

if got, want := ok, false; got != want {
return fmt.Errorf("got %v, want %v", got, want)
}
return nil
Expand All @@ -849,8 +850,9 @@ func TestNullableAttr_Time(t *testing.T) {
RFC3339Time: NewNullNullableAttr[time.Time](),
},
verification: func(root map[string]interface{}) error {
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
if got, want := v, (interface{})(nil); got != want {
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]

if got, want := ok, true; got != want {
return fmt.Errorf("got %v, want %v", got, want)
}
return nil
Expand Down Expand Up @@ -930,8 +932,9 @@ func TestNullableAttr_Bool(t *testing.T) {
Bool: nil,
},
verification: func(root map[string]interface{}) error {
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
if got, want := v, (interface{})(nil); got != want {
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]

if got, want := ok, false; got != want {
return fmt.Errorf("got %v, want %v", got, want)
}
return nil
Expand All @@ -944,8 +947,9 @@ func TestNullableAttr_Bool(t *testing.T) {
Bool: NewNullNullableAttr[bool](),
},
verification: func(root map[string]interface{}) error {
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
if got, want := v, (interface{})(nil); got != want {
_, ok := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]

if got, want := ok, true; got != want {
return fmt.Errorf("got %v, want %v", got, want)
}
return nil
Expand Down

0 comments on commit 49e11fe

Please sign in to comment.