-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
82 lines (66 loc) · 2.11 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package config
import (
"encoding/json"
"time"
"google.golang.org/protobuf/proto"
"github.com/smart-core-os/sc-golang/pkg/cmp"
"github.com/vanti-dev/sc-bos/pkg/auto"
)
type Root struct {
auto.Config
// Broker configures an MQTT broker to export data to.
Broker *MQTTBroker `json:"broker,omitempty"`
Sources []RawSource `json:"sources,omitempty"`
}
type Source struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
TopicPrefix string `json:"topicPrefix,omitempty"`
Duplicates *Duplicates `json:"duplicates,omitempty"`
}
type RawSource struct {
Source
Raw json.RawMessage `json:"-"`
}
func (r *RawSource) UnmarshalJSON(buf []byte) error {
_ = json.Unmarshal(buf, &r.Raw)
return json.Unmarshal(buf, &r.Source)
}
type Duplicates struct {
Include bool `json:"include,omitempty"`
// Consider floating point values of consecutive publications to be equal if they are within FloatMargin of each other.
// 1.2 and 1.22 are within 0.1 FloatMargin of each other and would be classes as duplicates.
FloatMargin *float64 `json:"floatMargin,omitempty"`
// there's room here to configure what we class as duplicate: is 1.200000001 a duplicate of 1.2
// Fields should map well to the sc-golang cmp package Value funcs.
}
func (d *Duplicates) TrackDuplicates() bool {
return d == nil || !d.Include
}
func (d *Duplicates) Cmp() cmp.Message {
if d == nil {
return cmp.Equal(
cmp.FloatValueApprox(0, 0.01),
cmp.TimeValueWithin(10*time.Millisecond),
cmp.DurationValueWithin(10*time.Millisecond),
)
}
if d.Include {
return func(x, y proto.Message) bool {
return false
}
}
var opts []cmp.Value
if d.FloatMargin == nil {
opts = append(opts, cmp.FloatValueApprox(0, 0.01))
} else {
opts = append(opts, cmp.FloatValueApprox(0, *d.FloatMargin))
}
// here we'd look at any other configuration options and apply them as cmp.Value to the response
// We don't have any other options yet so we just return the default
opts = append(opts,
cmp.TimeValueWithin(10*time.Millisecond),
cmp.DurationValueWithin(10*time.Millisecond),
)
return cmp.Equal(opts...)
}