Skip to content

Commit 2b3176c

Browse files
committed
feat: #1536 add sprig template functions
co-author: @deefdragon
1 parent 0d94f2f commit 2b3176c

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@ require (
1414
)
1515

1616
require (
17+
dario.cat/mergo v1.0.1 // indirect
18+
github.com/Masterminds/goutils v1.1.1 // indirect
19+
github.com/Masterminds/semver/v3 v3.3.0 // indirect
20+
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
1721
github.com/davecgh/go-spew v1.1.1 // indirect
1822
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
1923
github.com/go-openapi/jsonpointer v0.21.0 // indirect
2024
github.com/go-openapi/swag v0.23.0 // indirect
25+
github.com/google/uuid v1.6.0 // indirect
26+
github.com/huandu/xstrings v1.5.0 // indirect
2127
github.com/josharian/intern v1.0.0 // indirect
2228
github.com/mailru/easyjson v0.7.7 // indirect
29+
github.com/mitchellh/copystructure v1.2.0 // indirect
30+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2331
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
2432
github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect
2533
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
2634
github.com/perimeterx/marshmallow v1.1.5 // indirect
2735
github.com/pmezard/go-difflib v1.0.0 // indirect
36+
github.com/shopspring/decimal v1.4.0 // indirect
2837
github.com/speakeasy-api/jsonpath v0.6.0 // indirect
38+
github.com/spf13/cast v1.7.0 // indirect
2939
github.com/ugorji/go/codec v1.2.11 // indirect
3040
github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect
41+
golang.org/x/crypto v0.26.0 // indirect
3142
golang.org/x/sync v0.9.0 // indirect
3243
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
2+
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
3+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
4+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
5+
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
6+
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
7+
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
8+
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
19
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
210
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
311
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
@@ -33,7 +41,11 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
3341
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3442
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3543
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
44+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
45+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3646
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
47+
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
48+
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
3749
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
3850
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
3951
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
@@ -46,6 +58,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
4658
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
4759
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
4860
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
61+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
62+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
63+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
64+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
4965
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
5066
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
5167
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
@@ -75,10 +91,14 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
7591
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
7692
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
7793
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
94+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
95+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
7896
github.com/speakeasy-api/jsonpath v0.6.0 h1:IhtFOV9EbXplhyRqsVhHoBmmYjblIRh5D1/g8DHMXJ8=
7997
github.com/speakeasy-api/jsonpath v0.6.0/go.mod h1:ymb2iSkyOycmzKwbEAYPJV/yi2rSmvBCLZJcyD+VVWw=
8098
github.com/speakeasy-api/openapi-overlay v0.10.2 h1:VOdQ03eGKeiHnpb1boZCGm7x8Haj6gST0P3SGTX95GU=
8199
github.com/speakeasy-api/openapi-overlay v0.10.2/go.mod h1:n0iOU7AqKpNFfEt6tq7qYITC4f0yzVVdFw0S7hukemg=
100+
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
101+
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
82102
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
83103
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
84104
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -92,6 +112,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
92112
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
93113
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
94114
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
115+
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
116+
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
95117
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
96118
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
97119
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=

pkg/codegen/codegen.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
149149
globalState.initialismsMap = makeInitialismsMap(opts.OutputOptions.AdditionalInitialisms)
150150

151151
// This creates the golang templates text package
152-
TemplateFunctions["opts"] = func() Configuration { return globalState.options }
153-
t := template.New("oapi-codegen").Funcs(TemplateFunctions)
152+
tmplFns := GetTemplateFunctions()
153+
tmplFns["opts"] = func() Configuration { return globalState.options }
154+
t := template.New("oapi-codegen").Funcs(tmplFns)
154155
// This parses all of our own template files into the template object
155156
// above
156157
err := LoadTemplates(templates, t)

pkg/codegen/template_helpers.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ package codegen
1616
import (
1717
"bytes"
1818
"fmt"
19+
"maps"
1920
"os"
2021
"strings"
2122
"text/template"
2223

2324
"golang.org/x/text/cases"
2425
"golang.org/x/text/language"
2526

27+
"github.com/Masterminds/sprig/v3"
2628
"github.com/getkin/kin-openapi/openapi3"
2729
"github.com/oapi-codegen/oapi-codegen/v2/pkg/util"
2830
)
@@ -318,32 +320,41 @@ func genServerURLWithVariablesFunctionParams(goTypePrefix string, variables map[
318320

319321
// TemplateFunctions is passed to the template engine, and we can call each
320322
// function here by keyName from the template code.
321-
var TemplateFunctions = template.FuncMap{
322-
"genParamArgs": genParamArgs,
323-
"genParamTypes": genParamTypes,
324-
"genParamNames": genParamNames,
325-
"genParamFmtString": ReplacePathParamsWithStr,
326-
"swaggerUriToIrisUri": SwaggerUriToIrisUri,
327-
"swaggerUriToEchoUri": SwaggerUriToEchoUri,
328-
"swaggerUriToFiberUri": SwaggerUriToFiberUri,
329-
"swaggerUriToChiUri": SwaggerUriToChiUri,
330-
"swaggerUriToGinUri": SwaggerUriToGinUri,
331-
"swaggerUriToGorillaUri": SwaggerUriToGorillaUri,
332-
"swaggerUriToStdHttpUri": SwaggerUriToStdHttpUri,
333-
"lcFirst": LowercaseFirstCharacter,
334-
"ucFirst": UppercaseFirstCharacter,
335-
"ucFirstWithPkgName": UppercaseFirstCharacterWithPkgName,
336-
"camelCase": ToCamelCase,
337-
"genResponsePayload": genResponsePayload,
338-
"genResponseTypeName": genResponseTypeName,
339-
"genResponseUnmarshal": genResponseUnmarshal,
340-
"getResponseTypeDefinitions": getResponseTypeDefinitions,
341-
"toStringArray": toStringArray,
342-
"lower": strings.ToLower,
343-
"title": titleCaser.String,
344-
"stripNewLines": stripNewLines,
345-
"sanitizeGoIdentity": SanitizeGoIdentity,
346-
"toGoComment": StringWithTypeNameToGoComment,
347-
348-
"genServerURLWithVariablesFunctionParams": genServerURLWithVariablesFunctionParams,
323+
var templateFunctions template.FuncMap
324+
325+
func GetTemplateFunctions() template.FuncMap {
326+
if templateFunctions == nil {
327+
templateFunctions = template.FuncMap{
328+
"genParamArgs": genParamArgs,
329+
"genParamTypes": genParamTypes,
330+
"genParamNames": genParamNames,
331+
"genParamFmtString": ReplacePathParamsWithStr,
332+
"swaggerUriToIrisUri": SwaggerUriToIrisUri,
333+
"swaggerUriToEchoUri": SwaggerUriToEchoUri,
334+
"swaggerUriToFiberUri": SwaggerUriToFiberUri,
335+
"swaggerUriToChiUri": SwaggerUriToChiUri,
336+
"swaggerUriToGinUri": SwaggerUriToGinUri,
337+
"swaggerUriToGorillaUri": SwaggerUriToGorillaUri,
338+
"swaggerUriToStdHttpUri": SwaggerUriToStdHttpUri,
339+
"lcFirst": LowercaseFirstCharacter,
340+
"ucFirst": UppercaseFirstCharacter,
341+
"ucFirstWithPkgName": UppercaseFirstCharacterWithPkgName,
342+
"camelCase": ToCamelCase,
343+
"genResponsePayload": genResponsePayload,
344+
"genResponseTypeName": genResponseTypeName,
345+
"genResponseUnmarshal": genResponseUnmarshal,
346+
"getResponseTypeDefinitions": getResponseTypeDefinitions,
347+
"toStringArray": toStringArray,
348+
"lower": strings.ToLower,
349+
"title": titleCaser.String,
350+
"stripNewLines": stripNewLines,
351+
"sanitizeGoIdentity": SanitizeGoIdentity,
352+
"toGoComment": StringWithTypeNameToGoComment,
353+
354+
"genServerURLWithVariablesFunctionParams": genServerURLWithVariablesFunctionParams,
355+
}
356+
maps.Copy(templateFunctions, sprig.FuncMap())
357+
}
358+
359+
return templateFunctions
349360
}

0 commit comments

Comments
 (0)