-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
64 lines (52 loc) · 1013 Bytes
/
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
package jschema
import (
"encoding/json"
"reflect"
)
// JSON returns a JSON representation of the schemas.
// Usually you [json.Marshal] it to generate a json file.
func (s Schemas) JSON() map[string]*Schema {
m := map[string]*Schema{}
for id, v := range s.types {
m[id] = v
}
return m
}
// String returns a json string representation of the schemas.
func (s Schemas) String() string {
b, err := json.MarshalIndent(s.JSON(), "", " ")
if err != nil {
panic(err)
}
return string(b)
}
func (s *Schema) String() string {
b, err := json.MarshalIndent(s, "", " ")
if err != nil {
panic(err)
}
return string(b)
}
type Tag struct {
Name string
Ignore bool
Omitempty bool
String bool
}
func ParseJSONTag(st reflect.StructTag) *Tag {
v := st.Get("json")
if v == "" {
return nil
}
if v == "-" {
return &Tag{
Ignore: true,
}
}
name, t := parseTag(v)
return &Tag{
Name: name,
Omitempty: t.Contains("omitempty"),
String: t.Contains("string"),
}
}