Skip to content

Embedded structs v3 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const (
annotationOmitEmpty = "omitempty"
annotationISO8601 = "iso8601"
annotationSeperator = ","
annotationIgnore = "-"

iso8601TimeFormat = "2006-01-02T15:04:05Z"

Expand Down
64 changes: 64 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ type Node struct {
Meta *Meta `json:"meta,omitempty"`
}

func (n *Node) merge(node *Node) {
if node.Type != "" {
n.Type = node.Type
}

if node.ID != "" {
n.ID = node.ID
}

if node.ClientID != "" {
n.ClientID = node.ClientID
}

if n.Attributes == nil && node.Attributes != nil {
n.Attributes = make(map[string]interface{})
}
for k, v := range node.Attributes {
n.Attributes[k] = v
}

if n.Relationships == nil && node.Relationships != nil {
n.Relationships = make(map[string]interface{})
}
for k, v := range node.Relationships {
n.Relationships[k] = v
}

if node.Links != nil {
n.Links = node.Links
}
}

// RelationshipOneNode is used to represent a generic has one JSON API relation
type RelationshipOneNode struct {
Data *Node `json:"data"`
Expand Down Expand Up @@ -119,3 +151,35 @@ type RelationshipMetable interface {
// JSONRelationshipMeta will be invoked for each relationship with the corresponding relation name (e.g. `comments`)
JSONAPIRelationshipMeta(relation string) *Meta
}

// derefs the arg, and clones the map-type attributes
// note: maps are reference types, so they need an explicit copy.
func deepCopyNode(n *Node) *Node {
if n == nil {
return n
}

copyMap := func(m map[string]interface{}) map[string]interface{} {
if m == nil {
return m
}
cp := make(map[string]interface{})
for k, v := range m {
cp[k] = v
}
return cp
}

copy := *n
copy.Attributes = copyMap(copy.Attributes)
copy.Relationships = copyMap(copy.Relationships)
if copy.Links != nil {
tmp := Links(copyMap(map[string]interface{}(*copy.Links)))
copy.Links = &tmp
}
if copy.Meta != nil {
tmp := Meta(copyMap(map[string]interface{}(*copy.Meta)))
copy.Meta = &tmp
}
return &copy
}
Loading