-
Notifications
You must be signed in to change notification settings - Fork 110
/
wrapper.go
98 lines (82 loc) · 2.4 KB
/
wrapper.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gizmoapi
// The contents of this file are only needed for standalone (non-module) uses.
import (
"context"
"sync"
"github.com/edaniels/golog"
goutils "go.viam.com/utils"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/utils"
)
func wrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) {
mc, ok := r.(Gizmo)
if !ok {
return nil, NewUnimplementedInterfaceError(r)
}
if reconfigurable, ok := mc.(*reconfigurableGizmo); ok {
return reconfigurable, nil
}
return &reconfigurableGizmo{actual: mc, name: name}, nil
}
var (
_ = Gizmo(&reconfigurableGizmo{})
_ = resource.Reconfigurable(&reconfigurableGizmo{})
)
type reconfigurableGizmo struct {
mu sync.RWMutex
name resource.Name
actual Gizmo
}
func (g *reconfigurableGizmo) ProxyFor() interface{} {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual
}
func (g *reconfigurableGizmo) DoOne(ctx context.Context, arg1 string) (bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoOne(ctx, arg1)
}
func (g *reconfigurableGizmo) DoOneClientStream(ctx context.Context, arg1 []string) (bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoOneClientStream(ctx, arg1)
}
func (g *reconfigurableGizmo) DoOneServerStream(ctx context.Context, arg1 string) ([]bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoOneServerStream(ctx, arg1)
}
func (g *reconfigurableGizmo) DoOneBiDiStream(ctx context.Context, arg1 []string) ([]bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoOneBiDiStream(ctx, arg1)
}
func (g *reconfigurableGizmo) DoTwo(ctx context.Context, arg1 bool) (string, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoTwo(ctx, arg1)
}
func (g *reconfigurableGizmo) Reconfigure(ctx context.Context, newGizmo resource.Reconfigurable) error {
g.mu.Lock()
defer g.mu.Unlock()
actual, ok := newGizmo.(*reconfigurableGizmo)
if !ok {
return utils.NewUnexpectedTypeError(g, newGizmo)
}
if err := goutils.TryClose(ctx, g.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
g.actual = actual.actual
return nil
}
func (g *reconfigurableGizmo) Name() resource.Name {
g.mu.RLock()
defer g.mu.RUnlock()
return g.name
}
func (g *reconfigurableGizmo) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoCommand(ctx, cmd)
}