Skip to content

Commit 1037764

Browse files
committed
refactor: rename/document chooseFirstNonNilFieldValue
Adds test for nil hasOne polyrelation
1 parent e9893b8 commit 1037764

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

response.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,10 @@ func MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error {
193193
return json.NewEncoder(w).Encode(payload)
194194
}
195195

196-
func chooseFirstNonNilFieldValue(structValue reflect.Value) (reflect.Value, error) {
196+
// selectChoiceTypeStructField returns the first non-nil struct pointer field in the
197+
// specified struct value that has a jsonapi type field defined within it.
198+
// An error is returned if there are no fields matching that definition.
199+
func selectChoiceTypeStructField(structValue reflect.Value) (reflect.Value, error) {
197200
for i := 0; i < structValue.NumField(); i++ {
198201
choiceFieldValue := structValue.Field(i)
199202
choiceTypeField := choiceFieldValue.Type()
@@ -416,7 +419,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
416419
}
417420

418421
structValue := choiceValue.Elem()
419-
if found, err := chooseFirstNonNilFieldValue(structValue); err == nil {
422+
if found, err := selectChoiceTypeStructField(structValue); err == nil {
420423
fieldValue = found
421424
}
422425
} else {
@@ -442,7 +445,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
442445

443446
structValue := itemValue.Elem()
444447

445-
if found, err := chooseFirstNonNilFieldValue(structValue); err == nil {
448+
if found, err := selectChoiceTypeStructField(structValue); err == nil {
446449
collection = append(collection, found.Interface())
447450
}
448451
}

response_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestMarshalPayload(t *testing.T) {
3838
}
3939
}
4040

41-
func TestMarshalPayloadWithOnePolyrelation(t *testing.T) {
41+
func TestMarshalPayloadWithHasOnePolyrelation(t *testing.T) {
4242
blog := &BlogPostWithPoly{
4343
ID: "1",
4444
Title: "Hello, World",
@@ -77,7 +77,7 @@ func TestMarshalPayloadWithOnePolyrelation(t *testing.T) {
7777
}
7878
}
7979

80-
func TestMarshalPayloadWithManyPolyrelation(t *testing.T) {
80+
func TestMarshalPayloadWithHasManyPolyrelation(t *testing.T) {
8181
blog := &BlogPostWithPoly{
8282
ID: "1",
8383
Title: "Hello, World",
@@ -133,7 +133,7 @@ func TestMarshalPayloadWithManyPolyrelation(t *testing.T) {
133133
}
134134
}
135135

136-
func TestMarshalPayloadWithManyPolyrelationWithNils(t *testing.T) {
136+
func TestMarshalPayloadWithHasManyPolyrelationWithNils(t *testing.T) {
137137
blog := &BlogPostWithPoly{
138138
ID: "1",
139139
Title: "Hello, World",
@@ -154,7 +154,20 @@ func TestMarshalPayloadWithManyPolyrelationWithNils(t *testing.T) {
154154
}
155155
}
156156

157-
func TestMarshalPayloadWithManyRelationWithNils(t *testing.T) {
157+
func TestMarshalPayloadWithHasOneNilPolyrelation(t *testing.T) {
158+
blog := &BlogPostWithPoly{
159+
ID: "1",
160+
Title: "Hello, World",
161+
Hero: nil,
162+
}
163+
164+
out := bytes.NewBuffer(nil)
165+
if err := MarshalPayload(out, blog); err != nil {
166+
t.Fatalf("expected no error but got %s", err)
167+
}
168+
}
169+
170+
func TestMarshalPayloadWithHasOneNilRelation(t *testing.T) {
158171
blog := &Blog{
159172
ID: 1,
160173
Title: "Hello, World",

0 commit comments

Comments
 (0)