Skip to content

Commit 4284f9d

Browse files
committed
feat(goctl): add default template func map utility
1 parent d212c81 commit 4284f9d

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

tools/goctl/api/gogen/genroutes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
1515
"github.com/zeromicro/go-zero/tools/goctl/config"
1616
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
17+
"github.com/zeromicro/go-zero/tools/goctl/util"
1718
"github.com/zeromicro/go-zero/tools/goctl/util/format"
1819
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
1920
"github.com/zeromicro/go-zero/tools/goctl/vars"
@@ -92,7 +93,7 @@ func genRoutes(dir, rootPkg, projectPkg string, cfg *config.Config, api *spec.Ap
9293
}
9394

9495
var hasTimeout bool
95-
gt := template.Must(template.New("groupTemplate").Parse(templateText))
96+
gt := template.Must(template.New("groupTemplate").Funcs(util.DefaultFuncMap()).Parse(templateText))
9697
for _, g := range groups {
9798
var gbuilder strings.Builder
9899
gbuilder.WriteString("[]rest.Route{")

tools/goctl/go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.21
44

55
require (
66
github.com/DATA-DOG/go-sqlmock v1.5.2
7+
github.com/Masterminds/sprig/v3 v3.3.0
78
github.com/emicklei/proto v1.14.2
89
github.com/fatih/structtag v1.2.0
910
github.com/go-openapi/spec v0.21.1-0.20250328170532-a3928469592e
@@ -24,7 +25,10 @@ require (
2425
)
2526

2627
require (
28+
dario.cat/mergo v1.0.1 // indirect
2729
filippo.io/edwards25519 v1.1.0 // indirect
30+
github.com/Masterminds/goutils v1.1.1 // indirect
31+
github.com/Masterminds/semver/v3 v3.3.0 // indirect
2832
github.com/alicebob/miniredis/v2 v2.35.0 // indirect
2933
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521184019-c5ad59b459ec // indirect
3034
github.com/beorn7/perks v1.0.1 // indirect
@@ -50,6 +54,7 @@ require (
5054
github.com/grafana/pyroscope-go v1.2.4 // indirect
5155
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
5256
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
57+
github.com/huandu/xstrings v1.5.0 // indirect
5358
github.com/inconshreveable/mousetrap v1.1.0 // indirect
5459
github.com/jackc/pgpassfile v1.0.0 // indirect
5560
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
@@ -62,6 +67,8 @@ require (
6267
github.com/mailru/easyjson v0.9.0 // indirect
6368
github.com/mattn/go-colorable v0.1.13 // indirect
6469
github.com/mattn/go-isatty v0.0.20 // indirect
70+
github.com/mitchellh/copystructure v1.2.0 // indirect
71+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
6572
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6673
github.com/modern-go/reflect2 v1.0.2 // indirect
6774
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
@@ -73,8 +80,10 @@ require (
7380
github.com/prometheus/common v0.62.0 // indirect
7481
github.com/prometheus/procfs v0.15.1 // indirect
7582
github.com/redis/go-redis/v9 v9.12.1 // indirect
83+
github.com/shopspring/decimal v1.4.0 // indirect
7684
github.com/spaolacci/murmur3 v1.1.0 // indirect
7785
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
86+
github.com/spf13/cast v1.7.0 // indirect
7887
github.com/yuin/gopher-lua v1.1.1 // indirect
7988
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
8089
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect

tools/goctl/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
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=
13
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
24
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
35
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
46
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
7+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
8+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
9+
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
10+
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
11+
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
12+
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
513
github.com/alicebob/miniredis/v2 v2.35.0 h1:QwLphYqCEAo1eu1TqPRN2jgVMPBweeQcR21jeqDCONI=
614
github.com/alicebob/miniredis/v2 v2.35.0/go.mod h1:TcL7YfarKPGDAthEtl5NBeHZfeUQj6OXMm/+iu5cLMM=
715
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210521184019-c5ad59b459ec h1:EEyRvzmpEUZ+I8WmD5cw/vY8EqhambkOqy5iFr0908A=
@@ -36,6 +44,8 @@ github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
3644
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
3745
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
3846
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
47+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
48+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
3949
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
4050
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
4151
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
@@ -83,6 +93,8 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1
8393
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
8494
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
8595
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
96+
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
97+
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
8698
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
8799
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
88100
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
@@ -119,6 +131,10 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
119131
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
120132
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
121133
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
134+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
135+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
136+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
137+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
122138
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
123139
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
124140
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -153,13 +169,22 @@ github.com/redis/go-redis/v9 v9.12.1/go.mod h1:huWgSWd8mW6+m0VPhJjSSQ+d6Nh1VICQ6
153169
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
154170
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
155171
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
172+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
173+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
156174
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
157175
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
158176
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
159177
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
160178
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
161179
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
162180
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
181+
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
182+
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
183+
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
184+
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
185+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
186+
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
187+
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
163188
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
164189
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
165190
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

tools/goctl/util/templatex.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"text/template"
99

10+
"github.com/Masterminds/sprig/v3"
1011
"github.com/zeromicro/go-zero/tools/goctl/internal/errorx"
1112
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
1213
)
@@ -93,3 +94,15 @@ func TemplateVariable(text string) string {
9394
}
9495
return ""
9596
}
97+
98+
func DefaultFuncMap() template.FuncMap {
99+
return sprig.FuncMap()
100+
}
101+
102+
func MergeWithDefaultFuncMap(funcMap template.FuncMap) template.FuncMap {
103+
mergedFuncMap := DefaultFuncMap()
104+
for k, v := range funcMap {
105+
mergedFuncMap[k] = v
106+
}
107+
return mergedFuncMap
108+
}

tools/goctl/util/templatex_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package util
22

33
import (
4+
"bytes"
45
"testing"
6+
"text/template"
57

8+
"github.com/stretchr/testify/assert"
69
"github.com/zeromicro/go-zero/tools/goctl/test"
710
)
811

@@ -91,3 +94,39 @@ func TestTemplateVariable(t *testing.T) {
9194
}...)
9295
executor.Run(t, TemplateVariable)
9396
}
97+
98+
func TestTemplateFuncMap(t *testing.T) {
99+
cases := []struct {
100+
name string
101+
funcMap template.FuncMap
102+
template string
103+
variable map[string]any
104+
want string
105+
}{
106+
{
107+
name: "upper",
108+
template: "{{.foo | upper}}",
109+
variable: map[string]any{"foo": "bar"},
110+
want: "BAR",
111+
},
112+
{
113+
name: "repeat upper",
114+
template: "{{.foo | repeat 3 | upper}}",
115+
variable: map[string]any{"foo": "bar"},
116+
want: "BARBARBAR",
117+
},
118+
}
119+
for _, c := range cases {
120+
t.Run(c.name, func(t *testing.T) {
121+
funcMap := DefaultFuncMap()
122+
if c.funcMap != nil {
123+
funcMap = MergeWithDefaultFuncMap(c.funcMap)
124+
}
125+
tpl := template.Must(template.New("test").Funcs(funcMap).Parse(c.template))
126+
var buf bytes.Buffer
127+
err := tpl.Execute(&buf, c.variable)
128+
assert.NoError(t, err)
129+
assert.Equal(t, c.want, buf.String())
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)