forked from alecthomas/jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelpers.go
96 lines (81 loc) · 2.28 KB
/
helpers.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
package jsonschema
import (
"reflect"
"strings"
)
// Ensure we get a non-pointer type
func getNonPointerType(t reflect.Type) reflect.Type {
if t.Kind() == reflect.Ptr {
t = t.Elem()
return getNonPointerType(t)
}
return t
}
// We need to ensure we get the pointer type to a *value struct* and the interface from a *non-nil* pointer value
func getNonNilPointerTypeAndInterface(t reflect.Type) (reflect.Type, interface{}) {
t = getNonPointerType(t)
// This gives us a pointer value from the *value struct*
pv := reflect.New(t)
t = pv.Type()
pvi := pv.Interface()
return t, pvi
}
// `json:"-"` and `json:"omitempty"` make the field optional
func requiredFromJSONTags(tags []string) bool {
if ignoredByJSONTags(tags) {
return false
}
for _, tag := range tags[1:] {
if tag == "omitempty" {
return false
}
}
return true
}
// Setting RequiredFromJSONSchemaTags to true allows usage of the `required` tag to make a field required
func requiredFromJSONSchemaTags(tags []string) bool {
if ignoredByJSONSchemaTags(tags) {
return false
}
for _, tag := range tags {
if tag == "required" {
return true
}
}
return false
}
// `jsonschema:"optional"` will make the field optional
//
// The use case for this is when you are taking json input where validation on a field should be optional
// but you do not want to declare `omitempty` because you serialize the struct to json to a third party
// and the fields must exist (such as a field that's an int)
func remainsRequiredFromJSONSchemaTags(tags []string, currentlyRequired bool) bool {
for _, tag := range tags {
if tag == "optional" {
return false
}
}
return currentlyRequired
}
func ignoredByJSONTags(tags []string) bool {
return tags[0] == "-"
}
func ignoredByJSONSchemaTags(tags []string) bool {
return tags[0] == "-"
}
// getPackageNameFromPath splits path to struct and return last element which is package name
func getPackageNameFromPath(path string) string {
pathSlices := strings.Split(path, "/")
return pathSlices[len(pathSlices)-1]
}
func getDefinitionKeyFromType(t reflect.Type) string {
packageName := getPackageNameFromPath(t.PkgPath())
return packageName + "." + t.Name()
}
// bool2bytes serializes bool to JSON
func bool2bytes(val bool) []byte {
if val {
return []byte("true")
}
return []byte("false")
}