-
Notifications
You must be signed in to change notification settings - Fork 6
/
generate.go
192 lines (166 loc) · 4.52 KB
/
generate.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package jsonschema2go
import (
"context"
"errors"
"fmt"
"net/url"
"path/filepath"
"sort"
"text/template"
"github.com/ns1/jsonschema2go/internal/cachingloader"
"github.com/ns1/jsonschema2go/internal/crawl"
"github.com/ns1/jsonschema2go/internal/planning"
"github.com/ns1/jsonschema2go/internal/print"
"github.com/ns1/jsonschema2go/pkg/gen"
)
func ExtractName(ctx context.Context, uri string, options ...Option) (string, string, error) {
s := &settings{
planner: planning.Composite,
printer: print.New(nil),
typer: planning.DefaultTyper,
loader: gen.NewLoader(),
}
for _, o := range options {
o(s)
}
ctx, cncl := context.WithCancel(ctx)
defer cncl()
if s.debug {
ctx = gen.SetDebug(ctx)
}
u, err := url.Parse(normalizeURI(uri))
if err != nil {
return "", "", fmt.Errorf("invalid uri: %w", err)
}
schema, err := s.loader.Load(ctx, u)
if err != nil {
return "", "", err
}
t, err := s.typer.TypeInfo(schema)
if errors.Is(err, planning.ErrUnknownType) {
err = nil
}
return t.GoPath, t.Name, err
}
// Generate generates Go source code from the provided JSON schemas. Options can be provided to customize the
// output behavior
func Generate(ctx context.Context, uris []string, options ...Option) error {
s := &settings{
planner: planning.Composite,
printer: print.New(nil),
typer: planning.DefaultTyper,
}
for _, o := range options {
o(s)
}
if s.loader == nil {
c := cachingloader.NewSimple()
defer func() {
_ = c.Close()
}()
s.loader = c
}
ctx, cncl := context.WithCancel(ctx)
defer cncl()
if s.debug {
ctx = gen.SetDebug(ctx)
}
if len(uris) == 0 {
return nil
}
normalized := make([]string, 0, len(uris))
for _, u := range uris {
normalized = append(normalized, normalizeURI(u))
}
sort.Strings(normalized)
grouped, err := crawl.Crawl(ctx, s.planner, s.loader, s.typer, normalized)
if err != nil {
return err
}
return print.Print(ctx, s.printer, grouped, s.prefixes)
}
// Option controls the behavior of jsonschema2go, specifying an alternative to the default configuration
type Option func(s *settings)
// PrefixMap specifies where package prefixes should be mapped to.
func PrefixMap(pairs ...string) Option {
prefixes := prefixPairs(pairs)
return func(s *settings) {
s.prefixes = prefixes
}
}
// Debug enables debug logging
func Debug(opt bool) Option {
return func(s *settings) {
s.debug = opt
}
}
// CustomTypeFunc registers a custom function for generating TypeInfo from a Schema.
func CustomTypeFunc(typeFunc func(schema *gen.Schema) gen.TypeInfo) Option {
return func(s *settings) {
s.typer.TypeFunc = typeFunc
}
}
// CustomPrimitivesMap registers a custom map for mapping from JSONSchema simple types to go primitives.
func CustomPrimitivesMap(primitivesMap map[gen.JSONType]string) Option {
return func(s *settings) {
s.typer.Primitives = primitivesMap
}
}
// CustomPlanners permits providing an entirely custom list of planners, which will be jointed together.
func CustomPlanners(planners ...gen.Planner) Option {
return func(s *settings) {
s.planner = planning.CompositePlanner(planners)
}
}
// TypeFromID defines how to map to type information from IDs
func TypeFromID(pairs ...string) Option {
mapper := planning.TypeFromId(prefixPairs(pairs))
return func(s *settings) {
s.typer.TypeFunc = func(schema *gen.Schema) gen.TypeInfo {
if t := planning.DefaultTypeFunc(schema); !t.Unknown() {
return t
}
if path, name := mapper(schema.ID.String()); name != "" {
return gen.TypeInfo{GoPath: path, Name: name}
}
return gen.TypeInfo{}
}
}
}
// CustomTemplate registers a custom top level template
func CustomTemplate(tmpl *template.Template) Option {
return func(s *settings) {
s.printer = print.New(tmpl)
}
}
// CustomInitialisms registers a custom list of initialisms used in name generation
func CustomInitialisms(names ...string) Option {
return func(s *settings) {
s.typer.Namer = planning.NewNamer(append(names, "id", "http"))
}
}
func prefixPairs(pairs []string) [][2]string {
if len(pairs)%2 != 0 {
panic("must be even list of prefixes")
}
var prefixes [][2]string
for i := 0; i < len(pairs); i += 2 {
prefixes = append(prefixes, [2]string{pairs[i], pairs[i+1]})
}
return prefixes
}
type settings struct {
prefixes [][2]string
typer planning.Typer
planner gen.Planner
printer print.Printer
loader gen.Loader
debug bool
}
func normalizeURI(uriOrFile string) string {
if u, err := url.Parse(uriOrFile); err == nil && u.Scheme != "" {
return uriOrFile
}
p, _ := filepath.Abs(uriOrFile)
return "file:" + p
}