Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/codegen/template_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,8 @@ var TemplateFunctions = template.FuncMap{
"getResponseTypeDefinitions": getResponseTypeDefinitions,
"toStringArray": toStringArray,
"lower": strings.ToLower,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"title": titleCaser.String,
"stripNewLines": stripNewLines,
"sanitizeGoIdentity": SanitizeGoIdentity,
Expand Down
19 changes: 19 additions & 0 deletions pkg/codegen/template_helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package codegen

import (
"bytes"
"testing"
"text/template"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestIsCustomHTTPMethod(t *testing.T) {
Expand All @@ -16,6 +19,22 @@ func TestIsCustomHTTPMethod(t *testing.T) {
}
}

func TestHasPrefixHasSuffixTemplateFuncs(t *testing.T) {
tmpl := template.Must(template.New("t").Funcs(TemplateFunctions).Parse(
`{{ if hasPrefix . "Get" }}p{{ end }}{{ if hasSuffix . "Response" }}s{{ end }}`))

for in, want := range map[string]string{
"GetPetResponse": "ps",
"GetPet": "p",
"PetResponse": "s",
"Pet": "",
} {
var buf bytes.Buffer
require.NoError(t, tmpl.Execute(&buf, in))
assert.Equal(t, want, buf.String(), in)
}
}

func TestCustomHTTPMethods(t *testing.T) {
// Standard-only specs yield nil so templates gated on the result emit
// nothing and existing generated code stays byte-identical.
Expand Down
Loading