Skip to content

Commit

Permalink
Use go:embed to embed template files
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinromaszewicz committed Jan 21, 2022
1 parent 6614812 commit bd5a9da
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 1,402 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ help:
@echo " tidy tidy go mod"

generate:
go generate ./pkg/...
go generate ./...

test:
Expand Down
12 changes: 6 additions & 6 deletions internal/test/issues/issue-52/issue.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 4 additions & 40 deletions internal/test/issues/issue-52/issue_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package issue_52

import (
_ "embed"
"testing"

"github.com/getkin/kin-openapi/openapi3"
Expand All @@ -9,48 +10,11 @@ import (
"github.com/deepmap/oapi-codegen/pkg/codegen"
)

const spec = `
openapi: 3.0.2
info:
version: '0.0.1'
title: example
desscription: |
Make sure that recursive types are handled properly
paths:
/example:
get:
operationId: exampleGet
responses:
'200':
description: "OK"
content:
'application/json':
schema:
$ref: '#/components/schemas/Document'
components:
schemas:
Document:
type: object
properties:
fields:
type: object
additionalProperties:
$ref: '#/components/schemas/Value'
Value:
type: object
properties:
stringValue:
type: string
arrayValue:
$ref: '#/components/schemas/ArrayValue'
ArrayValue:
type: array
items:
$ref: '#/components/schemas/Value'
`
//go:embed spec.yaml
var spec []byte

func TestIssue(t *testing.T) {
swagger, err := openapi3.NewLoader().LoadFromData([]byte(spec))
swagger, err := openapi3.NewLoader().LoadFromData(spec)
require.NoError(t, err)

opts := codegen.Options{
Expand Down
2 changes: 1 addition & 1 deletion internal/test/issues/issue-52/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ openapi: 3.0.2
info:
version: '0.0.1'
title: example
desscription: |
description: |
Make sure that recursive types are handled properly
paths:
/example:
Expand Down
37 changes: 34 additions & 3 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ package codegen
import (
"bufio"
"bytes"
"embed"
"fmt"
"io/fs"
"runtime/debug"
"sort"
"strings"
"text/template"

"github.com/getkin/kin-openapi/openapi3"
"golang.org/x/tools/imports"

"github.com/deepmap/oapi-codegen/pkg/codegen/templates"
)

// Embed the templates directory
//go:embed templates
var templates embed.FS

// Options defines the optional code to generate.
type Options struct {
GenerateChiServer bool // GenerateChiServer specifies whether to generate chi server boilerplate
Expand Down Expand Up @@ -116,7 +120,8 @@ func Generate(swagger *openapi3.T, packageName string, opts Options) (string, er
t := template.New("oapi-codegen").Funcs(TemplateFunctions)
// This parses all of our own template files into the template object
// above
t, err := templates.Parse(t)
err := LoadTemplates(templates, t)

if err != nil {
return "", fmt.Errorf("error parsing oapi-codegen templates: %w", err)
}
Expand Down Expand Up @@ -620,3 +625,29 @@ func SanitizeCode(goCode string) string {
// See: https://groups.google.com/forum/#!topic/golang-nuts/OToNIPdfkks
return strings.Replace(goCode, "\uFEFF", "", -1)
}

// LoadTemplates loads all of our template files into a text/template. The
// path of template is relative to the templates directory.
func LoadTemplates(src embed.FS, t *template.Template) error {
return fs.WalkDir(src, "templates", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("error walking directory %s: %w", path, err)
}
if d.IsDir() {
return nil
}

buf, err := src.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file '%s': %w", path, err)
}

templateName := strings.TrimPrefix(path, "templates/")
tmpl := t.New(templateName)
_, err = tmpl.Parse(string(buf))
if err != nil {
return fmt.Errorf("parsing template '%s': %w", path, err)
}
return nil
})
}
6 changes: 3 additions & 3 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,19 +660,19 @@ func GenerateTypesForOperations(t *template.Template, ops []OperationDefinition)
// GenerateChiServer This function generates all the go code for the ServerInterface as well as
// all the wrapper functions around our handlers.
func GenerateChiServer(t *template.Template, operations []OperationDefinition) (string, error) {
return GenerateTemplates([]string{"chi-interface.tmpl", "chi-middleware.tmpl", "chi-handler.tmpl"}, t, operations)
return GenerateTemplates([]string{"chi/chi-interface.tmpl", "chi/chi-middleware.tmpl", "chi/chi-handler.tmpl"}, t, operations)
}

// GenerateEchoServer This function generates all the go code for the ServerInterface as well as
// all the wrapper functions around our handlers.
func GenerateEchoServer(t *template.Template, operations []OperationDefinition) (string, error) {
return GenerateTemplates([]string{"echo-interface.tmpl", "echo-wrappers.tmpl", "echo-register.tmpl"}, t, operations)
return GenerateTemplates([]string{"echo/echo-interface.tmpl", "echo/echo-wrappers.tmpl", "echo/echo-register.tmpl"}, t, operations)
}

// GenerateGinServer This function generates all the go code for the ServerInterface as well as
// all the wrapper functions around our handlers.
func GenerateGinServer(t *template.Template, operations []OperationDefinition) (string, error) {
return GenerateTemplates([]string{"gin-interface.tmpl", "gin-wrappers.tmpl", "gin-register.tmpl"}, t, operations)
return GenerateTemplates([]string{"gin/gin-interface.tmpl", "gin/gin-wrappers.tmpl", "gin/gin-register.tmpl"}, t, operations)
}

// Uses the template engine to generate the function which registers our wrappers
Expand Down
3 changes: 0 additions & 3 deletions pkg/codegen/templates/doc.go

This file was deleted.

Loading

0 comments on commit bd5a9da

Please sign in to comment.