Skip to content

Commit 2dbeecf

Browse files
committed
chore: incorporate review feedback
1 parent cf85dab commit 2dbeecf

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

nullable.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package jsonapi
22

33
import (
44
"errors"
5-
"time"
65
)
76

87
// NullableAttr is a generic type, which implements a field that can be one of three states:
@@ -88,19 +87,3 @@ func (t NullableAttr[T]) IsSpecified() bool {
8887
func (t *NullableAttr[T]) SetUnspecified() {
8988
*t = map[bool]T{}
9089
}
91-
92-
func NullableBool(v bool) NullableAttr[bool] {
93-
return NewNullableAttrWithValue[bool](v)
94-
}
95-
96-
func NullBool() NullableAttr[bool] {
97-
return NewNullNullableAttr[bool]()
98-
}
99-
100-
func NullableTime(v time.Time) NullableAttr[time.Time] {
101-
return NewNullableAttrWithValue[time.Time](v)
102-
}
103-
104-
func NullTime() NullableAttr[time.Time] {
105-
return NewNullNullableAttr[time.Time]()
106-
}

request.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,8 @@ func handleNullable(
668668
structField reflect.StructField,
669669
fieldValue reflect.Value) (reflect.Value, error) {
670670

671-
if a, ok := attribute.(string); ok {
672-
if bytes.Equal([]byte(a), []byte("null")) {
673-
return reflect.ValueOf(nil), nil
674-
}
671+
if a, ok := attribute.(string); ok && a == "null" {
672+
return reflect.ValueOf(nil), nil
675673
}
676674

677675
innerType := fieldValue.Type().Elem()
@@ -682,7 +680,7 @@ func handleNullable(
682680
return reflect.ValueOf(nil), err
683681
}
684682

685-
fieldValue.Set(reflect.MakeMap(fieldValue.Type()))
683+
fieldValue.Set(reflect.MakeMapWithSize(fieldValue.Type(), 1))
686684
fieldValue.SetMapIndex(reflect.ValueOf(true), attrVal)
687685

688686
return fieldValue, nil

response_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ func TestNullableAttr_Time(t *testing.T) {
846846
desc: "time_null",
847847
input: &WithNullableAttrs{
848848
ID: 5,
849-
RFC3339Time: NullTime(),
849+
RFC3339Time: NewNullNullableAttr[time.Time](),
850850
},
851851
verification: func(root map[string]interface{}) error {
852852
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"]
@@ -860,7 +860,7 @@ func TestNullableAttr_Time(t *testing.T) {
860860
desc: "time_not_null_rfc3339",
861861
input: &WithNullableAttrs{
862862
ID: 5,
863-
RFC3339Time: NullableTime(aTime),
863+
RFC3339Time: NewNullableAttrWithValue[time.Time](aTime),
864864
},
865865
verification: func(root map[string]interface{}) error {
866866
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["rfc3339_time"].(string)
@@ -874,7 +874,7 @@ func TestNullableAttr_Time(t *testing.T) {
874874
desc: "time_not_null_iso8601",
875875
input: &WithNullableAttrs{
876876
ID: 5,
877-
ISO8601Time: NullableTime(aTime),
877+
ISO8601Time: NewNullableAttrWithValue[time.Time](aTime),
878878
},
879879
verification: func(root map[string]interface{}) error {
880880
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["iso8601_time"].(string)
@@ -888,7 +888,7 @@ func TestNullableAttr_Time(t *testing.T) {
888888
desc: "time_not_null_int",
889889
input: &WithNullableAttrs{
890890
ID: 5,
891-
IntTime: NullableTime(aTime),
891+
IntTime: NewNullableAttrWithValue[time.Time](aTime),
892892
},
893893
verification: func(root map[string]interface{}) error {
894894
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["int_time"].(float64)
@@ -941,7 +941,7 @@ func TestNullableAttr_Bool(t *testing.T) {
941941
desc: "bool_null",
942942
input: &WithNullableAttrs{
943943
ID: 5,
944-
Bool: NullBool(),
944+
Bool: NewNullNullableAttr[bool](),
945945
},
946946
verification: func(root map[string]interface{}) error {
947947
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"]
@@ -955,7 +955,7 @@ func TestNullableAttr_Bool(t *testing.T) {
955955
desc: "bool_not_null",
956956
input: &WithNullableAttrs{
957957
ID: 5,
958-
Bool: NullableBool(aBool),
958+
Bool: NewNullableAttrWithValue[bool](aBool),
959959
},
960960
verification: func(root map[string]interface{}) error {
961961
v := root["data"].(map[string]interface{})["attributes"].(map[string]interface{})["bool"].(bool)

0 commit comments

Comments
 (0)