Skip to content

Commit

Permalink
Add line numbers to error msgs + improve errors for schema mismatches (
Browse files Browse the repository at this point in the history
  • Loading branch information
timburks authored Oct 12, 2020
1 parent 349b567 commit 52a6193
Show file tree
Hide file tree
Showing 14 changed files with 455 additions and 372 deletions.
18 changes: 12 additions & 6 deletions compiler/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,36 @@

package compiler

import (
yaml "gopkg.in/yaml.v3"
)

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

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

// NewContext returns a new object representing the compiler state
func NewContext(name string, parent *Context) *Context {
func NewContext(name string, node *yaml.Node, parent *Context) *Context {
if parent != nil {
return &Context{Name: name, Parent: parent, ExtensionHandlers: parent.ExtensionHandlers}
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 {
return context.Parent.Description() + "." + context.Name
name = context.Parent.Description() + "." + name
}
return context.Name
return name
}
13 changes: 11 additions & 2 deletions compiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package compiler

import "fmt"

// Error represents compiler errors and their location in the document.
type Error struct {
Context *Context
Expand All @@ -25,12 +27,19 @@ 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 "ERROR " + err.Message
return err.Message
}
return "ERROR " + err.Context.Description() + " " + err.Message
return err.locationDescription() + " " + err.Message
}

// ErrorGroup is a container for groups of Error values.
Expand Down
70 changes: 35 additions & 35 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func NewAuth(in *yaml.Node, context *compiler.Context) (*Auth, error) {
v1 := compiler.MapValueForKey(m, "oauth2")
if v1 != nil {
var err error
x.Oauth2, err = NewOauth2(v1, compiler.NewContext("oauth2", context))
x.Oauth2, err = NewOauth2(v1, compiler.NewContext("oauth2", v1, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v9 := compiler.MapValueForKey(m, "icons")
if v9 != nil {
var err error
x.Icons, err = NewIcons(v9, compiler.NewContext("icons", context))
x.Icons, err = NewIcons(v9, compiler.NewContext("icons", v9, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v18 := compiler.MapValueForKey(m, "parameters")
if v18 != nil {
var err error
x.Parameters, err = NewParameters(v18, compiler.NewContext("parameters", context))
x.Parameters, err = NewParameters(v18, compiler.NewContext("parameters", v18, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -288,7 +288,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v19 := compiler.MapValueForKey(m, "auth")
if v19 != nil {
var err error
x.Auth, err = NewAuth(v19, compiler.NewContext("auth", context))
x.Auth, err = NewAuth(v19, compiler.NewContext("auth", v19, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -308,7 +308,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v21 := compiler.MapValueForKey(m, "schemas")
if v21 != nil {
var err error
x.Schemas, err = NewSchemas(v21, compiler.NewContext("schemas", context))
x.Schemas, err = NewSchemas(v21, compiler.NewContext("schemas", v21, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -317,7 +317,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v22 := compiler.MapValueForKey(m, "methods")
if v22 != nil {
var err error
x.Methods, err = NewMethods(v22, compiler.NewContext("methods", context))
x.Methods, err = NewMethods(v22, compiler.NewContext("methods", v22, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -326,7 +326,7 @@ func NewDocument(in *yaml.Node, context *compiler.Context) (*Document, error) {
v23 := compiler.MapValueForKey(m, "resources")
if v23 != nil {
var err error
x.Resources, err = NewResources(v23, compiler.NewContext("resources", context))
x.Resources, err = NewResources(v23, compiler.NewContext("resources", v23, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -491,7 +491,7 @@ func NewMediaUpload(in *yaml.Node, context *compiler.Context) (*MediaUpload, err
v3 := compiler.MapValueForKey(m, "protocols")
if v3 != nil {
var err error
x.Protocols, err = NewProtocols(v3, compiler.NewContext("protocols", context))
x.Protocols, err = NewProtocols(v3, compiler.NewContext("protocols", v3, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -565,7 +565,7 @@ func NewMethod(in *yaml.Node, context *compiler.Context) (*Method, error) {
v5 := compiler.MapValueForKey(m, "parameters")
if v5 != nil {
var err error
x.Parameters, err = NewParameters(v5, compiler.NewContext("parameters", context))
x.Parameters, err = NewParameters(v5, compiler.NewContext("parameters", v5, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -585,7 +585,7 @@ func NewMethod(in *yaml.Node, context *compiler.Context) (*Method, error) {
v7 := compiler.MapValueForKey(m, "request")
if v7 != nil {
var err error
x.Request, err = NewRequest(v7, compiler.NewContext("request", context))
x.Request, err = NewRequest(v7, compiler.NewContext("request", v7, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -594,7 +594,7 @@ func NewMethod(in *yaml.Node, context *compiler.Context) (*Method, error) {
v8 := compiler.MapValueForKey(m, "response")
if v8 != nil {
var err error
x.Response, err = NewResponse(v8, compiler.NewContext("response", context))
x.Response, err = NewResponse(v8, compiler.NewContext("response", v8, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -641,7 +641,7 @@ func NewMethod(in *yaml.Node, context *compiler.Context) (*Method, error) {
v13 := compiler.MapValueForKey(m, "mediaUpload")
if v13 != nil {
var err error
x.MediaUpload, err = NewMediaUpload(v13, compiler.NewContext("mediaUpload", context))
x.MediaUpload, err = NewMediaUpload(v13, compiler.NewContext("mediaUpload", v13, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -696,7 +696,7 @@ func NewMethods(in *yaml.Node, context *compiler.Context) (*Methods, error) {
pair := &NamedMethod{}
pair.Name = k
var err error
pair.Value, err = NewMethod(v, compiler.NewContext(k, context))
pair.Value, err = NewMethod(v, compiler.NewContext(k, v, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -736,7 +736,7 @@ func NewNamedMethod(in *yaml.Node, context *compiler.Context) (*NamedMethod, err
v2 := compiler.MapValueForKey(m, "value")
if v2 != nil {
var err error
x.Value, err = NewMethod(v2, compiler.NewContext("value", context))
x.Value, err = NewMethod(v2, compiler.NewContext("value", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -774,7 +774,7 @@ func NewNamedParameter(in *yaml.Node, context *compiler.Context) (*NamedParamete
v2 := compiler.MapValueForKey(m, "value")
if v2 != nil {
var err error
x.Value, err = NewParameter(v2, compiler.NewContext("value", context))
x.Value, err = NewParameter(v2, compiler.NewContext("value", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -812,7 +812,7 @@ func NewNamedResource(in *yaml.Node, context *compiler.Context) (*NamedResource,
v2 := compiler.MapValueForKey(m, "value")
if v2 != nil {
var err error
x.Value, err = NewResource(v2, compiler.NewContext("value", context))
x.Value, err = NewResource(v2, compiler.NewContext("value", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -850,7 +850,7 @@ func NewNamedSchema(in *yaml.Node, context *compiler.Context) (*NamedSchema, err
v2 := compiler.MapValueForKey(m, "value")
if v2 != nil {
var err error
x.Value, err = NewSchema(v2, compiler.NewContext("value", context))
x.Value, err = NewSchema(v2, compiler.NewContext("value", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -888,7 +888,7 @@ func NewNamedScope(in *yaml.Node, context *compiler.Context) (*NamedScope, error
v2 := compiler.MapValueForKey(m, "value")
if v2 != nil {
var err error
x.Value, err = NewScope(v2, compiler.NewContext("value", context))
x.Value, err = NewScope(v2, compiler.NewContext("value", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -917,7 +917,7 @@ func NewOauth2(in *yaml.Node, context *compiler.Context) (*Oauth2, error) {
v1 := compiler.MapValueForKey(m, "scopes")
if v1 != nil {
var err error
x.Scopes, err = NewScopes(v1, compiler.NewContext("scopes", context))
x.Scopes, err = NewScopes(v1, compiler.NewContext("scopes", v1, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1076,7 +1076,7 @@ func NewParameter(in *yaml.Node, context *compiler.Context) (*Parameter, error)
v15 := compiler.MapValueForKey(m, "properties")
if v15 != nil {
var err error
x.Properties, err = NewSchemas(v15, compiler.NewContext("properties", context))
x.Properties, err = NewSchemas(v15, compiler.NewContext("properties", v15, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1085,7 +1085,7 @@ func NewParameter(in *yaml.Node, context *compiler.Context) (*Parameter, error)
v16 := compiler.MapValueForKey(m, "additionalProperties")
if v16 != nil {
var err error
x.AdditionalProperties, err = NewSchema(v16, compiler.NewContext("additionalProperties", context))
x.AdditionalProperties, err = NewSchema(v16, compiler.NewContext("additionalProperties", v16, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1094,7 +1094,7 @@ func NewParameter(in *yaml.Node, context *compiler.Context) (*Parameter, error)
v17 := compiler.MapValueForKey(m, "items")
if v17 != nil {
var err error
x.Items, err = NewSchema(v17, compiler.NewContext("items", context))
x.Items, err = NewSchema(v17, compiler.NewContext("items", v17, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1103,7 +1103,7 @@ func NewParameter(in *yaml.Node, context *compiler.Context) (*Parameter, error)
v18 := compiler.MapValueForKey(m, "annotations")
if v18 != nil {
var err error
x.Annotations, err = NewAnnotations(v18, compiler.NewContext("annotations", context))
x.Annotations, err = NewAnnotations(v18, compiler.NewContext("annotations", v18, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1131,7 +1131,7 @@ func NewParameters(in *yaml.Node, context *compiler.Context) (*Parameters, error
pair := &NamedParameter{}
pair.Name = k
var err error
pair.Value, err = NewParameter(v, compiler.NewContext(k, context))
pair.Value, err = NewParameter(v, compiler.NewContext(k, v, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1162,7 +1162,7 @@ func NewProtocols(in *yaml.Node, context *compiler.Context) (*Protocols, error)
v1 := compiler.MapValueForKey(m, "simple")
if v1 != nil {
var err error
x.Simple, err = NewSimple(v1, compiler.NewContext("simple", context))
x.Simple, err = NewSimple(v1, compiler.NewContext("simple", v1, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1171,7 +1171,7 @@ func NewProtocols(in *yaml.Node, context *compiler.Context) (*Protocols, error)
v2 := compiler.MapValueForKey(m, "resumable")
if v2 != nil {
var err error
x.Resumable, err = NewResumable(v2, compiler.NewContext("resumable", context))
x.Resumable, err = NewResumable(v2, compiler.NewContext("resumable", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ func NewResource(in *yaml.Node, context *compiler.Context) (*Resource, error) {
v1 := compiler.MapValueForKey(m, "methods")
if v1 != nil {
var err error
x.Methods, err = NewMethods(v1, compiler.NewContext("methods", context))
x.Methods, err = NewMethods(v1, compiler.NewContext("methods", v1, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1247,7 +1247,7 @@ func NewResource(in *yaml.Node, context *compiler.Context) (*Resource, error) {
v2 := compiler.MapValueForKey(m, "resources")
if v2 != nil {
var err error
x.Resources, err = NewResources(v2, compiler.NewContext("resources", context))
x.Resources, err = NewResources(v2, compiler.NewContext("resources", v2, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1275,7 +1275,7 @@ func NewResources(in *yaml.Node, context *compiler.Context) (*Resources, error)
pair := &NamedResource{}
pair.Name = k
var err error
pair.Value, err = NewResource(v, compiler.NewContext(k, context))
pair.Value, err = NewResource(v, compiler.NewContext(k, v, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1494,7 +1494,7 @@ func NewSchema(in *yaml.Node, context *compiler.Context) (*Schema, error) {
v14 := compiler.MapValueForKey(m, "properties")
if v14 != nil {
var err error
x.Properties, err = NewSchemas(v14, compiler.NewContext("properties", context))
x.Properties, err = NewSchemas(v14, compiler.NewContext("properties", v14, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1503,7 +1503,7 @@ func NewSchema(in *yaml.Node, context *compiler.Context) (*Schema, error) {
v15 := compiler.MapValueForKey(m, "additionalProperties")
if v15 != nil {
var err error
x.AdditionalProperties, err = NewSchema(v15, compiler.NewContext("additionalProperties", context))
x.AdditionalProperties, err = NewSchema(v15, compiler.NewContext("additionalProperties", v15, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1512,7 +1512,7 @@ func NewSchema(in *yaml.Node, context *compiler.Context) (*Schema, error) {
v16 := compiler.MapValueForKey(m, "items")
if v16 != nil {
var err error
x.Items, err = NewSchema(v16, compiler.NewContext("items", context))
x.Items, err = NewSchema(v16, compiler.NewContext("items", v16, context))
if err != nil {
errors = append(errors, err)
}
Expand All @@ -1530,7 +1530,7 @@ func NewSchema(in *yaml.Node, context *compiler.Context) (*Schema, error) {
v18 := compiler.MapValueForKey(m, "annotations")
if v18 != nil {
var err error
x.Annotations, err = NewAnnotations(v18, compiler.NewContext("annotations", context))
x.Annotations, err = NewAnnotations(v18, compiler.NewContext("annotations", v18, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1567,7 +1567,7 @@ func NewSchemas(in *yaml.Node, context *compiler.Context) (*Schemas, error) {
pair := &NamedSchema{}
pair.Name = k
var err error
pair.Value, err = NewSchema(v, compiler.NewContext(k, context))
pair.Value, err = NewSchema(v, compiler.NewContext(k, v, context))
if err != nil {
errors = append(errors, err)
}
Expand Down Expand Up @@ -1626,7 +1626,7 @@ func NewScopes(in *yaml.Node, context *compiler.Context) (*Scopes, error) {
pair := &NamedScope{}
pair.Name = k
var err error
pair.Value, err = NewScope(v, compiler.NewContext(k, context))
pair.Value, err = NewScope(v, compiler.NewContext(k, v, context))
if err != nil {
errors = append(errors, err)
}
Expand Down
3 changes: 2 additions & 1 deletion discovery/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func ParseDocument(b []byte) (*Document, error) {
if err != nil {
return nil, err
}
return NewDocument(info.Content[0], compiler.NewContext("$root", nil))
root := info.Content[0]
return NewDocument(root, compiler.NewContext("$root", root, nil))
}
Loading

0 comments on commit 52a6193

Please sign in to comment.