remove 'JsonApi' everywhere, since we're already in a pkg

This commit is contained in:
Sam Woodard 2015-07-10 09:07:12 -07:00
parent 5fc25e655a
commit 0becfab81d
5 changed files with 74 additions and 74 deletions

22
node.go
View File

@ -1,14 +1,14 @@
package jsonapi
type JsonApiOnePayload struct {
Data *JsonApiNode `json:"data"`
Included []*JsonApiNode `json:"included,omitempty"`
type OnePayload struct {
Data *Node `json:"data"`
Included []*Node `json:"included,omitempty"`
Links *map[string]string `json:"links,omitempty"`
}
type JsonApiManyPayload struct {
Data []*JsonApiNode `json:"data"`
Included []*JsonApiNode `json:"included,omitempty"`
type ManyPayload struct {
Data []*Node `json:"data"`
Included []*Node `json:"included,omitempty"`
Links *map[string]string `json:"links,omitempty"`
}
@ -16,19 +16,19 @@ type Models interface {
GetData() []interface{}
}
type JsonApiNode struct {
type Node struct {
Type string `json:"type"`
Id string `json:"id"`
Attributes map[string]interface{} `json:"attributes,omitempty"`
Relationships map[string]interface{} `json:"relationships,omitempty"`
}
type JsonApiRelationshipOneNode struct {
Data *JsonApiNode `json:"data"`
type RelationshipOneNode struct {
Data *Node `json:"data"`
Links *map[string]string `json:"links,omitempty"`
}
type JsonApiRelationshipManyNode struct {
Data []*JsonApiNode `json:"data"`
type RelationshipManyNode struct {
Data []*Node `json:"data"`
Links *map[string]string `json:"links,omitempty"`
}

View File

@ -11,17 +11,17 @@ import (
"time"
)
func UnmarshalJsonApiPayload(in io.Reader, model interface{}) error {
payload := new(JsonApiOnePayload)
func UnmarshalPayload(in io.Reader, model interface{}) error {
payload := new(OnePayload)
if err := json.NewDecoder(in).Decode(payload); err != nil {
return err
}
return unmarshalJsonApiNode(payload.Data, reflect.ValueOf(model))
return unmarshalNode(payload.Data, reflect.ValueOf(model))
}
func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
func unmarshalNode(data *Node, model reflect.Value) error {
modelValue := model.Elem()
modelType := model.Type().Elem()
@ -130,7 +130,7 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
m := reflect.New(fieldValue.Type().Elem().Elem())
h := r.(map[string]interface{})
if err := unmarshalJsonApiNode(mapToJsonApiNode(h), m); err != nil {
if err := unmarshalNode(mapToNode(h), m); err != nil {
er = err
return false
}
@ -143,7 +143,7 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
m := reflect.New(fieldValue.Type().Elem())
h := relationship["data"].(map[string]interface{})
if err := unmarshalJsonApiNode(mapToJsonApiNode(h), m); err != nil {
if err := unmarshalNode(mapToNode(h), m); err != nil {
er = err
return false
}
@ -166,8 +166,8 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error {
return nil
}
func mapToJsonApiNode(m map[string]interface{}) *JsonApiNode {
node := &JsonApiNode{Type: m["type"].(string)}
func mapToNode(m map[string]interface{}) *Node {
node := &Node{Type: m["type"].(string)}
if m["id"] != nil {
node.Id = m["id"].(string)

View File

@ -14,7 +14,7 @@ type BadModel struct {
func TestMalformedTag(t *testing.T) {
out := new(BadModel)
err := UnmarshalJsonApiPayload(samplePayload(), out)
err := UnmarshalPayload(samplePayload(), out)
if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag")
}
@ -30,7 +30,7 @@ func TestUnmarshalSetsId(t *testing.T) {
in := samplePayloadWithId()
out := new(Blog)
if err := UnmarshalJsonApiPayload(in, out); err != nil {
if err := UnmarshalPayload(in, out); err != nil {
t.Fatal(err)
}
@ -101,7 +101,7 @@ func unmarshalSamplePayload() (*Blog, error) {
in := samplePayload()
out := new(Blog)
if err := UnmarshalJsonApiPayload(in, out); err != nil {
if err := UnmarshalPayload(in, out); err != nil {
return nil, err
}
@ -109,8 +109,8 @@ func unmarshalSamplePayload() (*Blog, error) {
}
func samplePayload() io.Reader {
payload := &JsonApiOnePayload{
Data: &JsonApiNode{
payload := &OnePayload{
Data: &Node{
Type: "blogs",
Attributes: map[string]interface{}{
"title": "New blog",
@ -118,16 +118,16 @@ func samplePayload() io.Reader {
"view_count": 1000,
},
Relationships: map[string]interface{}{
"posts": &JsonApiRelationshipManyNode{
Data: []*JsonApiNode{
&JsonApiNode{
"posts": &RelationshipManyNode{
Data: []*Node{
&Node{
Type: "posts",
Attributes: map[string]interface{}{
"title": "Foo",
"body": "Bar",
},
},
&JsonApiNode{
&Node{
Type: "posts",
Attributes: map[string]interface{}{
"title": "X",
@ -136,23 +136,23 @@ func samplePayload() io.Reader {
},
},
},
"current_post": &JsonApiRelationshipOneNode{
Data: &JsonApiNode{
"current_post": &RelationshipOneNode{
Data: &Node{
Type: "posts",
Attributes: map[string]interface{}{
"title": "Bas",
"body": "Fuubar",
},
Relationships: map[string]interface{}{
"comments": &JsonApiRelationshipManyNode{
Data: []*JsonApiNode{
&JsonApiNode{
"comments": &RelationshipManyNode{
Data: []*Node{
&Node{
Type: "comments",
Attributes: map[string]interface{}{
"body": "Great post!",
},
},
&JsonApiNode{
&Node{
Type: "comments",
Attributes: map[string]interface{}{
"body": "Needs some work!",
@ -175,8 +175,8 @@ func samplePayload() io.Reader {
}
func samplePayloadWithId() io.Reader {
payload := &JsonApiOnePayload{
Data: &JsonApiNode{
payload := &OnePayload{
Data: &Node{
Id: "2",
Type: "blogs",
Attributes: map[string]interface{}{

View File

@ -10,11 +10,11 @@ import (
"time"
)
func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
func MarshalManyPayload(w io.Writer, models Models) error {
d := models.GetData()
data := make([]*JsonApiNode, 0, len(d))
data := make([]*Node, 0, len(d))
incl := make([]*JsonApiNode, 0)
incl := make([]*Node, 0)
for _, model := range d {
node, included, err := visitModelNode(model, true)
@ -25,7 +25,7 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
incl = append(incl, included...)
}
uniqueIncluded := make(map[string]*JsonApiNode)
uniqueIncluded := make(map[string]*Node)
for i, n := range incl {
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
@ -36,7 +36,7 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
}
}
payload := &JsonApiManyPayload{
payload := &ManyPayload{
Data: data,
Included: incl,
}
@ -48,27 +48,27 @@ func MarshalJsonApiManyPayload(w io.Writer, models Models) error {
return nil
}
func MarshalJsonApiOnePayloadEmbedded(model interface{}) (*JsonApiOnePayload, error) {
func MarshalOnePayloadEmbedded(model interface{}) (*OnePayload, error) {
rootNode, _, err := visitModelNode(model, false)
if err != nil {
return nil, err
}
resp := &JsonApiOnePayload{Data: rootNode}
resp := &OnePayload{Data: rootNode}
return resp, nil
}
func MarshalJsonApiOnePayload(w io.Writer, model interface{}) error {
func MarshalOnePayload(w io.Writer, model interface{}) error {
rootNode, included, err := visitModelNode(model, true)
if err != nil {
return err
}
payload := &JsonApiOnePayload{Data: rootNode}
payload := &OnePayload{Data: rootNode}
uniqueIncluded := make(map[string]*JsonApiNode)
uniqueIncluded := make(map[string]*Node)
for i, n := range included {
k := fmt.Sprintf("%s,%s", n.Type, n.Id)
@ -88,11 +88,11 @@ func MarshalJsonApiOnePayload(w io.Writer, model interface{}) error {
return nil
}
func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiNode, error) {
node := new(JsonApiNode)
func visitModelNode(model interface{}, sideload bool) (*Node, []*Node, error) {
node := new(Node)
var er error
var included []*JsonApiNode
var included []*Node
modelType := reflect.TypeOf(model).Elem()
modelValue := reflect.ValueOf(model).Elem()
@ -154,7 +154,7 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
}
if included == nil {
included = make([]*JsonApiNode, 0)
included = make([]*Node, 0)
}
if isSlice {
@ -163,15 +163,15 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
if err == nil {
if sideload {
shallowNodes := make([]*JsonApiNode, 0)
shallowNodes := make([]*Node, 0)
for _, node := range d {
included = append(included, node)
shallowNodes = append(shallowNodes, cloneAndRemoveAttributes(node))
}
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: shallowNodes}
node.Relationships[args[1]] = &RelationshipManyNode{Data: shallowNodes}
} else {
node.Relationships[args[1]] = &JsonApiRelationshipManyNode{Data: d}
node.Relationships[args[1]] = &RelationshipManyNode{Data: d}
}
} else {
er = err
@ -182,9 +182,9 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
if err == nil {
if sideload {
included = append(included, relationship)
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: cloneAndRemoveAttributes(relationship)}
node.Relationships[args[1]] = &RelationshipOneNode{Data: cloneAndRemoveAttributes(relationship)}
} else {
node.Relationships[args[1]] = &JsonApiRelationshipOneNode{Data: relationship}
node.Relationships[args[1]] = &RelationshipOneNode{Data: relationship}
}
} else {
er = err
@ -208,16 +208,16 @@ func visitModelNode(model interface{}, sideload bool) (*JsonApiNode, []*JsonApiN
return node, included, nil
}
func cloneAndRemoveAttributes(node *JsonApiNode) *JsonApiNode {
func cloneAndRemoveAttributes(node *Node) *Node {
n := *node
n.Attributes = nil
return &n
}
func visitModelNodeRelationships(relationName string, models reflect.Value, sideload bool) (map[string]*JsonApiRelationshipManyNode, error) {
m := make(map[string]*JsonApiRelationshipManyNode)
nodes := make([]*JsonApiNode, 0)
func visitModelNodeRelationships(relationName string, models reflect.Value, sideload bool) (map[string]*RelationshipManyNode, error) {
m := make(map[string]*RelationshipManyNode)
nodes := make([]*Node, 0)
for i := 0; i < models.Len(); i++ {
node, _, err := visitModelNode(models.Index(i).Interface(), sideload)
@ -228,12 +228,12 @@ func visitModelNodeRelationships(relationName string, models reflect.Value, side
nodes = append(nodes, node)
}
m[relationName] = &JsonApiRelationshipManyNode{Data: nodes}
m[relationName] = &RelationshipManyNode{Data: nodes}
return m, nil
}
func deleteNode(a []*JsonApiNode, i int) []*JsonApiNode {
func deleteNode(a []*Node, i int) []*Node {
if i < len(a)-1 {
a = append(a[:i], a[i+1:]...)
} else {

View File

@ -42,7 +42,7 @@ func (b Blogs) GetData() []interface{} {
func TestMalformedTagResposne(t *testing.T) {
testModel := &BadModel{}
out := bytes.NewBuffer(nil)
err := MarshalJsonApiOnePayload(out, testModel)
err := MarshalOnePayload(out, testModel)
if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag")
@ -63,11 +63,11 @@ func TestHasPrimaryAnnotation(t *testing.T) {
}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil {
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(JsonApiOnePayload)
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
@ -92,11 +92,11 @@ func TestSupportsAttributes(t *testing.T) {
}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil {
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(JsonApiOnePayload)
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
@ -120,11 +120,11 @@ func TestOmitsZeroTimes(t *testing.T) {
}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil {
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(JsonApiOnePayload)
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
@ -165,11 +165,11 @@ func TestRelations(t *testing.T) {
}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil {
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(JsonApiOnePayload)
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
@ -197,11 +197,11 @@ func TestNoRelations(t *testing.T) {
testModel := &Blog{Id: 1, Title: "Title 1", CreatedAt: time.Now()}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiOnePayload(out, testModel); err != nil {
if err := MarshalOnePayload(out, testModel); err != nil {
t.Fatal(err)
}
resp := new(JsonApiOnePayload)
resp := new(OnePayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}
@ -260,11 +260,11 @@ func TestMarshalMany(t *testing.T) {
}
out := bytes.NewBuffer(nil)
if err := MarshalJsonApiManyPayload(out, data); err != nil {
if err := MarshalManyPayload(out, data); err != nil {
t.Fatal(err)
}
resp := new(JsonApiManyPayload)
resp := new(ManyPayload)
if err := json.NewDecoder(out).Decode(resp); err != nil {
t.Fatal(err)
}