Skip to content

Commit 836f55b

Browse files
authored
Migrate gnostic to use gnostic-models (#400)
In order to avoid a panic at runtime in a downstream project that pulls in gnostic and gnostic-models, create a new `<proto>.pbalias.go` file which uses type aliases and variables to point to the equivalent protobuf types in gnostic-models. This will prevent any API breakage for code using the previous types from gnostic, and allow for this logic to be maintained in one place going forward.
1 parent 987797b commit 836f55b

31 files changed

+411
-28066
lines changed

COMPILE-PROTOS.sh

-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717

1818
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
1919

20-
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative openapiv2/*.proto
21-
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative openapiv3/*.proto
22-
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative discovery/*.proto
2320
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative plugins/*.proto
24-
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative extensions/*.proto
2521
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative surface/*.proto
26-
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative metrics/*.proto
2722
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative metrics/*.proto

compiler/context.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,14 @@
1515
package compiler
1616

1717
import (
18-
yaml "gopkg.in/yaml.v3"
18+
"github.com/google/gnostic-models/compiler"
1919
)
2020

2121
// Context contains state of the compiler as it traverses a document.
22-
type Context struct {
23-
Parent *Context
24-
Name string
25-
Node *yaml.Node
26-
ExtensionHandlers *[]ExtensionHandler
27-
}
22+
type Context = compiler.Context
2823

2924
// NewContextWithExtensions returns a new object representing the compiler state
30-
func NewContextWithExtensions(name string, node *yaml.Node, parent *Context, extensionHandlers *[]ExtensionHandler) *Context {
31-
return &Context{Name: name, Node: node, Parent: parent, ExtensionHandlers: extensionHandlers}
32-
}
25+
var NewContextWithExtensions = compiler.NewContextWithExtensions
3326

3427
// NewContext returns a new object representing the compiler state
35-
func NewContext(name string, node *yaml.Node, parent *Context) *Context {
36-
if parent != nil {
37-
return &Context{Name: name, Node: node, Parent: parent, ExtensionHandlers: parent.ExtensionHandlers}
38-
}
39-
return &Context{Name: name, Parent: parent, ExtensionHandlers: nil}
40-
}
41-
42-
// Description returns a text description of the compiler state
43-
func (context *Context) Description() string {
44-
name := context.Name
45-
if context.Parent != nil {
46-
name = context.Parent.Description() + "." + name
47-
}
48-
return name
49-
}
28+
var NewContext = compiler.NewContext

compiler/error.go

+7-46
Original file line numberDiff line numberDiff line change
@@ -14,57 +14,18 @@
1414

1515
package compiler
1616

17-
import "fmt"
17+
import (
18+
"github.com/google/gnostic-models/compiler"
19+
)
1820

1921
// Error represents compiler errors and their location in the document.
20-
type Error struct {
21-
Context *Context
22-
Message string
23-
}
22+
type Error = compiler.Error
2423

2524
// NewError creates an Error.
26-
func NewError(context *Context, message string) *Error {
27-
return &Error{Context: context, Message: message}
28-
}
29-
30-
func (err *Error) locationDescription() string {
31-
if err.Context.Node != nil {
32-
return fmt.Sprintf("[%d,%d] %s", err.Context.Node.Line, err.Context.Node.Column, err.Context.Description())
33-
}
34-
return err.Context.Description()
35-
}
36-
37-
// Error returns the string value of an Error.
38-
func (err *Error) Error() string {
39-
if err.Context == nil {
40-
return err.Message
41-
}
42-
return err.locationDescription() + " " + err.Message
43-
}
25+
var NewError = compiler.NewError
4426

4527
// ErrorGroup is a container for groups of Error values.
46-
type ErrorGroup struct {
47-
Errors []error
48-
}
28+
type ErrorGroup = compiler.ErrorGroup
4929

5030
// NewErrorGroupOrNil returns a new ErrorGroup for a slice of errors or nil if the slice is empty.
51-
func NewErrorGroupOrNil(errors []error) error {
52-
if len(errors) == 0 {
53-
return nil
54-
} else if len(errors) == 1 {
55-
return errors[0]
56-
} else {
57-
return &ErrorGroup{Errors: errors}
58-
}
59-
}
60-
61-
func (group *ErrorGroup) Error() string {
62-
result := ""
63-
for i, err := range group.Errors {
64-
if i > 0 {
65-
result += "\n"
66-
}
67-
result += err.Error()
68-
}
69-
return result
70-
}
31+
var NewErrorGroupOrNil = compiler.NewErrorGroupOrNil

compiler/extensions.go

+3-64
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,11 @@
1515
package compiler
1616

1717
import (
18-
"bytes"
19-
"fmt"
20-
"os/exec"
21-
"strings"
22-
23-
"github.com/golang/protobuf/proto"
24-
"github.com/golang/protobuf/ptypes/any"
25-
yaml "gopkg.in/yaml.v3"
26-
27-
extensions "github.com/google/gnostic/extensions"
18+
"github.com/google/gnostic-models/compiler"
2819
)
2920

3021
// ExtensionHandler describes a binary that is called by the compiler to handle specification extensions.
31-
type ExtensionHandler struct {
32-
Name string
33-
}
22+
type ExtensionHandler = compiler.ExtensionHandler
3423

3524
// CallExtension calls a binary extension handler.
36-
func CallExtension(context *Context, in *yaml.Node, extensionName string) (handled bool, response *any.Any, err error) {
37-
if context == nil || context.ExtensionHandlers == nil {
38-
return false, nil, nil
39-
}
40-
handled = false
41-
for _, handler := range *(context.ExtensionHandlers) {
42-
response, err = handler.handle(in, extensionName)
43-
if response == nil {
44-
continue
45-
} else {
46-
handled = true
47-
break
48-
}
49-
}
50-
return handled, response, err
51-
}
52-
53-
func (extensionHandlers *ExtensionHandler) handle(in *yaml.Node, extensionName string) (*any.Any, error) {
54-
if extensionHandlers.Name != "" {
55-
yamlData, _ := yaml.Marshal(in)
56-
request := &extensions.ExtensionHandlerRequest{
57-
CompilerVersion: &extensions.Version{
58-
Major: 0,
59-
Minor: 1,
60-
Patch: 0,
61-
},
62-
Wrapper: &extensions.Wrapper{
63-
Version: "unknown", // TODO: set this to the type/version of spec being parsed.
64-
Yaml: string(yamlData),
65-
ExtensionName: extensionName,
66-
},
67-
}
68-
requestBytes, _ := proto.Marshal(request)
69-
cmd := exec.Command(extensionHandlers.Name)
70-
cmd.Stdin = bytes.NewReader(requestBytes)
71-
output, err := cmd.Output()
72-
if err != nil {
73-
return nil, err
74-
}
75-
response := &extensions.ExtensionHandlerResponse{}
76-
err = proto.Unmarshal(output, response)
77-
if err != nil || !response.Handled {
78-
return nil, err
79-
}
80-
if len(response.Errors) != 0 {
81-
return nil, fmt.Errorf("Errors when parsing: %+v for field %s by vendor extension handler %s. Details %+v", in, extensionName, extensionHandlers.Name, strings.Join(response.Errors, ","))
82-
}
83-
return response.Value, nil
84-
}
85-
return nil, nil
86-
}
25+
var CallExtension = compiler.CallExtension

0 commit comments

Comments
 (0)