1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
package codegen
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"syscall"
"github.com/pkg/errors"
"github.com/vektah/gqlgen/codegen/templates"
"github.com/vektah/gqlgen/neelance/schema"
"golang.org/x/tools/imports"
)
func Generate(cfg Config) error {
if err := cfg.normalize(); err != nil {
return err
}
_ = syscall.Unlink(cfg.Exec.Filename)
_ = syscall.Unlink(cfg.Model.Filename)
modelsBuild, err := cfg.models()
if err != nil {
return errors.Wrap(err, "model plan failed")
}
if len(modelsBuild.Models) > 0 || len(modelsBuild.Enums) > 0 {
var buf *bytes.Buffer
buf, err = templates.Run("models.gotpl", modelsBuild)
if err != nil {
return errors.Wrap(err, "model generation failed")
}
if err = write(cfg.Model.Filename, buf.Bytes()); err != nil {
return err
}
for _, model := range modelsBuild.Models {
modelCfg := cfg.Models[model.GQLType]
modelCfg.Model = cfg.Model.ImportPath() + "." + model.GoType
cfg.Models[model.GQLType] = modelCfg
}
for _, enum := range modelsBuild.Enums {
modelCfg := cfg.Models[enum.GQLType]
modelCfg.Model = cfg.Model.ImportPath() + "." + enum.GoType
cfg.Models[enum.GQLType] = modelCfg
}
}
build, err := cfg.bind()
if err != nil {
return errors.Wrap(err, "exec plan failed")
}
var buf *bytes.Buffer
buf, err = templates.Run("generated.gotpl", build)
if err != nil {
return errors.Wrap(err, "exec codegen failed")
}
if err = write(cfg.Exec.Filename, buf.Bytes()); err != nil {
return err
}
if err = cfg.validate(); err != nil {
return errors.Wrap(err, "validation failed")
}
return nil
}
func (cfg *Config) normalize() error {
if err := cfg.Model.normalize(); err != nil {
return errors.Wrap(err, "model")
}
if err := cfg.Exec.normalize(); err != nil {
return errors.Wrap(err, "exec")
}
builtins := TypeMap{
"__Directive": {Model: "github.com/vektah/gqlgen/neelance/introspection.Directive"},
"__Type": {Model: "github.com/vektah/gqlgen/neelance/introspection.Type"},
"__Field": {Model: "github.com/vektah/gqlgen/neelance/introspection.Field"},
"__EnumValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.EnumValue"},
"__InputValue": {Model: "github.com/vektah/gqlgen/neelance/introspection.InputValue"},
"__Schema": {Model: "github.com/vektah/gqlgen/neelance/introspection.Schema"},
"Int": {Model: "github.com/vektah/gqlgen/graphql.Int"},
"Float": {Model: "github.com/vektah/gqlgen/graphql.Float"},
"String": {Model: "github.com/vektah/gqlgen/graphql.String"},
"Boolean": {Model: "github.com/vektah/gqlgen/graphql.Boolean"},
"ID": {Model: "github.com/vektah/gqlgen/graphql.ID"},
"Time": {Model: "github.com/vektah/gqlgen/graphql.Time"},
"Map": {Model: "github.com/vektah/gqlgen/graphql.Map"},
}
if cfg.Models == nil {
cfg.Models = TypeMap{}
}
for typeName, entry := range builtins {
if !cfg.Models.Exists(typeName) {
cfg.Models[typeName] = entry
}
}
cfg.schema = schema.New()
return cfg.schema.Parse(cfg.SchemaStr)
}
var invalidPackageNameChar = regexp.MustCompile(`[^\w]`)
func sanitizePackageName(pkg string) string {
return invalidPackageNameChar.ReplaceAllLiteralString(filepath.Base(pkg), "_")
}
func abs(path string) string {
absPath, err := filepath.Abs(path)
if err != nil {
panic(err)
}
return filepath.ToSlash(absPath)
}
func gofmt(filename string, b []byte) ([]byte, error) {
out, err := imports.Process(filename, b, nil)
if err != nil {
return b, errors.Wrap(err, "unable to gofmt")
}
return out, nil
}
func write(filename string, b []byte) error {
err := os.MkdirAll(filepath.Dir(filename), 0755)
if err != nil {
return errors.Wrap(err, "failed to create directory")
}
formatted, err := gofmt(filename, b)
if err != nil {
fmt.Fprintf(os.Stderr, "gofmt failed: %s\n", err.Error())
formatted = b
}
err = ioutil.WriteFile(filename, formatted, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write %s", filename)
}
return nil
}
|