-
Notifications
You must be signed in to change notification settings - Fork 110
/
mysum.go
58 lines (49 loc) · 1.28 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
// 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/config"
"go.viam.com/rdk/examples/customresources/apis/summationapi"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"
)
var Model = resource.NewModel(
resource.Namespace("acme"),
resource.ModelFamilyName("demo"),
resource.ModelName("mysum"),
)
func init() {
registry.RegisterService(summationapi.Subtype, Model, registry.Service{
Constructor: newMySum,
})
}
type mySum struct {
mu sync.Mutex
subtract bool
}
func newMySum(ctx context.Context, deps registry.Dependencies, cfg config.Service, logger *zap.SugaredLogger) (interface{}, error) {
return &mySum{subtract: cfg.Attributes.Bool("subtract", false)}, 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, cfg config.Service) error {
m.mu.Lock()
defer m.mu.Unlock()
m.subtract = cfg.Attributes.Bool("subtract", false)
return nil
}