-
Notifications
You must be signed in to change notification settings - Fork 109
/
collectors.go
51 lines (41 loc) · 1.05 KB
/
collectors.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
package encoder
import (
"context"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
)
type method int64
const (
ticksCount method = iota
)
func (m method) String() string {
if m == ticksCount {
return "TicksCount"
}
return "Unknown"
}
// Ticks wraps the returned ticks value.
type Ticks struct {
Ticks int64
}
func newTicksCountCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
encoder, err := assertEncoder(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
v, _, err := encoder.GetPosition(ctx, PositionTypeUnspecified, nil)
if err != nil {
return nil, data.FailedToReadErr(params.ComponentName, ticksCount.String(), err)
}
return Ticks{Ticks: int64(v)}, nil
})
return data.NewCollector(cFunc, params)
}
func assertEncoder(resource interface{}) (Encoder, error) {
encoder, ok := resource.(Encoder)
if !ok {
return nil, data.InvalidInterfaceErr(API)
}
return encoder, nil
}