Skip to content

Commit

Permalink
fix: embed template files
Browse files Browse the repository at this point in the history
  • Loading branch information
Takahiro Suzuki committed Feb 22, 2023
1 parent c4d7848 commit 8e0cf68
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 0 additions & 1 deletion cli/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func GenerateAction(context *cli.Context) error {
apiBuilder := api.NewAppSyncApiBuilder()
apiBuilder.SetName(context.String("name"))
apiBuilder.SetExportPath(context.String("output"))
apiBuilder.SetTemplates("./codegen/templates")
apiBuilder.SetSchema(context.String("schema"))
fmt.Printf("✅ Generated GraphQL schema to %s\n", c.Sprint(filepath.Join(*apiBuilder.ExportPath, "schema.graphql")))
apiBuilder.AddDataSource("DYNAMODB", context.String("name"))
Expand Down
6 changes: 3 additions & 3 deletions cli/generate_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package cli

import (
"github.com/kopkunka55/appsyncgen/codegen/api"
"testing"

"github.com/kopkunka55/appsyncgen/codegen/api"
)

func TestGenerate(t *testing.T) {
apiBuilder := api.NewAppSyncApiBuilder()
apiBuilder.SetExportPath("./../resolvers")
apiBuilder.SetTemplates("./../codegen/templates")
apiBuilder.SetSchema("./../testdata/schema.graphql")
apiBuilder.AddDataSource("DYNAMODB", "appsync")
apiBuilder.SetName("appsync")
appSync := apiBuilder.Build()
appSync.Synth()
appSync.Export()
appSync.Synth()
}
10 changes: 6 additions & 4 deletions codegen/api/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (a *AppSyncApiBuilder) AddDataSource(datasourceType string, datasourceName
}

func (a *AppSyncApiBuilder) SetSchema(pathToSchema string) {
a.SetTemplates()
if a.ExportPath == nil {
log.Fatalln("export path should be set before setting schema")
}
Expand All @@ -65,14 +66,15 @@ func (a *AppSyncApiBuilder) SetSchema(pathToSchema string) {
a.Schema = schema
}

func (a *AppSyncApiBuilder) SetTemplates(pathToTemplate string) {
func (a *AppSyncApiBuilder) SetTemplates() {

tmpl := templates.NewTemplate()
tmpl = templates.AddFunctionMap(tmpl, map[string]any{
"toLowerCase": strings.ToLower,
})
tmpl = templates.ImportTemplate(tmpl, filepath.Join(pathToTemplate, "resolver", "dynamodb", "*.tmpl"))
tmpl = templates.ImportTemplate(tmpl, filepath.Join(pathToTemplate, "resolver", "*.tmpl"))
tmpl = templates.ImportTemplate(tmpl, filepath.Join(pathToTemplate, "graphql", "*.tmpl"))
tmpl = templates.ImportTemplate(tmpl, "resolver/dynamodb/*.tmpl")
tmpl = templates.ImportTemplate(tmpl, "resolver/*.tmpl")
tmpl = templates.ImportTemplate(tmpl, "graphql/*.tmpl")
a.Templates = tmpl
}

Expand Down
6 changes: 5 additions & 1 deletion codegen/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package templates

import (
"bytes"
"embed"
"fmt"
"log"
"os"
"text/template"
)

//go:embed *
var f embed.FS

type DynamoDBResolverTemplateData struct {
PK string
SK string
Expand All @@ -22,7 +26,7 @@ func NewTemplate() *template.Template {

func ImportTemplate(tmpl *template.Template, tmplPaths ...string) *template.Template {
for _, p := range tmplPaths {
tmpl = template.Must(tmpl.ParseGlob(p))
tmpl = template.Must(tmpl.ParseFS(f, p))
}
return tmpl
}
Expand Down

0 comments on commit 8e0cf68

Please sign in to comment.