Skip to content

Commit

Permalink
Refactored template generation to use embedded binary assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
shakahl committed Oct 24, 2020
1 parent f50ffba commit 2b88712
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 32 deletions.
6 changes: 3 additions & 3 deletions assets/templates/graphql_input_objects.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ ReverseTypeMapInputObjects = map[string]string{

{{- define "inputObject" -}}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }}
type {{.name | identifier }} struct {{- "{" }}
type {{.name | identifier }} struct {
{{- range .inputFields -}}
{{- if eq .type.kind "NON_NULL" }}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }} (Required.)
{{ .name | identifier }} {{ .type | type }} ` + "`" + `json:"{{ .name }}" graphql:"!{{ .name }}"` + "`" + `
{{ .name | identifier }} {{ .type | type }} `json:"{{ .name }}" graphql:"!{{ .name }}"`
{{- end -}}
{{- if ne .type.kind "NON_NULL" }}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }} (Optional.)
{{ .name | identifier }} {{ .type | type }} ` + "`" + `json:"{{ .name }},omitempty" graphql:"{{ .name }}"` + "`" + `
{{ .name | identifier }} {{ .type | type }} `json:"{{ .name }},omitempty" graphql:"{{ .name }}"`
{{- end -}}
{{- end -}}
}
Expand Down
10 changes: 5 additions & 5 deletions assets/templates/graphql_objects.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
type Object interface{}

type GQLMeta struct {
// GQLTypeName @graphql="__typename" Is a meta field containing the GraphQL type name
GQLTypeName *string ` + "`" + `json:"__typename,omitempty"` + "`" + `
// GQLTypeName @graphql="__typename" Is a meta field containing the GraphQL type name
GQLTypeName *string `json:"__typename,omitempty"`
}

var (
Expand All @@ -39,16 +39,16 @@ ReverseTypeMapObjects = map[string]string{

{{- define "object" -}}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }}
type {{.name | identifier }} struct {{- "{" }}
type {{.name | identifier }} struct {
GQLMeta
{{- range .fields -}}
{{- if eq .type.kind "NON_NULL" }}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }} (Required.)
{{ .name | identifier }} {{ .type | type }} ` + "`" + `json:"{{ .name }}" graphql:"!{{ .name }}"` + "`" + `
{{ .name | identifier }} {{ .type | type }} `json:"{{ .name }}" graphql:"!{{ .name }}"`
{{- end -}}
{{- if ne .type.kind "NON_NULL" }}
// {{ .name | identifier }} @graphql="{{ .name }}" {{ if .description }}{{ .description | clean | formatDescription }}{{ end }} (Optional.)
{{ .name | identifier }} {{ .type | type }} ` + "`" + `json:"{{ .name }},omitempty" graphql:"{{ .name }}"` + "`" + `
{{ .name | identifier }} {{ .type | type }} `json:"{{ .name }},omitempty" graphql:"{{ .name }}"`
{{- end -}}
{{- end -}}
}
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl
cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I=
cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
Expand Down Expand Up @@ -41,6 +42,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
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/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -75,6 +77,7 @@ github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
Expand Down Expand Up @@ -113,6 +116,7 @@ github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqx
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
Expand All @@ -121,8 +125,10 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
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/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
Expand Down Expand Up @@ -155,6 +161,7 @@ github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/9
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
Expand All @@ -177,7 +184,9 @@ github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXk
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand All @@ -197,6 +206,7 @@ github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
Expand Down Expand Up @@ -320,6 +330,7 @@ google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ij
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type (
Debug bool `json:"debug" envconfig:"debug" default:"false"`
OutputDirectory string `json:"output_directory" envconfig:"output_directory" default:"internal/graphqltypes" split_words:"true"`
OutputPackage string `json:"output_package" envconfig:"output_package" required:"true" split_words:"true" default:"graphqltypes"`
GraphQLEndpoint string `json:"graphql_endpoint" envconfig:"graphql_endpoint" required:"true" split_words:"true"`
GraphQLEndpoint string `json:"graphql_endpoint" envconfig:"graphql_endpoint" required:"true" split_words:"false"`
GraphQLAuthHeader string `json:"graphql_auth_header" envconfig:"graphql_auth_header" required:"true" split_words:"true" default:"Authorization"`
GraphQLAuthToken string `json:"graphql_auth_token" envconfig:"graphql_auth_token" split_words:"true"`
}
Expand Down
43 changes: 28 additions & 15 deletions internal/gqlassist/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"text/template"

