-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
140 lines (126 loc) · 3.39 KB
/
config.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
package main
import (
"encoding/json"
"reflect"
)
type Settings struct {
Override bool `toml:"override"`
}
type Source struct {
Name string `toml:"name"`
Path string `toml:"path"`
Plugins []string `toml:"plugins"`
}
type Destination struct {
Name string `toml:"name"`
Path string `toml:"path"`
IgnoredFields []string `toml:"ignore"`
FieldsMap map[string]string `toml:"map"`
}
type Mapping struct {
Settings Settings `toml:"settings"`
Source Source `toml:"source"`
Destinations []Destination `toml:"destination"`
}
type Config struct {
Mappings []Mapping `toml:"mappings"`
Settings GenSetting `toml:"settings"`
}
func (c *Config) BuildTOMLSchema() string {
schema := make(map[string]any)
t := reflect.TypeOf(c).Elem()
properties := make(map[string]any)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("toml")
if tag == "" {
continue
}
fieldSchema := make(map[string]any)
switch field.Type.Kind() {
case reflect.Struct:
fieldSchema["type"] = "object"
fieldSchema["properties"] = buildStructSchema(field.Type)
case reflect.Slice:
fieldSchema["type"] = "array"
itemType := field.Type.Elem()
if itemType.Kind() == reflect.Struct {
fieldSchema["items"] = map[string]any{
"type": "object",
"properties": buildStructSchema(itemType),
}
} else {
fieldSchema["items"] = map[string]any{
"type": getJSONSchemaType(itemType.Kind()),
}
}
default:
fieldSchema["type"] = getJSONSchemaType(field.Type.Kind())
}
properties[tag] = fieldSchema
}
schema["$schema"] = "http://json-schema.org/draft-07/schema#"
schema["type"] = "object"
schema["properties"] = properties
jsonBytes, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return ""
}
return string(jsonBytes)
}
func buildStructSchema(t reflect.Type) map[string]any {
properties := make(map[string]any)
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
tag := field.Tag.Get("toml")
if tag == "" {
continue
}
fieldSchema := make(map[string]any)
switch field.Type.Kind() {
case reflect.Struct:
fieldSchema["type"] = "object"
fieldSchema["properties"] = buildStructSchema(field.Type)
case reflect.Slice:
fieldSchema["type"] = "array"
itemType := field.Type.Elem()
if itemType.Kind() == reflect.Struct {
fieldSchema["items"] = map[string]any{
"type": "object",
"properties": buildStructSchema(itemType),
}
} else {
fieldSchema["items"] = map[string]any{
"type": getJSONSchemaType(itemType.Kind()),
}
}
case reflect.Map:
fieldSchema["type"] = "object"
fieldSchema["additionalProperties"] = true
default:
fieldSchema["type"] = getJSONSchemaType(field.Type.Kind())
}
properties[tag] = fieldSchema
}
return properties
}
func getJSONSchemaType(kind reflect.Kind) string {
switch kind {
case reflect.String:
return "string"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return "integer"
case reflect.Float32, reflect.Float64:
return "number"
case reflect.Bool:
return "boolean"
default:
return "string"
}
}
type GenSetting struct {
Style string `toml:"style"`
Module string `toml:"module"`
PathFromModule string `toml:"path_from_module"`
}