-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollection_resource.go
63 lines (53 loc) · 1.5 KB
/
collection_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
package fractal
type CollectionResource struct {
sliceData []interface{}
sliceTransformed []map[string]interface{}
transformer TransformerInterface
includes Includes
err error
}
func (c *CollectionResource) SetData(data interface{}) {
sliceData, err := ConvertToInterfaceSlice(data)
c.err = err
c.sliceData = sliceData
}
func (c *CollectionResource) SetTransformer(transformer TransformerInterface) {
c.transformer = transformer
}
func (c *CollectionResource) SetIncludes(includes []string) {
c.includes.setIncludes(includes)
}
func (c *CollectionResource) SetNestedInclude(includes map[string]interface{}) {
c.includes.setNestedInclude(includes)
}
func (c *CollectionResource) ToJson() (string, error) {
if c.sliceTransformed == nil {
if err := c.TransformResource(); err != nil {
return "", err
}
}
return ToJson(c.sliceTransformed)
}
func (c *CollectionResource) TransformResource() error {
// 判断是否存在异常
if c.err != nil {
return c.err
}
c.includes.ensureInclude()
c.sliceTransformed = []map[string]interface{}{}
for _, value := range c.sliceData {
i := &ItemResource{}
i.SetData(value)
// 设置 transformer
i.SetTransformer(c.transformer)
i.includes.setNestedInclude(c.includes.nestedInclude)
if err := i.TransformResource(); err != nil {
return err
}
c.sliceTransformed = append(c.sliceTransformed, i.transformed)
}
return nil
}
func (c *CollectionResource) GetTransformedData() interface{} {
return c.sliceTransformed
}