From f06922b298bc0afb1d9ff2a683282169f021ff2e Mon Sep 17 00:00:00 2001 From: andig Date: Tue, 27 Sep 2022 17:43:27 +0200 Subject: [PATCH] Fronius Gen24: fix integer deprecated (#4602) --- templates/definition/meter/fronius-gen24.yaml | 2 ++ util/templates/init.go | 7 ++++++- util/templates/render.go | 5 ++++- util/templates/template.go | 4 +++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/templates/definition/meter/fronius-gen24.yaml b/templates/definition/meter/fronius-gen24.yaml index e858090a0..711aebc59 100644 --- a/templates/definition/meter/fronius-gen24.yaml +++ b/templates/definition/meter/fronius-gen24.yaml @@ -14,6 +14,8 @@ params: - name: host - name: port default: 502 + - name: integer + deprecated: true render: | # reference: https://github.com/volkszaehler/mbmd/blob/master/meters/sunspec/models.go {{- if eq .usage "grid" }} diff --git a/util/templates/init.go b/util/templates/init.go index cf1baeace..ba758159f 100644 --- a/util/templates/init.go +++ b/util/templates/init.go @@ -1,6 +1,7 @@ package templates import ( + "bytes" "fmt" "io/fs" "path" @@ -28,8 +29,12 @@ func init() { } func FromBytes(b []byte) (Template, error) { + // panic if template definition contains unknown fields + dec := yaml.NewDecoder(bytes.NewReader(b)) + dec.KnownFields(true) + var definition TemplateDefinition - if err := yaml.Unmarshal(b, &definition); err != nil { + if err := dec.Decode(&definition); err != nil { return Template{}, err } diff --git a/util/templates/render.go b/util/templates/render.go index a7c0b00fd..554373ccb 100644 --- a/util/templates/render.go +++ b/util/templates/render.go @@ -1,6 +1,7 @@ package templates import ( + "errors" "os" "testing" @@ -37,7 +38,9 @@ func RenderInstance(class Class, other map[string]interface{}) (Instance, error) } var instance Instance - err = yaml.Unmarshal(b, &instance) + if err = yaml.Unmarshal(b, &instance); err == nil && instance.Type == "" { + err = errors.New("empty instance type- check for missing usage") + } return instance, err } diff --git a/util/templates/template.go b/util/templates/template.go index 5b3a7a629..f2dfcaaa7 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -317,9 +317,11 @@ func (t *Template) RenderResult(renderMode string, other map[string]interface{}) out := strings.ToLower(key) if i, p := t.ParamByName(key); i == -1 { - if !slices.Contains(predefinedTemplateProperties, strings.ToLower(key)) { + if !slices.Contains(predefinedTemplateProperties, out) { return nil, values, fmt.Errorf("invalid key: %s", key) } + } else if p.Deprecated { + continue } else { out = p.Name }