From 57819f3b3423cca578e7119d22c7c18380bed945 Mon Sep 17 00:00:00 2001 From: Luke Young Date: Tue, 5 Jul 2016 20:32:15 -0500 Subject: [PATCH] Follow Golang naming specifications --- doc.go | 12 ++++---- examples/app.go | 36 ++++++++++++------------ node.go | 4 +-- request.go | 16 +++++------ request_test.go | 56 ++++++++++++++++++------------------- response.go | 16 +++++------ response_test.go | 72 ++++++++++++++++++++++++------------------------ 7 files changed, 106 insertions(+), 106 deletions(-) diff --git a/doc.go b/doc.go index 94e1e02..fe8b75b 100644 --- a/doc.go +++ b/doc.go @@ -13,26 +13,26 @@ the library in your jsonapi tags. Example structs using a Blog > Post > Comment structure, type Blog struct { - Id int `jsonapi:"primary,blogs"` + ID int `jsonapi:"primary,blogs"` Title string `jsonapi:"attr,title"` Posts []*Post `jsonapi:"relation,posts"` CurrentPost *Post `jsonapi:"relation,current_post"` - CurrentPostId int `jsonapi:"attr,current_post_id"` + CurrentPostID int `jsonapi:"attr,current_post_id"` CreatedAt time.Time `jsonapi:"attr,created_at"` ViewCount int `jsonapi:"attr,view_count"` } type Post struct { - Id int `jsonapi:"primary,posts"` - BlogId int `jsonapi:"attr,blog_id"` + ID int `jsonapi:"primary,posts"` + BlogID int `jsonapi:"attr,blog_id"` Title string `jsonapi:"attr,title"` Body string `jsonapi:"attr,body"` Comments []*Comment `jsonapi:"relation,comments"` } type Comment struct { - Id int `jsonapi:"primary,comments"` - PostId int `jsonapi:"attr,post_id"` + ID int `jsonapi:"primary,comments"` + PostID int `jsonapi:"attr,post_id"` Body string `jsonapi:"attr,body"` } diff --git a/examples/app.go b/examples/app.go index 2622628..c8c94c7 100644 --- a/examples/app.go +++ b/examples/app.go @@ -54,7 +54,7 @@ func showBlog(w http.ResponseWriter, r *http.Request) { // ...fetch your blog... - intId, err := strconv.Atoi(id) + intID, err := strconv.Atoi(id) if err != nil { http.Error(w, err.Error(), 500) return @@ -63,7 +63,7 @@ func showBlog(w http.ResponseWriter, r *http.Request) { jsonapiRuntime := jsonapi.NewRuntime().Instrument("blogs.show") // but, for now - blog := testBlogForCreate(intId) + blog := testBlogForCreate(intID) w.WriteHeader(200) w.Header().Set("Content-Type", "application/vnd.api+json") @@ -113,52 +113,52 @@ func main() { func testBlogForCreate(i int) *Blog { return &Blog{ - Id: 1 * i, + ID: 1 * i, Title: "Title 1", CreatedAt: time.Now(), Posts: []*Post{ &Post{ - Id: 1 * i, + ID: 1 * i, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1 * i, + ID: 1 * i, Body: "foo", }, &Comment{ - Id: 2 * i, + ID: 2 * i, Body: "bar", }, }, }, &Post{ - Id: 2 * i, + ID: 2 * i, Title: "Fuubar", Body: "Bas", Comments: []*Comment{ &Comment{ - Id: 1 * i, + ID: 1 * i, Body: "foo", }, &Comment{ - Id: 3 * i, + ID: 3 * i, Body: "bas", }, }, }, }, CurrentPost: &Post{ - Id: 1 * i, + ID: 1 * i, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1 * i, + ID: 1 * i, Body: "foo", }, &Comment{ - Id: 2 * i, + ID: 2 * i, Body: "bar", }, }, @@ -246,25 +246,25 @@ func exerciseHandler() { } type Blog struct { - Id int `jsonapi:"primary,blogs"` + ID int `jsonapi:"primary,blogs"` Title string `jsonapi:"attr,title"` Posts []*Post `jsonapi:"relation,posts"` CurrentPost *Post `jsonapi:"relation,current_post"` - CurrentPostId int `jsonapi:"attr,current_post_id"` + CurrentPostID int `jsonapi:"attr,current_post_id"` CreatedAt time.Time `jsonapi:"attr,created_at"` ViewCount int `jsonapi:"attr,view_count"` } type Post struct { - Id int `jsonapi:"primary,posts"` - BlogId int `jsonapi:"attr,blog_id"` + ID int `jsonapi:"primary,posts"` + BlogID int `jsonapi:"attr,blog_id"` Title string `jsonapi:"attr,title"` Body string `jsonapi:"attr,body"` Comments []*Comment `jsonapi:"relation,comments"` } type Comment struct { - Id int `jsonapi:"primary,comments"` - PostId int `jsonapi:"attr,post_id"` + ID int `jsonapi:"primary,comments"` + PostID int `jsonapi:"attr,post_id"` Body string `jsonapi:"attr,body"` } diff --git a/node.go b/node.go index 0a043e4..a8f00f3 100644 --- a/node.go +++ b/node.go @@ -14,8 +14,8 @@ type ManyPayload struct { type Node struct { Type string `json:"type"` - Id string `json:"id"` - ClientId string `json:"client-id,omitempty"` + ID string `json:"id"` + ClientID string `json:"client-id,omitempty"` Attributes map[string]interface{} `json:"attributes,omitempty"` Relationships map[string]interface{} `json:"relationships,omitempty"` } diff --git a/request.go b/request.go index c5afca3..3eaf513 100644 --- a/request.go +++ b/request.go @@ -62,7 +62,7 @@ func UnmarshalPayload(in io.Reader, model interface{}) error { if payload.Included != nil { includedMap := make(map[string]*Node) for _, included := range payload.Included { - key := fmt.Sprintf("%s,%s", included.Type, included.Id) + key := fmt.Sprintf("%s,%s", included.Type, included.ID) includedMap[key] = included } @@ -83,7 +83,7 @@ func UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error) { if payload.Included != nil { includedMap := make(map[string]*Node) for _, included := range payload.Included { - key := fmt.Sprintf("%s,%s", included.Type, included.Id) + key := fmt.Sprintf("%s,%s", included.Type, included.ID) includedMap[key] = included } @@ -151,7 +151,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) } if annotation == "primary" { - if data.Id == "" { + if data.ID == "" { continue } @@ -161,9 +161,9 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) } if fieldValue.Kind() == reflect.String { - fieldValue.Set(reflect.ValueOf(data.Id)) + fieldValue.Set(reflect.ValueOf(data.ID)) } else if fieldValue.Kind() == reflect.Int { - id, err := strconv.Atoi(data.Id) + id, err := strconv.Atoi(data.ID) if err != nil { er = err break @@ -174,11 +174,11 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) break } } else if annotation == "client-id" { - if data.ClientId == "" { + if data.ClientID == "" { continue } - fieldValue.Set(reflect.ValueOf(data.ClientId)) + fieldValue.Set(reflect.ValueOf(data.ClientID)) } else if annotation == "attr" { attributes := data.Attributes if attributes == nil || len(data.Attributes) == 0 { @@ -371,7 +371,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) } func fullNode(n *Node, included *map[string]*Node) *Node { - includedKey := fmt.Sprintf("%s,%s", n.Type, n.Id) + includedKey := fmt.Sprintf("%s,%s", n.Type, n.ID) if included != nil && (*included)[includedKey] != nil { return (*included)[includedKey] diff --git a/request_test.go b/request_test.go index 847ef43..d0eebe7 100644 --- a/request_test.go +++ b/request_test.go @@ -10,7 +10,7 @@ import ( ) type BadModel struct { - Id int `jsonapi:"primary"` + ID int `jsonapi:"primary"` } func TestMalformedTag(t *testing.T) { @@ -32,16 +32,16 @@ func TestUnmarshalInvalidJSON(t *testing.T) { } } -func TestUnmarshalSetsId(t *testing.T) { - in := samplePayloadWithId() +func TestUnmarshalSetsID(t *testing.T) { + in := samplePayloadWithID() out := new(Blog) if err := UnmarshalPayload(in, out); err != nil { t.Fatal(err) } - if out.Id != 2 { - t.Fatalf("Did not set Id on dst interface") + if out.ID != 2 { + t.Fatalf("Did not set ID on dst interface") } } @@ -200,8 +200,8 @@ func TestUnmarshalNestedRelationshipsEmbedded_withClientIDs(t *testing.T) { t.Fatal(err) } - if model.Posts[0].ClientId == "" { - t.Fatalf("ClientId not set from request on related record") + if model.Posts[0].ClientID == "" { + t.Fatalf("ClientID not set from request on related record") } } @@ -234,7 +234,7 @@ func samplePayload() io.Reader { "title": "Foo", "body": "Bar", }, - ClientId: "1", + ClientID: "1", }, &Node{ Type: "posts", @@ -242,7 +242,7 @@ func samplePayload() io.Reader { "title": "X", "body": "Y", }, - ClientId: "2", + ClientID: "2", }, }, }, @@ -253,7 +253,7 @@ func samplePayload() io.Reader { "title": "Bas", "body": "Fuubar", }, - ClientId: "3", + ClientID: "3", Relationships: map[string]interface{}{ "comments": &RelationshipManyNode{ Data: []*Node{ @@ -262,14 +262,14 @@ func samplePayload() io.Reader { Attributes: map[string]interface{}{ "body": "Great post!", }, - ClientId: "4", + ClientID: "4", }, &Node{ Type: "comments", Attributes: map[string]interface{}{ "body": "Needs some work!", }, - ClientId: "5", + ClientID: "5", }, }, }, @@ -287,10 +287,10 @@ func samplePayload() io.Reader { return out } -func samplePayloadWithId() io.Reader { +func samplePayloadWithID() io.Reader { payload := &OnePayload{ Data: &Node{ - Id: "2", + ID: "2", Type: "blogs", Attributes: map[string]interface{}{ "title": "New blog", @@ -308,66 +308,66 @@ func samplePayloadWithId() io.Reader { func testModel() *Blog { return &Blog{ - Id: 5, - ClientId: "1", + ID: 5, + ClientID: "1", Title: "Title 1", CreatedAt: time.Now(), Posts: []*Post{ &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 2, + ID: 2, Body: "bar", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, }, &Post{ - Id: 2, + ID: 2, Title: "Fuubar", Body: "Bas", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 3, + ID: 3, Body: "bas", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, }, }, CurrentPost: &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 2, + ID: 2, Body: "bar", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, }, diff --git a/response.go b/response.go index ca735f1..00093d5 100644 --- a/response.go +++ b/response.go @@ -178,15 +178,15 @@ func visitModelNode(model interface{}, included *map[string]*Node, sideload bool if annotation == "primary" { id := fieldValue.Interface() - switch nId := id.(type) { + switch nID := id.(type) { case string: - node.Id = nId + node.ID = nID case int: - node.Id = strconv.Itoa(nId) + node.ID = strconv.Itoa(nID) case int64: - node.Id = strconv.FormatInt(nId, 10) + node.ID = strconv.FormatInt(nID, 10) case uint64: - node.Id = strconv.FormatUint(nId, 10) + node.ID = strconv.FormatUint(nID, 10) default: er = ErrBadJSONAPIID break @@ -196,7 +196,7 @@ func visitModelNode(model interface{}, included *map[string]*Node, sideload bool } else if annotation == "client-id" { clientID := fieldValue.String() if clientID != "" { - node.ClientId = clientID + node.ClientID = clientID } } else if annotation == "attr" { var omitEmpty bool @@ -307,7 +307,7 @@ func visitModelNode(model interface{}, included *map[string]*Node, sideload bool func toShallowNode(node *Node) *Node { return &Node{ - Id: node.Id, + ID: node.ID, Type: node.Type, } } @@ -336,7 +336,7 @@ func appendIncluded(m *map[string]*Node, nodes ...*Node) { included := *m for _, n := range nodes { - k := fmt.Sprintf("%s,%s", n.Type, n.Id) + k := fmt.Sprintf("%s,%s", n.Type, n.ID) if _, hasNode := included[k]; hasNode { continue diff --git a/response_test.go b/response_test.go index bbafa99..419f3aa 100644 --- a/response_test.go +++ b/response_test.go @@ -8,21 +8,21 @@ import ( ) type Blog struct { - Id int `jsonapi:"primary,blogs"` - ClientId string `jsonapi:"client-id"` + ID int `jsonapi:"primary,blogs"` + ClientID string `jsonapi:"client-id"` Title string `jsonapi:"attr,title"` Posts []*Post `jsonapi:"relation,posts"` CurrentPost *Post `jsonapi:"relation,current_post"` - CurrentPostId int `jsonapi:"attr,current_post_id"` + CurrentPostID int `jsonapi:"attr,current_post_id"` CreatedAt time.Time `jsonapi:"attr,created_at"` ViewCount int `jsonapi:"attr,view_count"` } type Post struct { Blog - Id int `jsonapi:"primary,posts"` - BlogId int `jsonapi:"attr,blog_id"` - ClientId string `jsonapi:"client-id"` + ID int `jsonapi:"primary,posts"` + BlogID int `jsonapi:"attr,blog_id"` + ClientID string `jsonapi:"client-id"` Title string `jsonapi:"attr,title"` Body string `jsonapi:"attr,body"` Comments []*Comment `jsonapi:"relation,comments"` @@ -30,15 +30,15 @@ type Post struct { } type Comment struct { - Id int `jsonapi:"primary,comments"` - ClientId string `jsonapi:"client-id"` - PostId int `jsonapi:"attr,post_id"` + ID int `jsonapi:"primary,comments"` + ClientID string `jsonapi:"client-id"` + PostID int `jsonapi:"attr,post_id"` Body string `jsonapi:"attr,body"` } func TestHasPrimaryAnnotation(t *testing.T) { testModel := &Blog{ - Id: 5, + ID: 5, Title: "Title 1", CreatedAt: time.Now(), } @@ -60,14 +60,14 @@ func TestHasPrimaryAnnotation(t *testing.T) { t.Fatalf("type should have been blogs, got %s", data.Type) } - if data.Id != "5" { - t.Fatalf("Id not transfered") + if data.ID != "5" { + t.Fatalf("ID not transfered") } } func TestSupportsAttributes(t *testing.T) { testModel := &Blog{ - Id: 5, + ID: 5, Title: "Title 1", CreatedAt: time.Now(), } @@ -95,7 +95,7 @@ func TestSupportsAttributes(t *testing.T) { func TestOmitsZeroTimes(t *testing.T) { testModel := &Blog{ - Id: 5, + ID: 5, Title: "Title 1", CreatedAt: time.Time{}, } @@ -154,7 +154,7 @@ func TestRelations(t *testing.T) { } func TestNoRelations(t *testing.T) { - testModel := &Blog{Id: 1, Title: "Title 1", CreatedAt: time.Now()} + testModel := &Blog{ID: 1, Title: "Title 1", CreatedAt: time.Now()} out := bytes.NewBuffer(nil) if err := MarshalOnePayload(out, testModel); err != nil { @@ -174,45 +174,45 @@ func TestNoRelations(t *testing.T) { func TestMarshalMany(t *testing.T) { data := []interface{}{ &Blog{ - Id: 5, + ID: 5, Title: "Title 1", CreatedAt: time.Now(), Posts: []*Post{ &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", }, &Post{ - Id: 2, + ID: 2, Title: "Fuubar", Body: "Bas", }, }, CurrentPost: &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", }, }, &Blog{ - Id: 6, + ID: 6, Title: "Title 2", CreatedAt: time.Now(), Posts: []*Post{ &Post{ - Id: 3, + ID: 3, Title: "Foo", Body: "Bar", }, &Post{ - Id: 4, + ID: 4, Title: "Fuubar", Body: "Bas", }, }, CurrentPost: &Post{ - Id: 4, + ID: 4, Title: "Foo", Body: "Bar", }, @@ -238,65 +238,65 @@ func TestMarshalMany(t *testing.T) { func testBlog() *Blog { return &Blog{ - Id: 5, + ID: 5, Title: "Title 1", CreatedAt: time.Now(), Posts: []*Post{ &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 2, + ID: 2, Body: "bar", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, }, &Post{ - Id: 2, + ID: 2, Title: "Fuubar", Body: "Bas", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 3, + ID: 3, Body: "bas", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, }, }, CurrentPost: &Post{ - Id: 1, + ID: 1, Title: "Foo", Body: "Bar", Comments: []*Comment{ &Comment{ - Id: 1, + ID: 1, Body: "foo", }, &Comment{ - Id: 2, + ID: 2, Body: "bar", }, }, LatestComment: &Comment{ - Id: 1, + ID: 1, Body: "foo", }, },