diff options
author | Amine Hilaly <hilalyamine@gmail.com> | 2019-04-09 21:45:24 +0200 |
---|---|---|
committer | Amine Hilaly <hilalyamine@gmail.com> | 2019-04-09 21:45:24 +0200 |
commit | 26b5343e2160de172969e6834074cf8482ceb845 (patch) | |
tree | 04f27aa660a903d65f7b3d951bd1c6f92c59c0c3 /vendor/github.com/99designs/gqlgen/codegen/args.go | |
parent | 6e8496f4c1767ca8a8b95716a04f1b492bef7397 (diff) | |
download | git-bug-26b5343e2160de172969e6834074cf8482ceb845.tar.gz |
Update Gopkg.*
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/codegen/args.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/codegen/args.go | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/vendor/github.com/99designs/gqlgen/codegen/args.go b/vendor/github.com/99designs/gqlgen/codegen/args.go new file mode 100644 index 00000000..d1498bdd --- /dev/null +++ b/vendor/github.com/99designs/gqlgen/codegen/args.go @@ -0,0 +1,104 @@ +package codegen + +import ( + "fmt" + "go/types" + "strings" + + "github.com/99designs/gqlgen/codegen/config" + "github.com/99designs/gqlgen/codegen/templates" + "github.com/pkg/errors" + "github.com/vektah/gqlparser/ast" +) + +type ArgSet struct { + Args []*FieldArgument + FuncDecl string +} + +type FieldArgument struct { + *ast.ArgumentDefinition + TypeReference *config.TypeReference + VarName string // The name of the var in go + Object *Object // A link back to the parent object + Default interface{} // The default value + Directives []*Directive + Value interface{} // value set in Data +} + +func (f *FieldArgument) Stream() bool { + return f.Object != nil && f.Object.Stream +} + +func (b *builder) buildArg(obj *Object, arg *ast.ArgumentDefinition) (*FieldArgument, error) { + tr, err := b.Binder.TypeReference(arg.Type, nil) + if err != nil { + return nil, err + } + + argDirs, err := b.getDirectives(arg.Directives) + if err != nil { + return nil, err + } + newArg := FieldArgument{ + ArgumentDefinition: arg, + TypeReference: tr, + Object: obj, + VarName: templates.ToGoPrivate(arg.Name), + Directives: argDirs, + } + + if arg.DefaultValue != nil { + newArg.Default, err = arg.DefaultValue.Value(nil) + if err != nil { + return nil, errors.Errorf("default value is not valid: %s", err.Error()) + } + } + + return &newArg, nil +} + +func (b *builder) bindArgs(field *Field, params *types.Tuple) error { + var newArgs []*FieldArgument + +nextArg: + for j := 0; j < params.Len(); j++ { + param := params.At(j) + for _, oldArg := range field.Args { + if strings.EqualFold(oldArg.Name, param.Name()) { + tr, err := b.Binder.TypeReference(oldArg.Type, param.Type()) + if err != nil { + return err + } + oldArg.TypeReference = tr + + newArgs = append(newArgs, oldArg) + continue nextArg + } + } + + // no matching arg found, abort + return fmt.Errorf("arg %s not in schema", param.Name()) + } + + field.Args = newArgs + return nil +} + +func (a *Data) Args() map[string][]*FieldArgument { + ret := map[string][]*FieldArgument{} + for _, o := range a.Objects { + for _, f := range o.Fields { + if len(f.Args) > 0 { + ret[f.ArgsFunc()] = f.Args + } + } + } + + for _, d := range a.Directives { + if len(d.Args) > 0 { + ret[d.ArgsFunc()] = d.Args + } + } + return ret +} |