11package templates
22
33import (
4+ "bytes"
45 _ "embed"
5- "fmt "
6+ "slices "
67
78 "go.yaml.in/yaml/v4"
89)
@@ -12,12 +13,11 @@ var defaults []byte
1213
1314type configDefaults struct {
1415 Params []Param // Default values for common parameters
15- Presets map [string ]struct {
16- Params []Param
17- }
18- Modbus struct { // Details about possible ModbusInterfaces and ModbusConnectionTypes
19- Interfaces map [string ][]string // Information about physical modbus interface types (rs485, tcpip)
20- Types map [string ]struct { // Details about different ways to connect to a ModbusInterface and its defaults
16+ Presets map [string ][]Param
17+ Modbus struct { // Details about possible ModbusInterfaces and ModbusConnectionTypes
18+ Definitions []Param
19+ Interfaces map [string ][]string // Information about physical modbus interface types (rs485, tcpip)
20+ Types map [string ]struct { // Details about different ways to connect to a ModbusInterface and its defaults
2121 Description TextLanguage
2222 Params []Param
2323 }
@@ -32,25 +32,22 @@ func (c *configDefaults) Load() {
3232 return
3333 }
3434
35- if err := yaml .Unmarshal (defaults , & c ); err != nil {
36- panic (fmt .Errorf ("failed to parse deviceGroupListDefinition: %v" , err ))
35+ // panic on unknown fields
36+ dec := yaml .NewDecoder (bytes .NewReader (defaults ))
37+ dec .KnownFields (true )
38+
39+ if err := dec .Decode (& c ); err != nil {
40+ panic ("failed to parse config defaults: " + err .Error ())
3741 }
3842
3943 // resolve modbus param references
40- for k := range c .Modbus .Types {
41- for i , p := range c .Modbus .Types [k ].Params {
42- // if this is a reference, get the referenced values and then overwrite it with the values defined here
43- if p .IsReference () {
44- finalName := p .Name
45- referencedItemName := p .Name
46- if p .ReferenceName != "" {
47- referencedItemName = p .ReferenceName
48- }
49- _ , referencedParam := c .ParamByName (referencedItemName )
50- referencedParam .OverwriteProperties (p )
51- referencedParam .Name = finalName
52- p = referencedParam
53- c .Modbus .Types [k ].Params [i ] = p
44+ for typ := range c .Modbus .Types {
45+ for i , p := range c .Modbus .Types [typ ].Params {
46+ if idx := slices .IndexFunc (c .Modbus .Definitions , func (pd Param ) bool {
47+ return pd .Name == p .Name
48+ }); idx >= 0 {
49+ p .OverwriteProperties (c .Modbus .Definitions [idx ])
50+ c .Modbus .Types [typ ].Params [i ] = p
5451 }
5552 }
5653 }
0 commit comments