add definition config
This commit is contained in:
33
definition/option/tpl/option.tmpl
Normal file
33
definition/option/tpl/option.tmpl
Normal file
@@ -0,0 +1,33 @@
|
||||
// read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) read{{.FuncName}}(ctx context.Context) (v {{.Type}},e error) {
|
||||
val, err := i.Value(ctx, {{ .ParentKeys }}"{{ .Name }}")
|
||||
if err != nil {
|
||||
{{if .HasDefault}}
|
||||
{{$default := .Default}}
|
||||
{{range .DefaultErrors}}
|
||||
if errors.Is(err,{{.}}){
|
||||
return {{$default}}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
return v, fmt.Errorf("read {{.Keys}}:%w",err)
|
||||
}
|
||||
|
||||
{{.Parse "val" "v" .Keys }}
|
||||
}
|
||||
|
||||
// Read{{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) Read{{.FuncName}}(ctx context.Context) ({{.Type}}, error) {
|
||||
return i.read{{.FuncName}}(ctx)
|
||||
}
|
||||
|
||||
// {{.FuncName}} {{.Description}}.
|
||||
func (i {{.StructName}}) {{.FuncName}}({{if .Context}} ctx context.Context {{end}}) {{.Type}} {
|
||||
{{if not .Context}} ctx := context.Background() {{end}}
|
||||
val, err := i.read{{.FuncName}}(ctx)
|
||||
if err != nil {
|
||||
i.log(ctx, "get {{.Keys}}: %v", err)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
3
definition/option/tpl/parse.tmpl
Normal file
3
definition/option/tpl/parse.tmpl
Normal file
@@ -0,0 +1,3 @@
|
||||
{{block "Parse" .}}
|
||||
return {{.ValName}}.Parse{{ .FuncType}}()
|
||||
{{end}}
|
||||
8
definition/option/tpl/unmarshal_json.tmpl
Normal file
8
definition/option/tpl/unmarshal_json.tmpl
Normal file
@@ -0,0 +1,8 @@
|
||||
{{block "UnmarshalJSON" . }}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalJSON([]byte(pval))
|
||||
{{end}}
|
||||
8
definition/option/tpl/unmarshal_text.tmpl
Normal file
8
definition/option/tpl/unmarshal_text.tmpl
Normal file
@@ -0,0 +1,8 @@
|
||||
{{block "UnmarshalText" . }}
|
||||
pval, perr := {{.ValName}}.ParseString()
|
||||
if perr != nil {
|
||||
return {{.Value}}, fmt.Errorf("read {{.Keys}}:%w", perr)
|
||||
}
|
||||
|
||||
return {{.Value}}, {{.Value}}.UnmarshalText([]byte(pval))
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user