-
Notifications
You must be signed in to change notification settings - Fork 126
/
schema.go
101 lines (86 loc) · 2.2 KB
/
schema.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
package schema
import (
"encoding/json"
"fmt"
"reflect"
"github.com/invopop/jsonschema"
)
func SchemaBytesFromBytes(d []byte) ([]byte, error) {
var m map[string]interface{}
if err := json.Unmarshal(d, &m); err != nil {
return nil, err
}
return SchemaBytesFromMap(m)
}
func SchemaBytesFromMap(m map[string]interface{}) ([]byte, error) {
goType := parse(m)
// create instance of reflect type
t := reflect.New(goType).Elem()
s := jsonschema.Reflect(t.Interface())
return json.Marshal(s)
}
// parse recursively generates a reflect.Type from the given data.
func parse(data interface{}) reflect.Type {
switch v := data.(type) {
case map[string]interface{}:
return parseObject(v)
case []interface{}:
return parseArray(v)
case string:
return reflect.TypeOf("")
case float64:
// Check if it can be an int.
if v == float64(int(v)) {
return reflect.TypeOf(0)
}
return reflect.TypeOf(0.0)
case bool:
return reflect.TypeOf(false)
case nil:
return reflect.TypeOf(new(interface{})).Elem()
default:
fmt.Printf("Unhandled type: %T\n", v)
return reflect.TypeOf(new(interface{})).Elem()
}
}
// parseObject handles JSON objects by creating a struct type with appropriately typed fields.
func parseObject(obj map[string]interface{}) reflect.Type {
var fields []reflect.StructField
count := 0
for key, val := range obj {
fieldType := parse(val)
defaultValue := formatDefaultValue(val)
tag := fmt.Sprintf(`json:"%s" jsonschema:"default=%s"`, key, defaultValue)
if defaultValue == "" {
tag = fmt.Sprintf(`json:"%s"`, key)
}
field := reflect.StructField{
Name: fmt.Sprintf("Field%d", count),
Type: fieldType,
Tag: reflect.StructTag(tag),
}
fields = append(fields, field)
count++
}
return reflect.StructOf(fields)
}
func formatDefaultValue(val interface{}) string {
switch v := val.(type) {
case string:
return v
case float64, bool:
return fmt.Sprintf("%v", v)
case nil:
return "null"
default:
return ""
}
}
// parseArray handles JSON arrays by determining the type of its elements.
func parseArray(arr []interface{}) reflect.Type {
if len(arr) == 0 {
return reflect.SliceOf(reflect.TypeOf(""))
}
elemType := parse(arr[0])
return reflect.SliceOf(elemType)
}