Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit 079f9c9

Browse files
committed
Merge pull request #2 from shwoodard/fix-panic-on-no-tag
paniced before when added Blog field to Post
2 parents 853b80c + 23302de commit 079f9c9

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

request.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
7777
return false
7878
}
7979

80-
fieldValue := modelValue.Field(i)
8180
fieldType := modelType.Field(i)
81+
tag := fieldType.Tag.Get("jsonapi")
82+
if tag == "" {
83+
i += 1
84+
return false
85+
}
8286

83-
i += 1
87+
fieldValue := modelValue.Field(i)
8488

85-
tag := fieldType.Tag.Get("jsonapi")
89+
i += 1
8690

8791
args := strings.Split(tag, ",")
8892

response.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,21 @@ func visitModelNode(model interface{}, sideload bool) (*Node, []*Node, error) {
123123

124124
var i = 0
125125
modelType.FieldByNameFunc(func(name string) bool {
126-
fieldValue := modelValue.Field(i)
127-
structField := modelType.Field(i)
128-
129-
i += 1
126+
if er != nil {
127+
return false
128+
}
130129

130+
structField := modelType.Field(i)
131131
tag := structField.Tag.Get("jsonapi")
132-
133132
if tag == "" {
133+
i += 1
134134
return false
135135
}
136136

137+
fieldValue := modelValue.Field(i)
138+
139+
i += 1
140+
137141
args := strings.Split(tag, ",")
138142

139143
if len(args) != 2 {

response_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Blog struct {
1919
}
2020

2121
type Post struct {
22+
Blog
2223
Id int `jsonapi:"primary,posts"`
2324
BlogId int `jsonapi:"attr,blog_id"`
2425
Title string `jsonapi:"attr,title"`

0 commit comments

Comments
 (0)