feat(goctl): Support gateway sample generation (#3049)
This commit is contained in:
@@ -140,3 +140,15 @@ func ContainsAny(s string, runes ...rune) bool {
|
||||
func ContainsWhiteSpace(s string) bool {
|
||||
return ContainsAny(s, WhiteSpace...)
|
||||
}
|
||||
|
||||
func IsWhiteSpace(text string) bool {
|
||||
if len(text) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, r := range text {
|
||||
if !unicode.IsSpace(r) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
goformat "go/format"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
"text/template"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/internal/errorx"
|
||||
@@ -77,3 +78,18 @@ func (t *DefaultTemplate) Execute(data any) (*bytes.Buffer, error) {
|
||||
buf.Write(formatOutput)
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// IsTemplateVariable returns true if the text is a template variable.
|
||||
// The text must start with a dot and be a valid template.
|
||||
func IsTemplateVariable(text string) bool {
|
||||
match, _ := regexp.MatchString(`(?m)^{{(\.\w+)+}}$`, text)
|
||||
return match
|
||||
}
|
||||
|
||||
// TemplateVariable returns the variable name of the template.
|
||||
func TemplateVariable(text string) string {
|
||||
if IsTemplateVariable(text) {
|
||||
return text[3 : len(text)-2]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
93
tools/goctl/util/templatex_test.go
Normal file
93
tools/goctl/util/templatex_test.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/test"
|
||||
)
|
||||
|
||||
func TestIsTemplate(t *testing.T) {
|
||||
executor := test.NewExecutor[string, bool]()
|
||||
executor.Add([]test.Data[string, bool]{
|
||||
{
|
||||
Name: "empty",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{foo}",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{.foo}",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "$foo",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{{foo}}",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{{.}}",
|
||||
Want: false,
|
||||
},
|
||||
{
|
||||
Name: "valid",
|
||||
Input: "{{.foo}}",
|
||||
Want: true,
|
||||
},
|
||||
{
|
||||
Name: "valid",
|
||||
Input: "{{.foo.bar}}",
|
||||
Want: true,
|
||||
},
|
||||
}...)
|
||||
executor.Run(t, IsTemplateVariable)
|
||||
}
|
||||
|
||||
func TestTemplateVariable(t *testing.T) {
|
||||
executor := test.NewExecutor[string, string]()
|
||||
executor.Add([]test.Data[string, string]{
|
||||
{
|
||||
Name: "empty",
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{foo}",
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{.foo}",
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "$foo",
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{{foo}}",
|
||||
},
|
||||
{
|
||||
Name: "invalid",
|
||||
Input: "{{.}}",
|
||||
},
|
||||
{
|
||||
Name: "valid",
|
||||
Input: "{{.foo}}",
|
||||
Want: "foo",
|
||||
},
|
||||
{
|
||||
Name: "valid",
|
||||
Input: "{{.foo.bar}}",
|
||||
Want: "foo.bar",
|
||||
},
|
||||
}...)
|
||||
executor.Run(t, TemplateVariable)
|
||||
}
|
||||
Reference in New Issue
Block a user