diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-14 14:48:41 +0200 |
commit | 5c568a362b73cf163b06bc7371982f9a7ceaaf29 (patch) | |
tree | 755b61f969913b5290f3d56f4cfba4070b8155bc /vendor/github.com/vektah/gqlgen/codegen/models_build.go | |
parent | ef0d8fa108fbdef24997a84a3c6ecbf21cbc0a9e (diff) | |
download | git-bug-5c568a362b73cf163b06bc7371982f9a7ceaaf29.tar.gz |
gqlgen: add a small program to go:generate the code
Diffstat (limited to 'vendor/github.com/vektah/gqlgen/codegen/models_build.go')
-rw-r--r-- | vendor/github.com/vektah/gqlgen/codegen/models_build.go | 91 |
1 files changed, 91 insertions, 0 deletions
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 +} |