chore: simplify templates (#26787)

This commit is contained in:
andig 2026-01-18 18:52:16 +01:00 • committed by GitHub
parent 4048f855d9
commit 510102bdd9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 40 additions and 97 deletions

View file

@ -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)

View file

@ -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,
})
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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))

View file

@ -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"},
},
},
}

View file

@ -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
}