-
Notifications
You must be signed in to change notification settings - Fork 110
/
mysum.go
82 lines (70 loc) · 2.16 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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.viam.com/rdk/examples/customresources/apis/summationapi"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
)
// Model is the full model definition.
var Model = resource.NewModel("acme", "demo", "mysum")
// Config is the sum model's config.
type Config struct {
Subtract bool `json:"subtract,omitempty"` // the omitempty defaults the bool to golang's default of false
// Embed TriviallyValidateConfig to make config validation a no-op. We will not check if any attributes exist
// or are set to anything in particular, and there will be no implicit dependencies.
// Config structs used in resource registration must implement Validate.
resource.TriviallyValidateConfig
}
func init() {
resource.RegisterService(summationapi.API, Model, resource.Registration[summationapi.Summation, *Config]{
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 logging.Logger,
) (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 {
// This takes the generic resource.Config passed down from the parent and converts it to the
// model-specific (aka "native") Config structure defined above making it easier to directly access attributes.
sumConfig, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
m.subtract = sumConfig.Subtract
return nil
}