From 510102bdd9cbd224fc2e64f2a0019208165ed4a1 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 18 Jan 2026 18:52:16 +0100 Subject: [PATCH] chore: simplify templates (#26787) --- server/http_config_helper_test.go | 10 +++--- server/http_config_metadata_handler.go | 2 +- util/templates/init.go | 8 ++--- util/templates/render_testing.go | 2 +- util/templates/template.go | 50 ++++++-------------------- util/templates/template_test.go | 50 +++++++++++--------------- util/templates/types.go | 15 -------- 7 files changed, 40 insertions(+), 97 deletions(-) diff --git a/server/http_config_helper_test.go b/server/http_config_helper_test.go index 9db781d87..dcc3c22ec 100644 --- a/server/http_config_helper_test.go +++ b/server/http_config_helper_test.go @@ -166,12 +166,10 @@ func TestFilterValidTemplateParams(t *testing.T) { } result := filterValidTemplateParams(&templates.Template{ - TemplateDefinition: templates.TemplateDefinition{ - Params: []templates.Param{ - {Name: "usage"}, - {Name: "power"}, - {Name: "capacity"}, - }, + Params: []templates.Param{ + {Name: "usage"}, + {Name: "power"}, + {Name: "capacity"}, }, }, conf) diff --git a/server/http_config_metadata_handler.go b/server/http_config_metadata_handler.go index 22f5c2f09..bcd331093 100644 --- a/server/http_config_metadata_handler.go +++ b/server/http_config_metadata_handler.go @@ -117,7 +117,7 @@ func productsHandler(w http.ResponseWriter, r *http.Request) { for _, p := range t.Products { res = append(res, product{ Name: p.Title(lang), - Template: t.TemplateDefinition.Template, + Template: t.Template, Group: t.Group, }) } diff --git a/util/templates/init.go b/util/templates/init.go index d62eb4b42..65e3e9c34 100644 --- a/util/templates/init.go +++ b/util/templates/init.go @@ -68,15 +68,11 @@ func fromBytes(b []byte) (Template, error) { dec := yaml.NewDecoder(bytes.NewReader(b)) dec.KnownFields(true) - var definition TemplateDefinition - if err := dec.Decode(&definition); err != nil { + var tmpl Template + if err := dec.Decode(&tmpl); err != nil { return Template{}, err } - tmpl := Template{ - TemplateDefinition: definition, - } - for _, f := range []func() error{tmpl.ResolvePresets, tmpl.ResolveGroup, tmpl.UpdateParamsWithDefaults, tmpl.UpdateModbusParamsWithDefaults, tmpl.Validate} { if err := f(); err != nil { return tmpl, fmt.Errorf("template '%s': %w", tmpl.Template, err) diff --git a/util/templates/render_testing.go b/util/templates/render_testing.go index 8b0e2b101..57197b645 100644 --- a/util/templates/render_testing.go +++ b/util/templates/render_testing.go @@ -94,7 +94,7 @@ func TestClass(t *testing.T, class Class, instantiate func(t *testing.T, values values["host"] = "localhost" // test auth configuration - if err := testAuth(tmpl.TemplateDefinition.Auth); err != nil { + if err := testAuth(tmpl.Auth); err != nil { t.Error("authorization:", err) } diff --git a/util/templates/template.go b/util/templates/template.go index d20a4025b..ae2508734 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -16,10 +16,17 @@ import ( // Template describes is a proxy device for use with cli and automated testing type Template struct { - TemplateDefinition - - title string - titles []string + Template string + Deprecated bool `json:"-"` + Auth map[string]any `json:",omitempty"` // OAuth parameters (if required) + Group string `json:",omitempty"` // the group this template belongs to, references groupList entries + Covers []string `json:",omitempty"` // list of covered outdated template names + Products []Product `json:",omitempty"` // list of products this template is compatible with + Capabilities []string `json:",omitempty"` + Countries []CountryCode `json:",omitempty"` // list of countries supported by this template + Requirements Requirements `json:",omitempty"` + Params []Param `json:",omitempty"` + Render string `json:"-"` // rendering template } // UpdateParamWithDefaults adds default values to specific param name entries @@ -117,41 +124,6 @@ func (t *Template) Validate() error { return nil } -// set the language title by combining all product titles -func (t *Template) SetCombinedTitle(lang string) { - if len(t.titles) == 0 { - t.resolveTitles(lang) - } - - t.title = strings.Join(t.titles, "/") -} - -// set the title for this templates -func (t *Template) SetTitle(title string) { - t.title = title -} - -// return the title for this template -func (t *Template) Title() string { - return t.title -} - -// return the language specific product titles -func (t *Template) Titles(lang string) []string { - if len(t.titles) == 0 { - t.resolveTitles(lang) - } - - return t.titles -} - -// set the language specific product titles -func (t *Template) resolveTitles(lang string) { - for _, p := range t.Products { - t.titles = append(t.titles, p.Title(lang)) - } -} - // add the referenced base Params and overwrite existing ones func (t *Template) ResolvePresets() error { currentParams := make([]Param, len(t.Params)) diff --git a/util/templates/template_test.go b/util/templates/template_test.go index 10b889e89..25f73d2fa 100644 --- a/util/templates/template_test.go +++ b/util/templates/template_test.go @@ -16,12 +16,10 @@ func TestPresets(t *testing.T) { } tmpl := &Template{ - TemplateDefinition: TemplateDefinition{ - Params: []Param{ - {Name: "first"}, - {Preset: "preset"}, - {Name: "last"}, - }, + Params: []Param{ + {Name: "first"}, + {Preset: "preset"}, + {Name: "last"}, }, } @@ -36,12 +34,10 @@ func TestPresets(t *testing.T) { func TestRequired(t *testing.T) { tmpl := &Template{ - TemplateDefinition: TemplateDefinition{ - Params: []Param{ - { - Name: "param", - Required: true, - }, + Params: []Param{ + { + Name: "param", + Required: true, }, }, } @@ -69,13 +65,11 @@ func TestRequired(t *testing.T) { func TestRequiredDeprecated(t *testing.T) { tmpl := &Template{ - TemplateDefinition: TemplateDefinition{ - Params: []Param{ - { - Name: "param", - Required: true, - Deprecated: true, - }, + Params: []Param{ + { + Name: "param", + Required: true, + Deprecated: true, }, }, } @@ -103,16 +97,14 @@ func TestRequiredDeprecated(t *testing.T) { func TestRequiredPerUsage(t *testing.T) { tmpl := &Template{ - TemplateDefinition: TemplateDefinition{ - Params: []Param{ - { - Name: "usage", - }, - { - Name: "param", - Required: true, - Usages: []string{"battery"}, - }, + Params: []Param{ + { + Name: "usage", + }, + { + Name: "param", + Required: true, + Usages: []string{"battery"}, }, }, } diff --git a/util/templates/types.go b/util/templates/types.go index 1e32a537c..97ee3383a 100644 --- a/util/templates/types.go +++ b/util/templates/types.go @@ -284,18 +284,3 @@ func (c CountryCode) IsValid() bool { validCode := regexp.MustCompile(`^[A-Z]{2}$`) return validCode.MatchString(string(c)) } - -// TemplateDefinition contains properties of a device template -type TemplateDefinition struct { - Template string - Deprecated bool `json:"-"` - Auth map[string]any `json:",omitempty"` // OAuth parameters (if required) - Group string `json:",omitempty"` // the group this template belongs to, references groupList entries - Covers []string `json:",omitempty"` // list of covered outdated template names - Products []Product `json:",omitempty"` // list of products this template is compatible with - Capabilities []string `json:",omitempty"` - Countries []CountryCode `json:",omitempty"` // list of countries supported by this template - Requirements Requirements `json:",omitempty"` - Params []Param `json:",omitempty"` - Render string `json:"-"` // rendering template -}