Skip to content

Commit

Permalink
Add WrapIfUnwrapped and other Lint fixes (#275)
Browse files Browse the repository at this point in the history
* Add WrapIfUnwrapped

Signed-off-by: Steve Coffman <steve@khanacademy.org>

* Lint fixes

Signed-off-by: Steve Coffman <steve@khanacademy.org>

---------

Signed-off-by: Steve Coffman <steve@khanacademy.org>
  • Loading branch information
StevenACoffman committed Sep 7, 2023
1 parent 2159f67 commit 450a0ee
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 49 deletions.
4 changes: 2 additions & 2 deletions ast/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type PathElement interface {
var _ PathElement = PathIndex(0)
var _ PathElement = PathName("")

func (path Path) String() string {
func (path *Path) String() string {

This comment has been minimized.

Copy link
@fredzqm

fredzqm Sep 7, 2023

Contributor

This is actually breaking~

path := ast.Path{ast.PathName("xxx")}
fmt.Sprintf("%v", path)
// was "xxx"
// now is "[xxx]"

This comment has been minimized.

Copy link
@StevenACoffman

StevenACoffman Sep 7, 2023

Author Collaborator

Bummer. I hate having mixed recievers for both *Path and Path. I reverted this. Thanks for catching it!

var str bytes.Buffer
for i, v := range path {
for i, v := range *path {
switch v := v.(type) {
case PathIndex:
str.WriteString(fmt.Sprintf("[%d]", v))
Expand Down
8 changes: 4 additions & 4 deletions ast/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func (*Field) isSelection() {}
func (*FragmentSpread) isSelection() {}
func (*InlineFragment) isSelection() {}

func (s *Field) GetPosition() *Position { return s.Position }
func (f *Field) GetPosition() *Position { return f.Position }
func (s *FragmentSpread) GetPosition() *Position { return s.Position }
func (s *InlineFragment) GetPosition() *Position { return s.Position }
func (f *InlineFragment) GetPosition() *Position { return f.Position }

type Field struct {
Alias string
Expand All @@ -36,6 +36,6 @@ type Argument struct {
Comment *CommentGroup
}

func (s *Field) ArgumentMap(vars map[string]interface{}) map[string]interface{} {
return arg2map(s.Definition.Arguments, s.Arguments, vars)
func (f *Field) ArgumentMap(vars map[string]interface{}) map[string]interface{} {
return arg2map(f.Definition.Arguments, f.Arguments, vars)
}
9 changes: 7 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module github.com/vektah/gqlparser/v2

go 1.16
go 1.18

require (
github.com/agnivade/levenshtein v1.1.1
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/sergi/go-diff v1.3.1 // indirect
github.com/stretchr/testify v1.4.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
)
9 changes: 2 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
github.com/agnivade/levenshtein v1.0.1 h1:3oJU7J3FGFmyhn8KHjmVaZCN5hxTr7GxgRue+sxIXdQ=
github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM=
github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8=
github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q=
github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g=
github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -16,8 +16,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -27,8 +25,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
19 changes: 16 additions & 3 deletions gqlerror/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func (err *Error) Error() string {
return res.String()
}

func (err Error) pathString() string {
func (err *Error) pathString() string {
return err.Path.String()
}

func (err Error) Unwrap() error {
func (err *Error) Unwrap() error {
return err.err
}

Expand All @@ -92,7 +92,7 @@ func (errs List) Is(target error) bool {

func (errs List) As(target interface{}) bool {
for _, err := range errs {
if errors.As(err, target) {
if errors.As(err, &target) {
return true
}
}
Expand Down Expand Up @@ -120,6 +120,19 @@ func Wrap(err error) *Error {
}
}

func WrapIfUnwrapped(err error) *Error {
if err == nil {
return nil
}
if gqlErr, ok := err.(*Error); ok {
return gqlErr
}
return &Error{
err: err,
Message: err.Error(),
}
}

func Errorf(message string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(message, args...),
Expand Down
8 changes: 4 additions & 4 deletions gqlparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
)

func LoadSchema(str ...*ast.Source) (*ast.Schema, error) {
ast, err := validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
schema, err := validator.LoadSchema(append([]*ast.Source{validator.Prelude}, str...)...)
gqlErr, ok := err.(*gqlerror.Error)
if ok {
return ast, gqlErr
return schema, gqlErr
}
if err != nil {
return ast, gqlerror.Wrap(err)
return schema, gqlerror.Wrap(err)
}
return ast, nil
return schema, nil
}

func MustLoadSchema(str ...*ast.Source) *ast.Schema {
Expand Down
14 changes: 7 additions & 7 deletions parser/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ func ParseSchema(source *Source) (*SchemaDocument, error) {
p := parser{
lexer: lexer.New(source),
}
ast, err := p.parseSchemaDocument(), p.err
sd, err := p.parseSchemaDocument(), p.err
if err != nil {
return nil, err
}

for _, def := range ast.Definitions {
for _, def := range sd.Definitions {
def.BuiltIn = source.BuiltIn
}
for _, def := range ast.Extensions {
for _, def := range sd.Extensions {
def.BuiltIn = source.BuiltIn
}

return ast, nil
return sd, nil
}

func ParseSchemas(inputs ...*Source) (*SchemaDocument, error) {
ast := &SchemaDocument{}
sd := &SchemaDocument{}
for _, input := range inputs {
inputAst, err := ParseSchema(input)
if err != nil {
return nil, err
}
ast.Merge(inputAst)
sd.Merge(inputAst)
}
return ast, nil
return sd, nil
}

func (p *parser) parseSchemaDocument() *SchemaDocument {
Expand Down
6 changes: 3 additions & 3 deletions parser/testrunner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ func Test(t *testing.T, filename string, f func(t *testing.T, input string) Spec
result.AST = strings.TrimSpace(result.AST)

if spec.AST != "" && spec.AST != result.AST {
diff := diff.LineDiff(spec.AST, result.AST)
if diff != "" {
t.Errorf("AST mismatch:\n%s", diff)
diffStr := diff.LineDiff(spec.AST, result.AST)
if diffStr != "" {
t.Errorf("AST mismatch:\n%s", diffStr)
}
}

Expand Down
35 changes: 18 additions & 17 deletions validator/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,32 @@ import (
)

func LoadSchema(inputs ...*Source) (*Schema, error) {
ast, err := parser.ParseSchemas(inputs...)
sd, err := parser.ParseSchemas(inputs...)

if err != nil {
return nil, err
return nil, gqlerror.WrapIfUnwrapped(err)
}
return ValidateSchemaDocument(ast)
return ValidateSchemaDocument(sd)
}

func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
func ValidateSchemaDocument(sd *SchemaDocument) (*Schema, error) {
schema := Schema{
Types: map[string]*Definition{},
Directives: map[string]*DirectiveDefinition{},
PossibleTypes: map[string][]*Definition{},
Implements: map[string][]*Definition{},
}

for i, def := range ast.Definitions {
for i, def := range sd.Definitions {
if schema.Types[def.Name] != nil {
return nil, gqlerror.ErrorPosf(def.Position, "Cannot redeclare type %s.", def.Name)
}
schema.Types[def.Name] = ast.Definitions[i]
schema.Types[def.Name] = sd.Definitions[i]
}

defs := append(DefinitionList{}, ast.Definitions...)
defs := append(DefinitionList{}, sd.Definitions...)

for _, ext := range ast.Extensions {
for _, ext := range sd.Extensions {
def := schema.Types[ext.Name]
if def == nil {
schema.Types[ext.Name] = &Definition{
Expand Down Expand Up @@ -80,7 +81,7 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
}
}

for i, dir := range ast.Directives {
for i, dir := range sd.Directives {
if schema.Directives[dir.Name] != nil {
// While the spec says SDL must not (§3.5) explicitly define builtin
// scalars, it may (§3.13) define builtin directives. Here we check for
Expand All @@ -99,16 +100,16 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
return nil, gqlerror.ErrorPosf(dir.Position, "Cannot redeclare directive %s.", dir.Name)
}
}
schema.Directives[dir.Name] = ast.Directives[i]
schema.Directives[dir.Name] = sd.Directives[i]
}

if len(ast.Schema) > 1 {
return nil, gqlerror.ErrorPosf(ast.Schema[1].Position, "Cannot have multiple schema entry points, consider schema extensions instead.")
if len(sd.Schema) > 1 {
return nil, gqlerror.ErrorPosf(sd.Schema[1].Position, "Cannot have multiple schema entry points, consider schema extensions instead.")
}

if len(ast.Schema) == 1 {
schema.Description = ast.Schema[0].Description
for _, entrypoint := range ast.Schema[0].OperationTypes {
if len(sd.Schema) == 1 {
schema.Description = sd.Schema[0].Description
for _, entrypoint := range sd.Schema[0].OperationTypes {
def := schema.Types[entrypoint.Type]
if def == nil {
return nil, gqlerror.ErrorPosf(entrypoint.Position, "Schema root %s refers to a type %s that does not exist.", entrypoint.Operation, entrypoint.Type)
Expand All @@ -124,7 +125,7 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
}
}

for _, ext := range ast.SchemaExtension {
for _, ext := range sd.SchemaExtension {
for _, entrypoint := range ext.OperationTypes {
def := schema.Types[entrypoint.Type]
if def == nil {
Expand Down Expand Up @@ -152,7 +153,7 @@ func ValidateSchemaDocument(ast *SchemaDocument) (*Schema, error) {
// Inferred root operation type names should be performed only when a `schema` directive is
// **not** provided, when it is, `Mutation` and `Subscription` becomes valid types and are not
// assigned as a root operation on the schema.
if len(ast.Schema) == 0 {
if len(sd.Schema) == 0 {
if schema.Query == nil && schema.Types["Query"] != nil {
schema.Query = schema.Types["Query"]
}
Expand Down

0 comments on commit 450a0ee

Please sign in to comment.