-
Notifications
You must be signed in to change notification settings - Fork 110
/
mysum.go
61 lines (51 loc) · 1.4 KB
/
mysum.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
// Package mysum implements an acme:service:summation, a demo service which sums (or subtracts) a given list of numbers.
package mysum
import (
"context"
"errors"
"sync"
"go.uber.org/zap"
"go.viam.com/rdk/examples/customresources/apis/summationapi"
"go.viam.com/rdk/resource"
)
var Model = resource.NewModel("acme", "demo", "mysum")
func init() {
resource.RegisterService(summationapi.API, Model, resource.Registration[summationapi.Summation, resource.NoNativeConfig]{
Constructor: newMySum,
})
}
type mySum struct {
resource.Named
resource.TriviallyCloseable
mu sync.Mutex
subtract bool
}
func newMySum(ctx context.Context, deps resource.Dependencies, conf resource.Config, logger *zap.SugaredLogger) (summationapi.Summation, error) {
summer := &mySum{
Named: conf.ResourceName().AsNamed(),
}
if err := summer.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return summer, nil
}
func (m *mySum) Sum(ctx context.Context, nums []float64) (float64, error) {
if len(nums) <= 0 {
return 0, errors.New("must provide at least one number to sum")
}
var ret float64
for _, n := range nums {
if m.subtract {
ret -= n
} else {
ret += n
}
}
return ret, nil
}
func (m *mySum) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
m.mu.Lock()
defer m.mu.Unlock()
m.subtract = conf.Attributes.Bool("subtract", false)
return nil
}