mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
feat: goctl model Add a new method hasField (#5484)
This commit is contained in:
@@ -15,15 +15,17 @@ const regularPerm = 0o666
|
||||
|
||||
// DefaultTemplate is a tool to provides the text/template operations
|
||||
type DefaultTemplate struct {
|
||||
name string
|
||||
text string
|
||||
goFmt bool
|
||||
name string
|
||||
text string
|
||||
goFmt bool
|
||||
funcMap template.FuncMap
|
||||
}
|
||||
|
||||
// With returns an instance of DefaultTemplate
|
||||
func With(name string) *DefaultTemplate {
|
||||
return &DefaultTemplate{
|
||||
name: name,
|
||||
name: name,
|
||||
funcMap: make(template.FuncMap),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +57,11 @@ func (t *DefaultTemplate) SaveTo(data any, path string, forceUpdate bool) error
|
||||
|
||||
// Execute returns the codes after the template executed
|
||||
func (t *DefaultTemplate) Execute(data any) (*bytes.Buffer, error) {
|
||||
tem, err := template.New(t.name).Parse(t.text)
|
||||
tmp := template.New(t.name)
|
||||
if len(t.funcMap) > 0 {
|
||||
tmp.Funcs(t.funcMap)
|
||||
}
|
||||
tem, err := tmp.Parse(t.text)
|
||||
if err != nil {
|
||||
return nil, errorx.Wrap(err, "template parse error:", t.text)
|
||||
}
|
||||
@@ -79,6 +85,16 @@ func (t *DefaultTemplate) Execute(data any) (*bytes.Buffer, error) {
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
// AddFunc adds a template function. It returns the template instance for chaining.
|
||||
// If funcName is empty or function is nil, it returns the template without modification.
|
||||
func (t *DefaultTemplate) AddFunc(funcName string, function any) *DefaultTemplate {
|
||||
if funcName == "" || function == nil {
|
||||
return t
|
||||
}
|
||||
t.funcMap[funcName] = function
|
||||
return t
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
||||
Reference in New Issue
Block a user