This repository has been archived by the owner on Aug 23, 2023. It is now read-only.
forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_info.go
118 lines (100 loc) · 2.44 KB
/
field_info.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
package jsoninfo
import (
"reflect"
"strings"
"unicode"
"unicode/utf8"
)
// FieldInfo contains information about JSON serialization of a field.
type FieldInfo struct {
MultipleFields bool // Whether multiple Go fields share this JSON name
HasJSONTag bool
TypeIsMarshaller bool
TypeIsUnmarshaller bool
JSONOmitEmpty bool
JSONString bool
Index []int
Type reflect.Type
JSONName string
}
func AppendFields(fields []FieldInfo, parentIndex []int, t reflect.Type) []FieldInfo {
// For each field
numField := t.NumField()
iteration:
for i := 0; i < numField; i++ {
f := t.Field(i)
index := make([]int, 0, len(parentIndex)+1)
index = append(index, parentIndex...)
index = append(index, i)
// See whether this is an embedded field
if f.Anonymous {
if f.Tag.Get("json") == "-" {
continue
}
fields = AppendFields(fields, index, f.Type)
continue iteration
}
// Ignore certain types
switch f.Type.Kind() {
case reflect.Func, reflect.Chan:
continue iteration
}
// Is it a private (lowercase) field?
firstRune, _ := utf8.DecodeRuneInString(f.Name)
if unicode.IsLower(firstRune) {
continue iteration
}
// Declare a field
field := FieldInfo{
Index: index,
Type: f.Type,
JSONName: f.Name,
}
// Read "json" tag
jsonTag := f.Tag.Get("json")
// Read our custom "multijson" tag that
// allows multiple fields with the same name.
if v := f.Tag.Get("multijson"); v != "" {
field.MultipleFields = true
jsonTag = v
}
// Handle "-"
if jsonTag == "-" {
continue
}
// Parse the tag
if jsonTag != "" {
field.HasJSONTag = true
for i, part := range strings.Split(jsonTag, ",") {
if i == 0 {
if part != "" {
field.JSONName = part
}
} else {
switch part {
case "omitempty":
field.JSONOmitEmpty = true
case "string":
field.JSONString = true
}
}
}
}
_, field.TypeIsMarshaller = field.Type.MethodByName("MarshalJSON")
_, field.TypeIsUnmarshaller = field.Type.MethodByName("UnmarshalJSON")
// Field is done
fields = append(fields, field)
}
return fields
}
type sortableFieldInfos []FieldInfo
func (list sortableFieldInfos) Len() int {
return len(list)
}
func (list sortableFieldInfos) Less(i, j int) bool {
return list[i].JSONName < list[j].JSONName
}
func (list sortableFieldInfos) Swap(i, j int) {
a, b := list[i], list[j]
list[i], list[j] = b, a
}