-
Notifications
You must be signed in to change notification settings - Fork 6
/
serialize.go
145 lines (123 loc) · 3.63 KB
/
serialize.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 gody
import (
"fmt"
"reflect"
"strings"
)
// ErrInvalidBody represents all invalid body report
type ErrInvalidBody struct {
Kind reflect.Kind
}
func (e *ErrInvalidBody) Error() string {
return fmt.Sprintln("invalid body:", e.Kind)
}
// ErrInvalidTag represents all invalid tag report
type ErrInvalidTag struct {
Format string
}
func (e *ErrInvalidTag) Error() string {
return fmt.Sprintln("invalid tag:", e.Format)
}
type ErrEmptyTagName struct{}
func (e *ErrEmptyTagName) Error() string {
return "tag name is empty"
}
// Field is a struct to represents the domain about a field inner gody lib
type Field struct {
Name string
Value string
Tags map[string]string
}
func Serialize(b interface{}) ([]Field, error) {
return RawSerialize(DefaultTagName, b)
}
// RawSerialize is a func to serialize/parse all content about the struct input
func RawSerialize(tn string, b interface{}) ([]Field, error) {
if tn == "" {
return nil, &ErrEmptyTagName{}
}
if b == nil {
return nil, &ErrInvalidBody{}
}
valueOf := reflect.ValueOf(b)
typeOf := reflect.TypeOf(b)
if kindOfBody := typeOf.Kind(); kindOfBody != reflect.Struct {
return nil, &ErrInvalidBody{Kind: kindOfBody}
}
fields := make([]Field, 0)
for i := 0; i < typeOf.NumField(); i++ {
field := typeOf.Field(i)
tagString := field.Tag.Get(tn)
if tagString == "" && field.Type.Kind() != reflect.Slice && field.Type.Kind() != reflect.Struct {
continue
}
tagFormats := strings.Fields(tagString)
tags := make(map[string]string)
for _, tagFormat := range tagFormats {
tagFormatSplitted := strings.Split(tagFormat, "=")
if len(tagFormatSplitted) == 2 {
tagFormatRule := tagFormatSplitted[0]
tagFormatValue := tagFormatSplitted[1]
if tagFormatValue == "" {
return nil, &ErrInvalidTag{Format: tagFormat}
}
tags[tagFormatRule] = tagFormatValue
continue
}
if len(tagFormatSplitted) == 1 {
tagFormatRule := tagFormatSplitted[0]
tags[tagFormatRule] = ""
continue
}
return nil, &ErrInvalidTag{Format: tagFormat}
}
fieldValue := valueOf.FieldByName(field.Name)
fieldNameToLower := strings.ToLower(field.Name)
if fieldNameFromJSONTag := field.Tag.Get("json"); fieldNameFromJSONTag != "" {
fieldNameToLower = fieldNameFromJSONTag
}
if kindOfField := field.Type.Kind(); kindOfField == reflect.Struct {
if fieldConverted := fieldValue.Convert(fieldValue.Type()); fieldConverted.CanInterface() {
payload := fieldConverted.Interface()
serialized, err := RawSerialize(tn, payload)
if err != nil {
return nil, err
}
for _, item := range serialized {
fields = append(fields, Field{
Name: fmt.Sprintf("%s.%s", fieldNameToLower, item.Name),
Value: item.Value,
Tags: item.Tags,
})
}
}
} else if kindOfField := field.Type.Kind(); kindOfField == reflect.Slice {
j := fieldValue.Len()
for i := 0; i < j; i++ {
sliceFieldValue := fieldValue.Index(i)
if sliceFieldConverted := sliceFieldValue.Convert(sliceFieldValue.Type()); sliceFieldConverted.CanInterface() {
payload := sliceFieldValue.Convert(sliceFieldValue.Type()).Interface()
serialized, err := RawSerialize(tn, payload)
if err != nil {
return nil, err
}
for _, item := range serialized {
fields = append(fields, Field{
Name: fmt.Sprintf("%s[%v].%s", fieldNameToLower, i, item.Name),
Value: item.Value,
Tags: item.Tags,
})
}
}
}
} else {
fieldValueString := fmt.Sprintf("%v", fieldValue)
fields = append(fields, Field{
Name: fieldNameToLower,
Value: fieldValueString,
Tags: tags,
})
}
}
return fields, nil
}