-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoner.go
More file actions
95 lines (85 loc) · 2.32 KB
/
jsoner.go
File metadata and controls
95 lines (85 loc) · 2.32 KB
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
package jsoner
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/gomig/utils"
)
type JsonerOption struct {
Only []string
Ignore []string
}
// Jsoner marshal json with dynamic ignores
//
// you must pass path of json data as key and options as value.
// e.g. option to marshal isbn and title of author book, map[string]JsonerOption{"author.book": JsonerOption{Only: []string{"isbn", "title"}}}
// "." path used for root
func Jsoner(v any, opt map[string]JsonerOption) ([]byte, error) {
return json.Marshal(__mapper(v, ".", opt))
}
func JsonerIndent(v any, indent string, opt map[string]JsonerOption) ([]byte, error) {
return json.MarshalIndent(__mapper(v, ".", opt), "", indent)
}
func __mapper(v any, _path string, opt map[string]JsonerOption) any {
_option := func(path string) ([]string, []string) {
if path != "." {
path = strings.TrimLeft(path, ".")
}
if v, ok := opt[path]; ok {
return v.Only, v.Ignore
}
return []string{}, []string{}
}
_isPointer := func(kind reflect.Kind) bool {
return reflect.Ptr == kind
}
_isSimple := func(kind reflect.Kind) bool {
return !utils.Contains([]reflect.Kind{reflect.Array, reflect.Slice, reflect.Map, reflect.Struct}, kind)
}
// Check nil
if v == nil {
return v
}
val := reflect.ValueOf(v)
// Get value of pointer
if _isPointer(val.Kind()) {
if val.IsNil() {
return nil
}
return __mapper(val.Elem().Interface(), _path, opt)
}
// return non-struct field
if _isSimple(val.Kind()) {
return v
}
switch val.Kind() {
case reflect.Array:
case reflect.Slice:
_sliceV := make([]any, 0)
for i := 0; i < val.Len(); i++ {
_sliceV = append(_sliceV, __mapper(val.Index(i).Interface(), _path, opt))
}
return _sliceV
case reflect.Map:
res := make(map[string]any)
only, ignore := _option(_path)
for _, k := range val.MapKeys() {
// check ignore field
name := fmt.Sprint(k.Interface())
if (len(only) > 0 && !utils.Contains(only, name)) || (len(ignore) > 0 && utils.Contains(ignore, name)) {
continue
}
// Parse value
value := val.MapIndex(k)
res[name] = __mapper(value.Interface(), utils.If(_path == ".", "."+name, _path+"."+name), opt)
}
return res
case reflect.Struct:
bytes, _ := json.Marshal(val.Interface())
var res map[string]any
json.Unmarshal(bytes, &res)
return __mapper(res, _path, opt)
}
return nil
}