-
Notifications
You must be signed in to change notification settings - Fork 189
/
transformer_json.go
94 lines (83 loc) · 2.96 KB
/
transformer_json.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
package admin
import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"github.com/qor/roles"
)
// JSONTransformer json transformer
type JSONTransformer struct{}
// CouldEncode check if encodable
func (JSONTransformer) CouldEncode(encoder Encoder) bool {
return true
}
// Encode encode encoder to writer as JSON
func (JSONTransformer) Encode(writer io.Writer, encoder Encoder) error {
var (
context = encoder.Context
res = encoder.Resource
)
js, err := json.MarshalIndent(convertObjectToJSONMap(res, context, encoder.Result, encoder.Action), "", "\t")
if err != nil {
result := make(map[string]string)
result["error"] = err.Error()
js, _ = json.Marshal(result)
}
if w, ok := writer.(http.ResponseWriter); ok {
w.Header().Set("Content-Type", "application/json")
}
_, err = writer.Write(js)
return err
}
func convertObjectToJSONMap(res *Resource, context *Context, value interface{}, kind string) interface{} {
reflectValue := reflect.ValueOf(value)
for reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
switch reflectValue.Kind() {
case reflect.Slice:
values := []interface{}{}
for i := 0; i < reflectValue.Len(); i++ {
if reflect.Indirect(reflectValue.Index(i)).Kind() == reflect.Struct {
if reflectValue.Index(i).Kind() == reflect.Ptr {
values = append(values, convertObjectToJSONMap(res, context, reflectValue.Index(i).Interface(), kind))
} else {
values = append(values, convertObjectToJSONMap(res, context, reflectValue.Index(i).Addr().Interface(), kind))
}
} else {
values = append(values, fmt.Sprint(reflectValue.Index(i).Interface()))
}
}
return values
case reflect.Struct:
var metas []*Meta
if kind == "index" {
metas = res.ConvertSectionToMetas(res.allowedSections(res.IndexAttrs(), context, roles.Read))
} else if kind == "edit" {
metas = res.ConvertSectionToMetas(res.allowedSections(res.EditAttrs(), context, roles.Update))
} else if kind == "show" {
metas = res.ConvertSectionToMetas(res.allowedSections(res.ShowAttrs(), context, roles.Read))
}
values := map[string]interface{}{}
for _, meta := range metas {
if meta.HasPermission(roles.Read, context.Context) {
// has_one, has_many checker to avoid dead loop
if meta.Resource != nil && (meta.FieldStruct != nil && meta.FieldStruct.Relationship != nil && (meta.FieldStruct.Relationship.Kind == "has_one" || meta.FieldStruct.Relationship.Kind == "has_many" || meta.Type == "single_edit" || meta.Type == "collection_edit")) {
values[meta.GetName()] = convertObjectToJSONMap(meta.Resource, context, context.RawValueOf(value, meta), kind)
} else {
values[meta.GetName()] = context.FormattedValueOf(value, meta)
}
}
}
return values
case reflect.Map:
for _, key := range reflectValue.MapKeys() {
reflectValue.SetMapIndex(key, reflect.ValueOf(convertObjectToJSONMap(res, context, reflectValue.MapIndex(key).Interface(), kind)))
}
return reflectValue.Interface()
default:
return value
}
}