-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
data.go
41 lines (33 loc) · 876 Bytes
/
data.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
package serializer
import (
"github.com/firmeve/firmeve/kernel/contract"
)
type Data struct {
resource interface{}
}
func (d *Data) Resolve() interface{} {
collection := make(contract.ResourceResolveData, 0)
if data, ok := d.resource.(contract.ResourceDatable); ok {
collection[`data`] = data.Data()
} else if data, ok := d.resource.(contract.ResourceCollectionData); ok {
collection[`data`] = data.CollectionData()
} else {
collection[`data`] = d.resource
}
if meta, ok := d.resource.(contract.ResourceMeta); ok {
metaData := meta.Meta()
if len(metaData) > 0 {
collection[`meta`] = metaData
}
}
if link, ok := d.resource.(contract.ResourceLink); ok {
linkData := link.Link()
if len(linkData) > 0 {
collection[`link`] = linkData
}
}
return collection
}
func NewData(resource interface{}) *Data {
return &Data{resource: resource}
}