-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresource.go
145 lines (128 loc) · 3.09 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package fire
import (
"fmt"
"github.com/256dpi/jsonapi/v2"
"github.com/256dpi/fire/coal"
"github.com/256dpi/fire/stick"
)
// ConvertModel will convert the provided model to a resource.
func ConvertModel(model coal.Model) (*jsonapi.Resource, error) {
// get meta
meta := coal.GetMeta(model)
// get attributes
attributes, err := jsonapi.StructToMap(model, nil)
if err != nil {
return nil, err
}
// convert relationships
relationships := map[string]*jsonapi.Document{}
for _, rel := range meta.Relationships {
// handle to-one relationship
if rel.ToOne {
val := stick.MustGet(model, rel.Name)
if rel.Optional {
id := val.(*coal.ID)
if id != nil {
relationships[rel.RelName] = &jsonapi.Document{
Data: &jsonapi.HybridResource{
One: &jsonapi.Resource{
Type: rel.RelType,
ID: id.Hex(),
},
},
}
} else {
relationships[rel.RelName] = &jsonapi.Document{}
}
} else {
relationships[rel.RelName] = &jsonapi.Document{
Data: &jsonapi.HybridResource{
One: &jsonapi.Resource{
Type: rel.RelType,
ID: val.(coal.ID).Hex(),
},
},
}
}
}
// handle to-many relationship
if rel.ToMany {
ids := stick.MustGet(model, rel.Name).([]coal.ID)
many := make([]*jsonapi.Resource, 0, len(ids))
for _, id := range ids {
many = append(many, &jsonapi.Resource{
Type: rel.RelType,
ID: id.Hex(),
})
}
relationships[rel.RelName] = &jsonapi.Document{
Data: &jsonapi.HybridResource{
Many: many,
},
}
}
}
// prepare resource
res := &jsonapi.Resource{
Type: meta.PluralName,
ID: model.ID().Hex(),
Attributes: attributes,
Relationships: relationships,
}
return res, nil
}
// AssignResource will assign the provided resource to the specified model.
func AssignResource(model coal.Model, res *jsonapi.Resource) error {
// get meta
meta := coal.GetMeta(model)
// set base
base := model.GetBase()
if res.ID != "" {
*base = coal.B(coal.MustFromHex(res.ID))
} else {
*base = coal.Base{}
}
// assign attributes
err := res.Attributes.Assign(model)
if err != nil {
return err
}
// assign relationships
for name, doc := range res.Relationships {
// get relationship
rel := meta.Relationships[name]
if rel == nil {
return fmt.Errorf("wood: unknown relationship %q for %T", name, model)
}
// skip has-* relationships
if rel.HasOne || rel.HasMany {
continue
}
// handle to one
if rel.ToOne {
if rel.Optional {
if doc.Data != nil {
id := coal.MustFromHex(doc.Data.One.ID)
stick.MustSet(model, rel.Name, &id)
} else {
stick.MustSet(model, rel.Name, nil)
}
} else {
stick.MustSet(model, rel.Name, coal.MustFromHex(doc.Data.One.ID))
}
}
// handle to many
if rel.ToMany {
if len(doc.Data.Many) > 0 {
ids := make([]coal.ID, 0, len(doc.Data.Many))
for _, rel := range doc.Data.Many {
ids = append(ids, coal.MustFromHex(rel.ID))
}
stick.MustSet(model, rel.Name, ids)
} else {
stick.MustSet(model, rel.Name, []coal.ID(nil))
}
}
}
return nil
}