Skip to content

Commit fbbf904

Browse files
committed
Move surface model generation from code generators up into gnostic.
1 parent e815c22 commit fbbf904

File tree

4 files changed

+87
-66
lines changed

4 files changed

+87
-66
lines changed

gnostic.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,35 +49,36 @@ import (
4949
"github.com/googleapis/gnostic/discovery"
5050
"github.com/googleapis/gnostic/jsonwriter"
5151
plugins "github.com/googleapis/gnostic/plugins"
52+
surface "github.com/googleapis/gnostic/surface"
5253
"gopkg.in/yaml.v2"
5354
)
5455

55-
const ( // OpenAPI Version
56-
openAPIvUnknown = 0
57-
openAPIv2 = 2
58-
openAPIv3 = 3
59-
discoveryFormat = 4
56+
const ( // Source Format
57+
SourceFormatUnknown = 0
58+
SourceFormatOpenAPI2 = 2
59+
SourceFormatOpenAPI3 = 3
60+
SourceFormatDiscovery = 4
6061
)
6162

6263
// Determine the version of an OpenAPI description read from JSON or YAML.
6364
func getOpenAPIVersionFromInfo(info interface{}) int {
6465
m, ok := compiler.UnpackMap(info)
6566
if !ok {
66-
return openAPIvUnknown
67+
return SourceFormatUnknown
6768
}
6869
swagger, ok := compiler.MapValueForKey(m, "swagger").(string)
6970
if ok && strings.HasPrefix(swagger, "2.0") {
70-
return openAPIv2
71+
return SourceFormatOpenAPI2
7172
}
7273
openapi, ok := compiler.MapValueForKey(m, "openapi").(string)
7374
if ok && strings.HasPrefix(openapi, "3.0") {
74-
return openAPIv3
75+
return SourceFormatOpenAPI3
7576
}
7677
kind, ok := compiler.MapValueForKey(m, "kind").(string)
7778
if ok && kind == "discovery#restDescription" {
78-
return discoveryFormat
79+
return SourceFormatDiscovery
7980
}
80-
return openAPIvUnknown
81+
return SourceFormatUnknown
8182
}
8283

8384
const (
@@ -91,7 +92,7 @@ type pluginCall struct {
9192
}
9293

9394
// Invokes a plugin.
94-
func (p *pluginCall) perform(document proto.Message, openAPIVersion int, sourceName string) error {
95+
func (p *pluginCall) perform(document proto.Message, sourceFormat int, sourceName string) error {
9596
if p.Name != "" {
9697
request := &plugins.Request{}
9798

@@ -142,13 +143,16 @@ func (p *pluginCall) perform(document proto.Message, openAPIVersion int, sourceN
142143
request.OutputPath = outputLocation
143144

144145
request.SourceName = sourceName
145-
switch openAPIVersion {
146-
case openAPIv2:
146+
switch sourceFormat {
147+
case SourceFormatOpenAPI2:
147148
request.Openapi2 = document.(*openapi_v2.Document)
148-
case openAPIv3:
149+
request.Surface, _ = surface.NewModelFromOpenAPI2(request.Openapi2)
150+
case SourceFormatOpenAPI3:
149151
request.Openapi3 = document.(*openapi_v3.Document)
152+
request.Surface, _ = surface.NewModelFromOpenAPI3(request.Openapi3)
150153
default:
151154
}
155+
152156
requestBytes, _ := proto.Marshal(request)
153157

154158
cmd := exec.Command(executableName, "-plugin")
@@ -232,7 +236,7 @@ type Gnostic struct {
232236
resolveReferences bool
233237
pluginCalls []*pluginCall
234238
extensionHandlers []compiler.ExtensionHandler
235-
openAPIVersion int
239+
sourceFormat int
236240
}
237241

238242
// Initialize a structure to store global application state.
@@ -340,18 +344,18 @@ func (g *Gnostic) readOpenAPIText(bytes []byte) (message proto.Message, err erro
340344
return nil, err
341345
}
342346
// Determine the OpenAPI version.
343-
g.openAPIVersion = getOpenAPIVersionFromInfo(info)
344-
if g.openAPIVersion == openAPIvUnknown {
347+
g.sourceFormat = getOpenAPIVersionFromInfo(info)
348+
if g.sourceFormat == SourceFormatUnknown {
345349
return nil, errors.New("unable to identify OpenAPI version")
346350
}
347351
// Compile to the proto model.
348-
if g.openAPIVersion == openAPIv2 {
352+
if g.sourceFormat == SourceFormatOpenAPI2 {
349353
document, err := openapi_v2.NewDocument(info, compiler.NewContextWithExtensions("$root", nil, &g.extensionHandlers))
350354
if err != nil {
351355
return nil, err
352356
}
353357
message = document
354-
} else if g.openAPIVersion == openAPIv3 {
358+
} else if g.sourceFormat == SourceFormatOpenAPI3 {
355359
document, err := openapi_v3.NewDocument(info, compiler.NewContextWithExtensions("$root", nil, &g.extensionHandlers))
356360
if err != nil {
357361
return nil, err
@@ -373,21 +377,21 @@ func (g *Gnostic) readOpenAPIBinary(data []byte) (message proto.Message, err err
373377
documentV3 := &openapi_v3.Document{}
374378
err = proto.Unmarshal(data, documentV3)
375379
if err == nil && strings.HasPrefix(documentV3.Openapi, "3.0") {
376-
g.openAPIVersion = openAPIv3
380+
g.sourceFormat = SourceFormatOpenAPI3
377381
return documentV3, nil
378382
}
379383
// if that failed, try to read an OpenAPI v2 document
380384
documentV2 := &openapi_v2.Document{}
381385
err = proto.Unmarshal(data, documentV2)
382386
if err == nil && strings.HasPrefix(documentV2.Swagger, "2.0") {
383-
g.openAPIVersion = openAPIv2
387+
g.sourceFormat = SourceFormatOpenAPI2
384388
return documentV2, nil
385389
}
386390
// if that failed, try to read a Discovery Format document
387391
discoveryDocument := &discovery_v1.Document{}
388392
err = proto.Unmarshal(data, discoveryDocument)
389393
if err == nil { // && strings.HasPrefix(documentV2.Swagger, "2.0") {
390-
g.openAPIVersion = discoveryFormat
394+
g.sourceFormat = SourceFormatDiscovery
391395
return discoveryDocument, nil
392396
}
393397
return nil, err
@@ -416,19 +420,19 @@ func (g *Gnostic) writeJSONYAMLOutput(message proto.Message) {
416420
var rawInfo yaml.MapSlice
417421
var ok bool
418422
var err error
419-
if g.openAPIVersion == openAPIv2 {
423+
if g.sourceFormat == SourceFormatOpenAPI2 {
420424
document := message.(*openapi_v2.Document)
421425
rawInfo, ok = document.ToRawInfo().(yaml.MapSlice)
422426
if !ok {
423427
rawInfo = nil
424428
}
425-
} else if g.openAPIVersion == openAPIv3 {
429+
} else if g.sourceFormat == SourceFormatOpenAPI3 {
426430
document := message.(*openapi_v3.Document)
427431
rawInfo, ok = document.ToRawInfo().(yaml.MapSlice)
428432
if !ok {
429433
rawInfo = nil
430434
}
431-
} else if g.openAPIVersion == discoveryFormat {
435+
} else if g.sourceFormat == SourceFormatDiscovery {
432436
document := message.(*discovery_v1.Document)
433437
rawInfo, ok = document.ToRawInfo().(yaml.MapSlice)
434438
if !ok {
@@ -467,10 +471,10 @@ func (g *Gnostic) writeJSONYAMLOutput(message proto.Message) {
467471
func (g *Gnostic) performActions(message proto.Message) (err error) {
468472
// Optionally resolve internal references.
469473
if g.resolveReferences {
470-
if g.openAPIVersion == openAPIv2 {
474+
if g.sourceFormat == SourceFormatOpenAPI2 {
471475
document := message.(*openapi_v2.Document)
472476
_, err = document.ResolveReferences(g.sourceName)
473-
} else if g.openAPIVersion == openAPIv3 {
477+
} else if g.sourceFormat == SourceFormatOpenAPI3 {
474478
document := message.(*openapi_v3.Document)
475479
_, err = document.ResolveReferences(g.sourceName)
476480
}
@@ -492,7 +496,7 @@ func (g *Gnostic) performActions(message proto.Message) (err error) {
492496
}
493497
// Call all specified plugins.
494498
for _, p := range g.pluginCalls {
495-
err := p.perform(message, g.openAPIVersion, g.sourceName)
499+
err := p.perform(message, g.sourceFormat, g.sourceName)
496500
if err != nil {
497501
writeFile(g.errorOutputPath, g.errorBytes(err), g.sourceName, "errors")
498502
defer os.Exit(-1) // run all plugins, even when some have errors

plugins/gnostic-go-generator/main.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ package main
1818

1919
import (
2020
"encoding/json"
21+
"errors"
2122
"strings"
2223

2324
plugins "github.com/googleapis/gnostic/plugins"
24-
surface "github.com/googleapis/gnostic/surface"
2525
)
2626

2727
// This is the main function for the code generation plugin.
@@ -42,15 +42,15 @@ func main() {
4242
files = []string{"client.go", "server.go", "provider.go", "types.go", "constants.go"}
4343
}
4444

45-
// Create the model.
46-
var model *surface.Model
47-
if env.Request.Openapi2 != nil {
48-
model, err = surface.NewModelFromOpenAPI2(env.Request.Openapi2)
49-
} else if env.Request.Openapi3 != nil {
50-
model, err = surface.NewModelFromOpenAPI3(env.Request.Openapi3)
45+
// Get the code surface model.
46+
model := env.Request.Surface
47+
48+
if model == nil {
49+
err = errors.New("No generated code surface model is available.")
50+
env.RespondAndExitIfError(err)
5151
}
52-
env.RespondAndExitIfError(err)
5352

53+
// Customize the code surface model for Go
5454
NewGoLanguageModel().Prepare(model)
5555

5656
modelJSON, _ := json.MarshalIndent(model, "", " ")

plugins/plugin.pb.go

Lines changed: 43 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/plugin.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ syntax = "proto3";
2525
import "github.com/googleapis/gnostic/OpenAPIv2/OpenAPIv2.proto";
2626
import "github.com/googleapis/gnostic/OpenAPIv3/OpenAPIv3.proto";
2727
import "github.com/googleapis/gnostic/discovery/discovery.proto";
28+
import "github.com/googleapis/gnostic/surface/surface.proto";
2829

2930
package gnostic.plugin.v1;
3031

@@ -92,6 +93,9 @@ message Request {
9293

9394
// Discovery API representation
9495
discovery.v1.Document discovery = 7;
96+
97+
// generated code surface representation
98+
surface.v1.Model surface = 8;
9599
}
96100

97101
// The plugin writes an encoded Response to stdout.

0 commit comments

Comments
 (0)