Skip to content

Commit 8d437bb

Browse files
committed
add config files examples and parsers
1 parent 76d81a2 commit 8d437bb

File tree

9 files changed

+207
-1
lines changed

9 files changed

+207
-1
lines changed

.vscode/settings.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,12 @@
33
"Victron"
44
],
55
"go.lintTool": "golangci-lint",
6-
"go.testFlags": ["-race"],
6+
"go.testFlags": ["-race", "-count=1"],
7+
"go.addTags": {
8+
"tags": "json,yaml",
9+
"options": "json=omitempty,yaml=omitempty",
10+
"promptForTags": false,
11+
"transform": "camelcase",
12+
"template": ""
13+
}
714
}

configs/smartshunt500.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
serial:
2+
path: /dev/ttyUSB0
3+
baudRate: 19200
4+
dataBits: 8
5+
stopBits: 1
6+
parity: N
7+
gauges:
8+
- name: "battery_volts"
9+
help: "Main battery voltage"
10+
# matches the thing from ve-direct
11+
label: "V"
12+
multiplier: 0.01
13+
# TODO: OTHERS

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
module github.com/lazy-electron-consulting/ve-direct-exporter
22

33
go 1.18
4+
5+
require (
6+
github.com/goburrow/serial v0.1.0
7+
github.com/stretchr/testify v1.7.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.0 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
gopkg.in/yaml.v2 v2.4.0
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
15+
)

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA=
4+
github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA=
5+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
11+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
13+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
15+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config/config.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"io"
6+
7+
"github.com/lazy-electron-consulting/ve-direct-exporter/internal/util"
8+
"gopkg.in/yaml.v2"
9+
)
10+
11+
const (
12+
DefaultAddress = ":8000"
13+
DefaultBaudRate = 19200
14+
DefaultDataBits = 8
15+
DefaultStopBits = 1
16+
DefaultParity = "N"
17+
)
18+
19+
type Serial struct {
20+
Path string `json:"path,omitempty" yaml:"path,omitempty"`
21+
BaudRate int `json:"baudRate,omitempty" yaml:"baudRate,omitempty"`
22+
DataBits int `json:"dataBits,omitempty" yaml:"dataBits,omitempty"`
23+
StopBits int `json:"stopBits,omitempty" yaml:"stopBits,omitempty"`
24+
Parity string `json:"parity,omitempty" yaml:"parity,omitempty"`
25+
}
26+
27+
func (m *Serial) defaults() {
28+
m.BaudRate = util.Default(m.BaudRate, DefaultBaudRate)
29+
m.DataBits = util.Default(m.DataBits, DefaultDataBits)
30+
m.StopBits = util.Default(m.StopBits, DefaultStopBits)
31+
m.Parity = util.Default(m.Parity, DefaultParity)
32+
}
33+
34+
type Gauge struct {
35+
Name string `json:"name,omitempty" yaml:"name,omitempty"`
36+
Label string `json:"label,omitempty" yaml:"label,omitempty"`
37+
Help string `json:"help,omitempty" yaml:"help,omitempty"`
38+
Multiplier float32 `json:"multiplier,omitempty" yaml:"multiplier,omitempty"`
39+
}
40+
41+
type Config struct {
42+
Address string `json:"address,omitempty" yaml:"address,omitempty"`
43+
Serial Serial `json:"serial,omitempty" yaml:"serial,omitempty"`
44+
Gauges []Gauge `json:"gauges,omitempty" yaml:"gauges,omitempty"`
45+
}
46+
47+
func (c *Config) defaults() {
48+
c.Address = util.Default(c.Address, DefaultAddress)
49+
c.Serial.defaults()
50+
51+
}
52+
53+
func ParseYaml(r io.Reader) (*Config, error) {
54+
decoder := yaml.NewDecoder(r)
55+
decoder.SetStrict(true)
56+
57+
var config Config
58+
if err := decoder.Decode(&config); err != nil {
59+
return nil, fmt.Errorf("failed to parse: %w", err)
60+
}
61+
config.defaults()
62+
return &config, nil
63+
}

internal/config/config_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package config_test
2+
3+
import (
4+
"embed"
5+
"testing"
6+
7+
"github.com/lazy-electron-consulting/ve-direct-exporter/internal/config"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
//go:embed testdata
12+
var testdata embed.FS
13+
14+
func TestParseYaml(t *testing.T) {
15+
t.Parallel()
16+
17+
tests := map[string]struct {
18+
path string
19+
want *config.Config
20+
}{
21+
"full": {
22+
path: "testdata/full.yml",
23+
want: &config.Config{
24+
Address: ":9090",
25+
Serial: config.Serial{
26+
Path: "/dev/ttyUSB0",
27+
BaudRate: 9600,
28+
DataBits: 7,
29+
StopBits: 2,
30+
Parity: "Y",
31+
},
32+
Gauges: []config.Gauge{
33+
{
34+
Name: "battery_volts",
35+
Help: "Main battery voltage",
36+
Label: "V",
37+
Multiplier: 0.01,
38+
},
39+
},
40+
},
41+
},
42+
"empty": {
43+
path: "testdata/empty.yml",
44+
want: &config.Config{
45+
Address: config.DefaultAddress,
46+
Serial: config.Serial{
47+
Path: "/dev/ttyUSB1",
48+
BaudRate: config.DefaultBaudRate,
49+
DataBits: config.DefaultDataBits,
50+
StopBits: config.DefaultStopBits,
51+
Parity: config.DefaultParity,
52+
},
53+
},
54+
},
55+
}
56+
for name, tc := range tests {
57+
tc := tc
58+
t.Run(name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
file, err := testdata.Open(tc.path)
62+
require.NoError(t, err)
63+
64+
got, err := config.ParseYaml(file)
65+
require.NoError(t, err)
66+
require.EqualValues(t, tc.want, got)
67+
})
68+
}
69+
}

internal/config/testdata/empty.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
serial:
2+
path: /dev/ttyUSB1

internal/config/testdata/full.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
address: :9090
2+
serial:
3+
path: /dev/ttyUSB0
4+
baudRate: 9600
5+
dataBits: 7
6+
stopBits: 2
7+
parity: Y
8+
gauges:
9+
- name: "battery_volts"
10+
help: "Main battery voltage"
11+
# matches the thing from ve-direct
12+
label: "V"
13+
multiplier: 0.01

internal/util/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package util
2+
3+
// Default checks if the given value is empty, and if so returns the defaultValue
4+
func Default[T comparable](value, defaultValue T) T {
5+
var x T
6+
if value == x {
7+
return defaultValue
8+
}
9+
return value
10+
}
11+
12+
func Ptr[T any](value T) *T { return &value }

0 commit comments

Comments
 (0)