-
Notifications
You must be signed in to change notification settings - Fork 251
/
generate-extension.go
386 lines (344 loc) · 12.2 KB
/
generate-extension.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// 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.
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"github.com/google/gnostic/compiler"
"github.com/google/gnostic/jsonschema"
"github.com/google/gnostic/printer"
)
var protoOptionsForExtensions = []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: "VendorExtensionProto",
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.",
},
}
const additionalCompilerCodeWithMain = "" +
"func handleExtension(extensionName string, yamlInput string) (bool, proto.Message, error) {\n" +
" switch extensionName {\n" +
" // All supported extensions\n" +
" %s\n" +
" default:\n" +
" return false, nil, nil\n" +
" }\n" +
"}\n" +
"\n" +
"func main() {\n" +
" gnostic_extension_v1.Main(handleExtension)\n" +
"}\n"
const caseStringForObjectTypes = "\n" +
"case \"%s\":\n" +
"var info yaml.Node\n" +
"err := yaml.Unmarshal([]byte(yamlInput), &info)\n" +
"if err != nil {\n" +
" return true, nil, err\n" +
"}\n" +
"info = *info.Content[0]\n" +
"newObject, err := %s.New%s(&info, compiler.NewContext(\"$root\", &info, nil))\n" +
"return true, newObject, err"
const caseStringForWrapperTypes = "\n" +
"case \"%s\":\n" +
"var info yaml.Node\n" +
"err := yaml.Unmarshal([]byte(yamlInput), &info)\n" +
"if err != nil {\n" +
" return true, nil, err\n" +
"}\n" +
"v, ok := compiler.%sForScalarNode(&info)\n" +
"if !ok {\n" +
" return true, nil, nil\n" +
"}\n" +
"newObject := &wrapperspb.%s{Value: v}\n" +
"return true, newObject, nil"
// generateMainFile generates the main program for an extension.
func generateMainFile(packageName string, license string, codeBody string, imports []string) string {
code := &printer.Code{}
code.Print(license)
code.Print("// THIS FILE IS AUTOMATICALLY GENERATED.\n")
// generate package declaration
code.Print("package %s\n", packageName)
code.Print("import (")
for _, filename := range imports {
code.Print("\"" + filename + "\"")
}
code.Print(")\n")
code.Print(codeBody)
return code.String()
}
func getBaseFileNameWithoutExt(filePath string) string {
tmp := filepath.Base(filePath)
return tmp[0 : len(tmp)-len(filepath.Ext(tmp))]
}
func toProtoPackageName(input string) string {
var out = ""
nonAlphaNumeric := regexp.MustCompile("[^0-9A-Za-z_]+")
input = nonAlphaNumeric.ReplaceAllString(input, "")
for index, character := range input {
if character >= 'A' && character <= 'Z' {
if index > 0 && input[index-1] != '_' {
out += "_"
}
out += string(character - 'A' + 'a')
} else {
out += string(character)
}
}
return out
}
type primitiveTypeInfo struct {
goTypeName string
wrapperProtoName string
}
var supportedPrimitiveTypeInfos = map[string]primitiveTypeInfo{
"string": primitiveTypeInfo{goTypeName: "String", wrapperProtoName: "StringValue"},
"number": primitiveTypeInfo{goTypeName: "Float", wrapperProtoName: "DoubleValue"},
"integer": primitiveTypeInfo{goTypeName: "Int", wrapperProtoName: "Int64Value"},
"boolean": primitiveTypeInfo{goTypeName: "Bool", wrapperProtoName: "BoolValue"},
// TODO: Investigate how to support arrays. For now users will not be allowed to
// create extension handlers for arrays and they will have to use the
// plane yaml string as is.
}
type generatedTypeInfo struct {
schemaName string
// if this is not nil, the schema should be treataed as a primitive type.
optionalPrimitiveTypeInfo *primitiveTypeInfo
}
// generateExtension generates the implementation of an extension.
func generateExtension(schemaFile string, outDir string) error {
outFileBaseName := getBaseFileNameWithoutExt(schemaFile)
extensionNameWithoutXDashPrefix := outFileBaseName[len("x-"):]
outDir = path.Join(outDir, "gnostic-x-"+extensionNameWithoutXDashPrefix)
protoPackage := toProtoPackageName(extensionNameWithoutXDashPrefix)
protoPackageName := strings.ToLower(protoPackage)
goPackageName := protoPackageName
protoOutDirectory := outDir + "/" + "proto"
var err error
baseSchema, err := jsonschema.NewBaseSchema()
if err != nil {
return err
}
baseSchema.ResolveRefs()
baseSchema.ResolveAllOfs()
openapiSchema, err := jsonschema.NewSchemaFromFile(schemaFile)
if err != nil {
return err
}
openapiSchema.ResolveRefs()
openapiSchema.ResolveAllOfs()
// build a simplified model of the types described by the schema
cc := NewDomain(openapiSchema, "v2") // TODO fix for OpenAPI v3
// create a type for each object defined in the schema
extensionNameToMessageName := make(map[string]generatedTypeInfo)
schemaErrors := make([]error, 0)
supportedPrimitives := make([]string, 0)
for key := range supportedPrimitiveTypeInfos {
supportedPrimitives = append(supportedPrimitives, key)
}
sort.Strings(supportedPrimitives)
if cc.Schema.Definitions != nil {
for _, pair := range *(cc.Schema.Definitions) {
definitionName := pair.Name
definitionSchema := pair.Value
// ensure the id field is set
if definitionSchema.ID == nil || len(*(definitionSchema.ID)) == 0 {
schemaErrors = append(schemaErrors,
fmt.Errorf("schema %s has no 'id' field, which must match the "+
"name of the OpenAPI extension that the schema represents",
definitionName))
} else {
if _, ok := extensionNameToMessageName[*(definitionSchema.ID)]; ok {
schemaErrors = append(schemaErrors,
fmt.Errorf("schema %s and %s have the same 'id' field value",
definitionName, extensionNameToMessageName[*(definitionSchema.ID)].schemaName))
} else if (definitionSchema.Type == nil) || (*definitionSchema.Type.String == "object") {
extensionNameToMessageName[*(definitionSchema.ID)] = generatedTypeInfo{schemaName: definitionName}
} else {
// this is a primitive type
if val, ok := supportedPrimitiveTypeInfos[*definitionSchema.Type.String]; ok {
extensionNameToMessageName[*(definitionSchema.ID)] = generatedTypeInfo{schemaName: definitionName, optionalPrimitiveTypeInfo: &val}
} else {
schemaErrors = append(schemaErrors,
fmt.Errorf("Schema %s has type '%s' which is "+
"not supported. Supported primitive types are "+
"%s.\n", definitionName,
*definitionSchema.Type.String,
supportedPrimitives))
}
}
}
typeName := cc.TypeNameForStub(definitionName)
typeModel := cc.BuildTypeForDefinition(typeName, definitionName, definitionSchema)
if typeModel != nil {
cc.TypeModels[typeName] = typeModel
}
}
}
if len(schemaErrors) > 0 {
// error has been reported.
return compiler.NewErrorGroupOrNil(schemaErrors)
}
err = os.MkdirAll(outDir, os.ModePerm)
if err != nil {
return err
}
err = os.MkdirAll(protoOutDirectory, os.ModePerm)
if err != nil {
return err
}
// generate the protocol buffer description
protoOptions := append(protoOptionsForExtensions,
ProtoOption{Name: "java_package", Value: "org.openapi.extension." + strings.ToLower(protoPackage), Comment: "// The Java package name must be proto package name with proper prefix."},
ProtoOption{Name: "objc_class_prefix", Value: strings.ToLower(protoPackage),
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: "./;" + strings.ToLower(protoPackage),
Comment: "// The Go package path.",
},
)
proto := cc.generateProto(protoPackageName, License, protoOptions, nil)
protoFilename := path.Join(protoOutDirectory, outFileBaseName+".proto")
err = ioutil.WriteFile(protoFilename, []byte(proto), 0644)
if err != nil {
return err
}
// generate the compiler
compiler := cc.GenerateCompiler(goPackageName, License, []string{
"fmt",
"regexp",
"strings",
"github.com/google/gnostic/compiler",
"gopkg.in/yaml.v3",
})
goFilename := path.Join(protoOutDirectory, outFileBaseName+".go")
err = ioutil.WriteFile(goFilename, []byte(compiler), 0644)
if err != nil {
return err
}
err = exec.Command(runtime.GOROOT()+"/bin/gofmt", "-w", goFilename).Run()
if err != nil {
return err
}
// generate the main file.
// TODO: This path is currently fixed to the location of the samples.
// Can we make it relative, perhaps with an option or by generating
// a go.mod file for the generated extension handler?
outDirRelativeToPackageRoot := "github.com/google/gnostic/extensions/sample/" + outDir
var extensionNameKeys []string
for k := range extensionNameToMessageName {
extensionNameKeys = append(extensionNameKeys, k)
}
sort.Strings(extensionNameKeys)
wrapperTypeIncluded := false
var cases string
for _, extensionName := range extensionNameKeys {
if extensionNameToMessageName[extensionName].optionalPrimitiveTypeInfo == nil {
cases += fmt.Sprintf(caseStringForObjectTypes,
extensionName,
goPackageName,
extensionNameToMessageName[extensionName].schemaName)
} else {
wrapperTypeIncluded = true
cases += fmt.Sprintf(caseStringForWrapperTypes,
extensionName,
extensionNameToMessageName[extensionName].optionalPrimitiveTypeInfo.goTypeName,
extensionNameToMessageName[extensionName].optionalPrimitiveTypeInfo.wrapperProtoName)
}
}
extMainCode := fmt.Sprintf(additionalCompilerCodeWithMain, cases)
imports := []string{
"github.com/google/gnostic/extensions",
"github.com/google/gnostic/compiler",
"google.golang.org/protobuf/proto",
"gopkg.in/yaml.v3",
outDirRelativeToPackageRoot + "/" + "proto",
}
if wrapperTypeIncluded {
imports = append(imports, "google.golang.org/protobuf/types/known/wrapperspb")
}
main := generateMainFile("main", License, extMainCode, imports)
mainFileName := path.Join(outDir, "main.go")
err = ioutil.WriteFile(mainFileName, []byte(main), 0644)
if err != nil {
return err
}
// format the compiler
return exec.Command(runtime.GOROOT()+"/bin/gofmt", "-w", mainFileName).Run()
}
func generateExtensions() error {
outDir := ""
schemaFile := ""
extParamRegex, _ := regexp.Compile("--(.+)=(.+)")
for i, arg := range os.Args {
if i == 0 {
continue // skip the tool name
}
var m [][]byte
if m = extParamRegex.FindSubmatch([]byte(arg)); m != nil {
flagName := string(m[1])
flagValue := string(m[2])
switch flagName {
case "out_dir":
outDir = flagValue
default:
fmt.Printf("Unknown option: %s.\n%s\n", arg, usage())
os.Exit(-1)
}
} else if arg == "--extension" {
continue
} else if arg[0] == '-' {
fmt.Printf("Unknown option: %s.\n%s\n", arg, usage())
os.Exit(-1)
} else {
schemaFile = arg
}
}
if schemaFile == "" {
fmt.Printf("No input json schema specified.\n%s\n", usage())
os.Exit(-1)
}
if outDir == "" {
fmt.Printf("Missing output directive.\n%s\n", usage())
os.Exit(-1)
}
if !strings.HasPrefix(getBaseFileNameWithoutExt(schemaFile), "x-") {
fmt.Printf("Schema file name has to start with 'x-'.\n%s\n", usage())
os.Exit(-1)
}
return generateExtension(schemaFile, outDir)
}