/
gen-schemas.go
64 lines (54 loc) · 1.19 KB
/
gen-schemas.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
//+build ignore
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/serenize/snaker"
yaml "gopkg.in/yaml.v2"
)
type Schema struct {
ID string `yaml:"id"`
Title string
Tag string
Type string
Properties Properties
Required []string
ExtraProps bool `yaml:"additionalProperties"`
}
type Properties map[string]Property
func (props *Properties) UnmashalYAML(unmarshal func(interface{}) error) error {
return unmarshal(props)
}
type Property struct {
Description string
Type string
Format string
}
func main() {
name := os.Args[1]
buf, err := ioutil.ReadFile(name)
if err != nil {
panic(err)
}
var schema Schema
err = yaml.Unmarshal(buf, &schema)
if err != nil {
panic(err)
}
fmt.Printf("properties=%v\n", schema.Properties)
fmt.Printf("\n\n/////\n")
tag := strings.Split(schema.Tag, "/")
tname := strings.Split(tag[len(tag)-1], "-")[0]
fmt.Printf("type %s struct {\n", snaker.SnakeToCamel(tname))
for k, prop := range schema.Properties {
fmt.Printf("\t%s %v `yaml:\"%s\"` // %q\n",
snaker.SnakeToCamel(k),
prop.Type, k,
strings.TrimSpace(prop.Description),
)
//fmt.Printf("// %#v\n", prop)
}
fmt.Printf("}\n")
}