"github.com/shakahl/gqlassist/internal/apiclient"
"github.com/shakahl/gqlassist/internal/statikdata"
"github.com/shakahl/gqlassist/internal/utils"
)

Expand All @@ -29,9 +30,10 @@ type GraphQLTypeDefGeneratorConfig struct {
}

type GraphQLTypeDefGenerator struct {
options GraphQLTypeDefGeneratorConfig
client *apiclient.ApiClient
logger *log.Logger
options GraphQLTypeDefGeneratorConfig
client *apiclient.ApiClient
logger *log.Logger
templates map[string]*template.Template
}

func New(options GraphQLTypeDefGeneratorConfig, logger *log.Logger) *GraphQLTypeDefGenerator {
Expand All @@ -46,15 +48,26 @@ func New(options GraphQLTypeDefGeneratorConfig, logger *log.Logger) *GraphQLType
}

// getTemplates returns a template map (filename->template)
func (g *GraphQLTypeDefGenerator) createTemplates(params map[string]interface{}) map[string]*template.Template {
// Filename -> Template.
var templates = map[string]*template.Template{
"gen_graphql_scalars.go": renderGeneratorTemplate("graphql_scalars.gotmpl", GeneratorTemplateScalar),
"gen_graphql_enums.go": renderGeneratorTemplate("graphql_enums.gotmpl", GeneratorTemplateEnum),
"gen_graphql_input_objects.go": renderGeneratorTemplate("graphql_input_objects.gotmpl", GeneratorTemplateInputObjects),
"gen_graphql_objects.go": renderGeneratorTemplate("graphql_objects.gotmpl", GeneratorTemplateObjects),
}
return templates
func (g *GraphQLTypeDefGenerator) GetTemplates(params map[string]interface{}) map[string]*template.Template {
if g.templates != nil {
return g.templates
}

statikTemplates := map[string]string{
"gen_graphql_enums.go": "/graphql_enums.gotmpl",
"gen_graphql_scalars.go": "/graphql_scalars.gotmpl",
"gen_graphql_input_objects.go": "/graphql_input_objects.gotmpl",
"gen_graphql_objects.go": "/graphql_objects.gotmpl",
}

g.templates = make(map[string]*template.Template)

for name, path := range statikTemplates {
content := statikdata.MustReadFileString(path)
g.templates[name] = renderGeneratorTemplate(name, content)
}

return g.templates
}

func (g *GraphQLTypeDefGenerator) getWorkingDirectory() string {
Expand Down Expand Up @@ -95,8 +108,8 @@ func (g *GraphQLTypeDefGenerator) fetchGraphQLSchema() (string, error) {
return string(result.GetBody()), nil
}

func (g *GraphQLTypeDefGenerator) decodeStringToInterface(schema string) (interface{}, error) {
var target interface{}
func (g *GraphQLTypeDefGenerator) decodeStringToInterface(schema string) (map[string]interface{}, error) {
var target map[string]interface{}
s := strings.NewReader(schema)
if err := json.NewDecoder(s).Decode(&target); err != nil {
return nil, err
Expand Down Expand Up @@ -138,7 +151,7 @@ func (g *GraphQLTypeDefGenerator) Generate() error {
}
// use_integer_enums

templates := g.createTemplates(params)
templates := g.GetTemplates(params)

for filename, t := range templates {
outputFile := g.getOutputFilePath(filename)
Expand Down
Loading

0 comments on commit 2b88712

Please sign in to comment.