From 5c568a362b73cf163b06bc7371982f9a7ceaaf29 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Tue, 14 Aug 2018 14:48:41 +0200 Subject: gqlgen: add a small program to go:generate the code --- .../vektah/gqlgen/codegen/models_build.go | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 vendor/github.com/vektah/gqlgen/codegen/models_build.go (limited to 'vendor/github.com/vektah/gqlgen/codegen/models_build.go') diff --git a/vendor/github.com/vektah/gqlgen/codegen/models_build.go b/vendor/github.com/vektah/gqlgen/codegen/models_build.go new file mode 100644 index 00000000..211d4bd4 --- /dev/null +++ b/vendor/github.com/vektah/gqlgen/codegen/models_build.go @@ -0,0 +1,91 @@ +package codegen + +import ( + "sort" + "strings" + + "github.com/vektah/gqlgen/neelance/schema" + "golang.org/x/tools/go/loader" +) + +func (cfg *Config) buildModels(types NamedTypes, prog *loader.Program) ([]Model, error) { + var models []Model + + for _, typ := range cfg.schema.Types { + var model Model + switch typ := typ.(type) { + case *schema.Object: + obj, err := cfg.buildObject(types, typ) + if err != nil { + return nil, err + } + if obj.Root || obj.IsUserDefined { + continue + } + model = cfg.obj2Model(obj) + case *schema.InputObject: + obj, err := buildInput(types, typ) + if err != nil { + return nil, err + } + if obj.IsUserDefined { + continue + } + model = cfg.obj2Model(obj) + case *schema.Interface, *schema.Union: + intf := cfg.buildInterface(types, typ, prog) + if intf.IsUserDefined { + continue + } + model = int2Model(intf) + default: + continue + } + + models = append(models, model) + } + + sort.Slice(models, func(i, j int) bool { + return strings.Compare(models[i].GQLType, models[j].GQLType) == -1 + }) + + return models, nil +} + +func (cfg *Config) obj2Model(obj *Object) Model { + model := Model{ + NamedType: obj.NamedType, + Fields: []ModelField{}, + } + + model.GoType = ucFirst(obj.GQLType) + model.Marshaler = &Ref{GoType: obj.GoType} + + for i := range obj.Fields { + field := &obj.Fields[i] + mf := ModelField{Type: field.Type, GQLName: field.GQLName} + + mf.GoVarName = ucFirst(field.GQLName) + if mf.IsScalar { + if mf.GoVarName == "Id" { + mf.GoVarName = "ID" + } + } + + model.Fields = append(model.Fields, mf) + } + + return model +} + +func int2Model(obj *Interface) Model { + model := Model{ + NamedType: obj.NamedType, + Fields: []ModelField{}, + } + + model.GoType = ucFirst(obj.GQLType) + model.Marshaler = &Ref{GoType: obj.GoType} + + return model +} -- cgit