Skip to content

Commit a60bf6a

Browse files
committed
Merge pull request #172 from manyminds/jsonapi-ignore-tag
use `jsonapi:"-"` tag to ignore fields, fixes #171
2 parents 2ea3634 + ecd743b commit a60bf6a

14 files changed

+79
-64
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ comments that belong with a has-many relation to the post.
4949

5050
```go
5151
type Post struct {
52-
ID int
52+
ID int `jsonapi:"-"` // Ignore ID field because the ID is fetched via the
53+
// GetID() method and must not be inside the attributes object.
5354
Title string
54-
Comments []Comment `json:"-"` // this will be ignored by the api2go marshaller
55-
CommentsIDs []int `json:"-"` // it's only useful for our internal relationship handling
55+
Comments []Comment `jsonapi:"-"` // this will be ignored by the api2go marshaller
56+
CommentsIDs []int `jsonapi:"-"` // it's only useful for our internal relationship handling
5657
}
5758

5859
type Comment struct {
@@ -65,8 +66,9 @@ You must at least implement one interface for api2go to work, which is the one f
6566
that you want to marshal/unmarshal. This is because of the huge variety of types that you could use for the primary ID. For example a string,
6667
a UUID or a BSON Object for MongoDB etc...
6768

68-
If the struct already has a field named `ID`, or `Id`, it will be ignored automatically. If your ID field has a different name, please use the
69-
json ignore tag. Api2go will use the `GetID` method that you implemented for your struct to fetch the ID of the struct.
69+
In the Post example struct, the `ID` field is ignored because Api2go will use the `GetID` method that you implemented for your struct to fetch the ID of the struct.
70+
Every field inside a struct will be marshaled into the `attributes` object in
71+
the json. In our example, we just want to have the `Title` field there.
7072

7173
In order to use different internal names for elements, you can specify a jsonapi tag. The api will marshal results now with the name in the tag.
7274
Create/Update/Delete works accordingly, but will fallback to the internal value as well if possible.

api_entity_name_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type BaguetteTaste struct {
13-
ID string `json:"-"`
13+
ID string `jsonapi:"-"`
1414
Taste string
1515
}
1616

api_interfaces_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
type SomeData struct {
16-
ID string `json:"-"`
16+
ID string `jsonapi:"-"`
1717
Data string
1818
CustomerID string `jsonapi:"name=customerId"`
1919
}

api_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ func (r Response) StatusCode() int {
4141
}
4242

4343
type Post struct {
44-
ID string `json:"-"`
44+
ID string `jsonapi:"-"`
4545
Title string
4646
Value null.Float
47-
Author *User `json:"-"`
48-
Comments []Comment `json:"-"`
49-
Bananas []Banana `json:"-"`
47+
Author *User `jsonapi:"-"`
48+
Comments []Comment `jsonapi:"-"`
49+
Bananas []Banana `jsonapi:"-"`
5050
}
5151

5252
func (p Post) GetID() string {
@@ -181,7 +181,7 @@ func (p Post) GetReferencedStructs() []jsonapi.MarshalIdentifier {
181181
}
182182

183183
type Comment struct {
184-
ID string `json:"-"`
184+
ID string `jsonapi:"-"`
185185
Value string
186186
}
187187

@@ -199,7 +199,7 @@ func (b Banana) GetID() string {
199199
}
200200

201201
type User struct {
202-
ID string `json:"-"`
202+
ID string `jsonapi:"-"`
203203
Name string
204204
}
205205

examples/crud_example_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ var _ = Describe("CrudExample", func() {
5353
"id": "1",
5454
"type": "users",
5555
"attributes": {
56-
"id": "1",
5756
"user-name": "marvin"
5857
},
5958
"relationships": {
@@ -144,7 +143,6 @@ var _ = Describe("CrudExample", func() {
144143
},
145144
"data": {
146145
"attributes": {
147-
"id": "1",
148146
"user-name": "marvin"
149147
},
150148
"id": "1",
@@ -219,7 +217,6 @@ var _ = Describe("CrudExample", func() {
219217
},
220218
"data": {
221219
"attributes": {
222-
"id": "1",
223220
"user-name": "marvin"
224221
},
225222
"id": "1",
@@ -272,7 +269,6 @@ var _ = Describe("CrudExample", func() {
272269
},
273270
"data": {
274271
"attributes": {
275-
"id": "1",
276272
"user-name": "marvin"
277273
},
278274
"id": "1",
@@ -397,7 +393,6 @@ var _ = Describe("CrudExample", func() {
397393
},
398394
"data": {
399395
"attributes": {
400-
"id": "1",
401396
"user-name": "marvin"
402397
},
403398
"id": "1",

examples/model/model_chocolate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package model
22

33
// Chocolate is the chocolate that a user consumes in order to get fat and happy
44
type Chocolate struct {
5-
ID string `json:"-"`
5+
ID string `jsonapi:"-"`
66
Name string
77
Taste string
88
}

examples/model/model_user.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
// User is a generic database user
1010
type User struct {
11-
ID string
11+
ID string `jsonapi:"-"`
1212
//rename the username field to user-name.
1313
Username string `jsonapi:"name=user-name"`
14-
PasswordHash string `json:"-"`
15-
Chocolates []*Chocolate `json:"-"`
16-
ChocolatesIDs []string `json:"-"`
14+
PasswordHash string `jsonapi:"-"`
15+
Chocolates []*Chocolate `jsonapi:"-"`
16+
ChocolatesIDs []string `jsonapi:"-"`
1717
exists bool
1818
}
1919

jsonapi/fixtures_test.go

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type Magic struct {
14-
ID MagicID `json:"-"`
14+
ID MagicID `jsonapi:"-"`
1515
}
1616

1717
func (m Magic) GetID() string {
@@ -25,7 +25,7 @@ func (m MagicID) String() string {
2525
}
2626

2727
type Comment struct {
28-
ID int `json:"-"`
28+
ID int `jsonapi:"-"`
2929
Text string
3030
}
3131

@@ -45,9 +45,9 @@ func (c *Comment) SetID(stringID string) error {
4545
}
4646

4747
type User struct {
48-
ID int `json:"-"`
48+
ID int `jsonapi:"-"`
4949
Name string
50-
Password string `json:"-"`
50+
Password string `jsonapi:"-"`
5151
}
5252

5353
func (u User) GetID() string {
@@ -66,8 +66,9 @@ func (u *User) SetID(stringID string) error {
6666
}
6767

6868
type SimplePost struct {
69-
ID string `json:"-"`
69+
ID string `jsonapi:"-"`
7070
Title, Text string
71+
Internal string `jsonapi:"-"`
7172
Size int
7273
Created time.Time `jsonapi:"name=create-date"`
7374
}
@@ -83,14 +84,14 @@ func (s *SimplePost) SetID(ID string) error {
8384
}
8485

8586
type Post struct {
86-
ID int `json:"-"`
87+
ID int `jsonapi:"-"`
8788
Title string
88-
Comments []Comment `json:"-"`
89-
CommentsIDs []int `json:"-"`
90-
CommentsEmpty bool `json:"-"`
91-
Author *User `json:"-"`
92-
AuthorID sql.NullInt64 `json:"-"`
93-
AuthorEmpty bool `json:"-"`
89+
Comments []Comment `jsonapi:"-"`
90+
CommentsIDs []int `jsonapi:"-"`
91+
CommentsEmpty bool `jsonapi:"-"`
92+
Author *User `jsonapi:"-"`
93+
AuthorID sql.NullInt64 `jsonapi:"-"`
94+
AuthorEmpty bool `jsonapi:"-"`
9495
}
9596

9697
func (c Post) GetID() string {
@@ -213,9 +214,9 @@ func (c *Post) SetReferencedStructs(references []UnmarshalIdentifier) error {
213214
}
214215

215216
type AnotherPost struct {
216-
ID int `json:"-"`
217-
AuthorID int `json:"-"`
218-
Author *User `json:"-"`
217+
ID int `jsonapi:"-"`
218+
AuthorID int `jsonapi:"-"`
219+
Author *User `jsonapi:"-"`
219220
}
220221

221222
func (p AnotherPost) GetID() string {
@@ -242,7 +243,7 @@ func (p AnotherPost) GetReferencedIDs() []ReferenceID {
242243
}
243244

244245
type ZeroPost struct {
245-
ID string `json:"-"`
246+
ID string `jsonapi:"-"`
246247
Title string
247248
Value zero.Float
248249
}
@@ -252,7 +253,7 @@ func (z ZeroPost) GetID() string {
252253
}
253254

254255
type ZeroPostPointer struct {
255-
ID string `json:"-"`
256+
ID string `jsonapi:"-"`
256257
Title string
257258
Value *zero.Float
258259
}
@@ -262,10 +263,10 @@ func (z ZeroPostPointer) GetID() string {
262263
}
263264

264265
type Question struct {
265-
ID string `json:"-"`
266+
ID string `jsonapi:"-"`
266267
Text string
267-
InspiringQuestionID sql.NullString `json:"-"`
268-
InspiringQuestion *Question `json:"-"`
268+
InspiringQuestionID sql.NullString `jsonapi:"-"`
269+
InspiringQuestion *Question `jsonapi:"-"`
269270
}
270271

271272
func (q Question) GetID() string {
@@ -300,7 +301,7 @@ func (q Question) GetReferencedStructs() []MarshalIdentifier {
300301
}
301302

302303
type Identity struct {
303-
ID int64 `json:"-"`
304+
ID int64 `jsonapi:"-"`
304305
Scopes []string `json:"scopes"`
305306
}
306307

@@ -324,7 +325,7 @@ func (u Unicorn) GetID() string {
324325
}
325326

326327
type NumberPost struct {
327-
ID string `json:"-"`
328+
ID string `jsonapi:"-"`
328329
Title string
329330
Number int64
330331
UnsignedNumber uint64
@@ -337,7 +338,7 @@ func (n *NumberPost) SetID(ID string) error {
337338
}
338339

339340
type SQLNullPost struct {
340-
ID string `json:"-"`
341+
ID string `jsonapi:"-"`
341342
Title zero.String
342343
Likes zero.Int
343344
Rating zero.Float

jsonapi/integration_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
)
99

1010
type Book struct {
11-
ID string `json:"-"`
12-
Author *StupidUser `json:"-"`
13-
AuthorID string `json:"-"`
14-
Pages []Page `json:"-"`
15-
PagesIDs []string `json:"-"`
11+
ID string `jsonapi:"-"`
12+
Author *StupidUser `jsonapi:"-"`
13+
AuthorID string `jsonapi:"-"`
14+
Pages []Page `jsonapi:"-"`
15+
PagesIDs []string `jsonapi:"-"`
1616
}
1717

1818
func (b Book) GetID() string {
@@ -84,7 +84,7 @@ func (b Book) GetReferencedStructs() []MarshalIdentifier {
8484
}
8585

8686
type StupidUser struct {
87-
ID string `json:"-"`
87+
ID string `jsonapi:"-"`
8888
Name string
8989
}
9090

@@ -93,7 +93,7 @@ func (s StupidUser) GetID() string {
9393
}
9494

9595
type Page struct {
96-
ID string `json:"-"`
96+
ID string `jsonapi:"-"`
9797
Content string
9898
}
9999

jsonapi/marshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func getStructFields(data MarshalIdentifier) map[string]interface{} {
387387
}
388388
valType := val.Type()
389389
for i := 0; i < val.NumField(); i++ {
390-
tag := valType.Field(i).Tag.Get("json")
390+
tag := valType.Field(i).Tag.Get("jsonapi")
391391
if tag == "-" {
392392
continue
393393
}

0 commit comments

Comments
 (0)