Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate gnostic to use gnostic-models #400

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions COMPILE-PROTOS.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@

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

protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative openapiv2/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative openapiv3/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative discovery/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative plugins/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative extensions/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative surface/*.proto
protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative metrics/*.proto
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the directories which have proto definitions in gnostic-models, and noticed metrics was compiling twice.

protoc -I . -I ./third_party --go_out=. --go_opt=paths=source_relative metrics/*.proto
29 changes: 4 additions & 25 deletions compiler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,14 @@
package compiler

import (
yaml "gopkg.in/yaml.v3"
"github.com/google/gnostic-models/compiler"
)

// Context contains state of the compiler as it traverses a document.
type Context struct {
Parent *Context
Name string
Node *yaml.Node
ExtensionHandlers *[]ExtensionHandler
}
type Context = compiler.Context

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

// NewContext returns a new object representing the compiler state
func NewContext(name string, node *yaml.Node, parent *Context) *Context {
if parent != nil {
return &Context{Name: name, Node: node, Parent: parent, ExtensionHandlers: parent.ExtensionHandlers}
}
return &Context{Name: name, Parent: parent, ExtensionHandlers: nil}
}

// Description returns a text description of the compiler state
func (context *Context) Description() string {
name := context.Name
if context.Parent != nil {
name = context.Parent.Description() + "." + name
}
return name
}
var NewContext = compiler.NewContext
53 changes: 7 additions & 46 deletions compiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,18 @@

package compiler

import "fmt"
import (
"github.com/google/gnostic-models/compiler"
)

// Error represents compiler errors and their location in the document.
type Error struct {
Context *Context
Message string
}
type Error = compiler.Error

// NewError creates an Error.
func NewError(context *Context, message string) *Error {
return &Error{Context: context, Message: message}
}

func (err *Error) locationDescription() string {
if err.Context.Node != nil {
return fmt.Sprintf("[%d,%d] %s", err.Context.Node.Line, err.Context.Node.Column, err.Context.Description())
}
return err.Context.Description()
}

// Error returns the string value of an Error.
func (err *Error) Error() string {
if err.Context == nil {
return err.Message
}
return err.locationDescription() + " " + err.Message
}
var NewError = compiler.NewError

// ErrorGroup is a container for groups of Error values.
type ErrorGroup struct {
Errors []error
}
type ErrorGroup = compiler.ErrorGroup

// NewErrorGroupOrNil returns a new ErrorGroup for a slice of errors or nil if the slice is empty.
func NewErrorGroupOrNil(errors []error) error {
if len(errors) == 0 {
return nil
} else if len(errors) == 1 {
return errors[0]
} else {
return &ErrorGroup{Errors: errors}
}
}

func (group *ErrorGroup) Error() string {
result := ""
for i, err := range group.Errors {
if i > 0 {
result += "\n"
}
result += err.Error()
}
return result
}
var NewErrorGroupOrNil = compiler.NewErrorGroupOrNil
67 changes: 3 additions & 64 deletions compiler/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,11 @@
package compiler

import (
"bytes"
"fmt"
"os/exec"
"strings"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/any"
yaml "gopkg.in/yaml.v3"

extensions "github.com/google/gnostic/extensions"
"github.com/google/gnostic-models/compiler"
)

// ExtensionHandler describes a binary that is called by the compiler to handle specification extensions.
type ExtensionHandler struct {
Name string
}
type ExtensionHandler = compiler.ExtensionHandler

// CallExtension calls a binary extension handler.
func CallExtension(context *Context, in *yaml.Node, extensionName string) (handled bool, response *any.Any, err error) {
if context == nil || context.ExtensionHandlers == nil {
return false, nil, nil
}
handled = false
for _, handler := range *(context.ExtensionHandlers) {
response, err = handler.handle(in, extensionName)
if response == nil {
continue
} else {
handled = true
break
}
}
return handled, response, err
}

func (extensionHandlers *ExtensionHandler) handle(in *yaml.Node, extensionName string) (*any.Any, error) {
if extensionHandlers.Name != "" {
yamlData, _ := yaml.Marshal(in)
request := &extensions.ExtensionHandlerRequest{
CompilerVersion: &extensions.Version{
Major: 0,
Minor: 1,
Patch: 0,
},
Wrapper: &extensions.Wrapper{
Version: "unknown", // TODO: set this to the type/version of spec being parsed.
Yaml: string(yamlData),
ExtensionName: extensionName,
},
}
requestBytes, _ := proto.Marshal(request)
cmd := exec.Command(extensionHandlers.Name)
cmd.Stdin = bytes.NewReader(requestBytes)
output, err := cmd.Output()
if err != nil {
return nil, err
}
response := &extensions.ExtensionHandlerResponse{}
err = proto.Unmarshal(output, response)
if err != nil || !response.Handled {
return nil, err
}
if len(response.Errors) != 0 {
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, ","))
}
return response.Value, nil
}
return nil, nil
}
var CallExtension = compiler.CallExtension
Loading
Loading