forked from google/gnostic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
276 lines (247 loc) · 8.1 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2017 Google LLC. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// generator generates Protocol Buffer models and support code from
// JSON Schemas. It is used to generate representations of the
// OpenAPI Specification and vendor and specification extensions
// that are added by third-party OpenAPI authors.
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strings"
"golang.org/x/tools/imports"
"github.com/google/gnostic/jsonschema"
)
// License is the software license applied to generated code.
const License = "" +
"// Copyright 2020 Google LLC. All Rights Reserved.\n" +
"//\n" +
"// Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
"// you may not use this file except in compliance with the License.\n" +
"// You may obtain a copy of the License at\n" +
"//\n" +
"// http://www.apache.org/licenses/LICENSE-2.0\n" +
"//\n" +
"// Unless required by applicable law or agreed to in writing, software\n" +
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
"// See the License for the specific language governing permissions and\n" +
"// limitations under the License.\n"
func protoOptions(directoryName string, packageName string) []ProtoOption {
return []ProtoOption{
ProtoOption{
Name: "java_multiple_files",
Value: "true",
Comment: "// This option lets the proto compiler generate Java code inside the package\n" +
"// name (see below) instead of inside an outer class. It creates a simpler\n" +
"// developer experience by reducing one-level of name nesting and be\n" +
"// consistent with most programming languages that don't support outer classes.",
},
ProtoOption{
Name: "java_outer_classname",
Value: "OpenAPIProto",
Comment: "// The Java outer classname should be the filename in UpperCamelCase. This\n" +
"// class is only used to hold proto descriptor, so developers don't need to\n" +
"// work with it directly.",
},
ProtoOption{
Name: "java_package",
Value: "org." + packageName,
Comment: "// The Java package name must be proto package name with proper prefix.",
},
ProtoOption{
Name: "objc_class_prefix",
Value: "OAS",
Comment: "// A reasonable prefix for the Objective-C symbols generated from the package.\n" +
"// It should at a minimum be 3 characters long, all uppercase, and convention\n" +
"// is to use an abbreviation of the package name. Something short, but\n" +
"// hopefully unique enough to not conflict with things that may come along in\n" +
"// the future. 'GPB' is reserved for the protocol buffer implementation itself.",
},
ProtoOption{
Name: "go_package",
Value: "./" + directoryName + ";" + packageName,
Comment: "// The Go package name.",
},
}
}
func generateOpenAPIModel(version string) error {
var input string
var filename string
var protoPackageName string
var directoryName string
switch version {
case "v2":
input = "openapi-2.0.json"
filename = "OpenAPIv2"
protoPackageName = "openapi.v2"
directoryName = "openapiv2"
case "v3":
input = "openapi-3.1.json"
filename = "OpenAPIv3"
protoPackageName = "openapi.v3"
directoryName = "openapiv3"
case "discovery":
input = "discovery.json"
filename = "discovery"
protoPackageName = "discovery.v1"
directoryName = "discovery"
default:
return fmt.Errorf("Unknown OpenAPI version %s", version)
}
goPackageName := strings.Replace(protoPackageName, ".", "_", -1)
projectRoot := "./"
baseSchema, err := jsonschema.NewBaseSchema()
if err != nil {
return err
}
baseSchema.ResolveRefs()
baseSchema.ResolveAllOfs()
openapiSchema, err := jsonschema.NewSchemaFromFile(projectRoot + directoryName + "/" + input)
if err != nil {
return err
}
openapiSchema.ResolveRefs()
openapiSchema.ResolveAllOfs()
// build a simplified model of the types described by the schema
cc := NewDomain(openapiSchema, version)
// generators will map these patterns to the associated property names
// these pattern names are a bit of a hack until we find a more automated way to obtain them
switch version {
case "v2":
cc.TypeNameOverrides = map[string]string{
"VendorExtension": "Any",
}
cc.PropertyNameOverrides = map[string]string{
"PathItem": "Path",
"ResponseValue": "ResponseCode",
}
case "v3":
cc.TypeNameOverrides = map[string]string{
"SpecificationExtension": "Any",
}
cc.PropertyNameOverrides = map[string]string{
"PathItem": "Path",
"ResponseValue": "ResponseCode",
}
case "discovery":
cc.TypeNameOverrides = map[string]string{}
cc.PropertyNameOverrides = map[string]string{}
default:
return fmt.Errorf("Unknown OpenAPI version %s", version)
}
err = cc.Build()
if err != nil {
return err
}
if true {
log.Printf("Type Model:\n%s", cc.Description())
}
// ensure that the target directory exists
err = os.MkdirAll(projectRoot+directoryName, 0755)
if err != nil {
return err
}
// generate the protocol buffer description
log.Printf("Generating protocol buffer description")
proto := cc.generateProto(protoPackageName, License,
protoOptions(directoryName, goPackageName), []string{"google/protobuf/any.proto"})
protoFileName := projectRoot + directoryName + "/" + filename + ".proto"
err = ioutil.WriteFile(protoFileName, []byte(proto), 0644)
if err != nil {
return err
}
packageImports := []string{
"fmt",
"gopkg.in/yaml.v3",
"strings",
"regexp",
"github.com/google/gnostic/compiler",
}
// generate the compiler
log.Printf("Generating compiler support code")
compiler := cc.GenerateCompiler(goPackageName, License, packageImports)
goFileName := projectRoot + directoryName + "/" + filename + ".go"
// format the compiler
log.Printf("Formatting compiler support code")
imports.LocalPrefix = "github.com/google/gnostic"
data, err := imports.Process(goFileName, []byte(compiler), &imports.Options{
TabWidth: 8,
TabIndent: true,
Comments: true,
Fragment: true,
})
if err != nil {
return err
}
return ioutil.WriteFile(goFileName, []byte(data), 0644)
}
func usage() string {
return fmt.Sprintf(`
Usage: %s [OPTIONS]
Options:
--v2
Generate Protocol Buffer representation and support code for OpenAPI v2.
Files are read from and written to appropriate locations in the gnostic
project directory.
--v3
Generate Protocol Buffer representation and support code for OpenAPI v3
Files are read from and written to appropriate locations in the gnostic
project directory.
--extension EXTENSION_SCHEMA [EXTENSIONOPTIONS]
Generate a gnostic extension that reads a set of OpenAPI extensions.
EXTENSION_SCHEMA is the json schema for the OpenAPI extensions to be
supported.
EXTENSION_OPTIONS
--out_dir=PATH: Location for writing extension models and support code.
`, path.Base(os.Args[0]))
}
func main() {
var openapiVersion = ""
var shouldGenerateExtensions = false
for i, arg := range os.Args {
if i == 0 {
continue // skip the tool name
}
if arg == "--v2" {
openapiVersion = "v2"
} else if arg == "--v3" {
openapiVersion = "v3"
} else if arg == "--discovery" {
openapiVersion = "discovery"
} else if arg == "--extension" {
shouldGenerateExtensions = true
break
} else {
fmt.Printf("Unknown option: %s.\n%s\n", arg, usage())
os.Exit(-1)
}
}
if openapiVersion != "" {
err := generateOpenAPIModel(openapiVersion)
if err != nil {
fmt.Printf("%+v\n", err)
}
} else if shouldGenerateExtensions {
err := generateExtensions()
if err != nil {
fmt.Printf("%+v\n", err)
}
} else {
fmt.Printf("%s\n", usage())
}
}