Improve actionlog err/message placeholder replacement

This commit is contained in:
Denis Arh
2020-05-26 19:02:32 +02:00
parent 2597029164
commit 4d751c1875
11 changed files with 580 additions and 184 deletions
+28 -7
View File
@@ -113,7 +113,22 @@ func (p {{ $.Service }}ActionProps) serialize() actionlog.Meta {
// This function is auto-generated.
//
func (p {{ $.Service }}ActionProps) tr(in string, err error) string {
var pairs = []string{"{err}"}
var (
pairs = []string{"{err}"}
{{- if $.Props }}
// first non-empty string
fns = func(ii ... interface{}) string {
for _, i:= range ii {
if s :=fmt.Sprintf("%v", i); len(s) > 0 {
return s
}
}
return ""
}
{{- end }}
)
if err != nil {
for {
@@ -133,17 +148,23 @@ func (p {{ $.Service }}ActionProps) tr(in string, err error) string {
{{- range $prop := $.Props }}
{{- if $prop.Builtin }}
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}}", fmt.Sprintf("%v", p.{{ $prop.Name }}))
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}}", fns(p.{{ $prop.Name }}))
{{- else }}
if p.{{ $prop.Name }} != nil {
{{- if $prop.DefaultField }}
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}}", fmt.Sprintf("%v", p.{{ $prop.Name }}.{{ camelCase " " $prop.DefaultField }}))
{{- end }}
// replacement for "{{"{"}}{{ $prop.Name }}}" (in order how fields are defined)
pairs = append(
pairs,
"{{"{"}}{{ $prop.Name }}}",
fns(
{{- range $f := $prop.Fields }}
p.{{ $prop.Name }}.{{ camelCase " " $f }},
{{- end }}
),
)
{{- range $f := $prop.Fields }}
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}.{{ $f }}}", fmt.Sprintf("%v", p.{{ $prop.Name }}.{{ camelCase " " $f }}))
pairs = append(pairs, "{{"{"}}{{ $prop.Name }}.{{ $f }}}", fns(p.{{ $prop.Name }}.{{ camelCase " " $f }}))
{{- end }}
}
{{- end }}
+4 -10
View File
@@ -23,11 +23,10 @@ type (
// List of event/log properties that can/will be captured
// and injected into log or message string
propsDef struct {
Name string
Type string
Fields []string
DefaultField string
Builtin bool
Name string
Type string
Fields []string
Builtin bool
}
actionDef struct {
@@ -195,11 +194,6 @@ func procDef(path, output string) {
knownProps[m.Name] = true
if len(m.Fields) > 0 {
m.DefaultField = m.Fields[0]
}
for _, f := range m.Fields {
knownProps[fmt.Sprintf("%s.%s", m.Name, f)] = true