-
Notifications
You must be signed in to change notification settings - Fork 2
/
interfaces.go
59 lines (48 loc) · 1.46 KB
/
interfaces.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
package plc
import (
"fmt"
"reflect"
)
type ReadWriter interface {
Reader
Writer
}
// Reader is the interface that wraps the basic ReadTag method.
type Reader interface {
// ReadTag reads the requested tag into the provided value.
ReadTag(name string, value interface{}) error
}
// Writer is the interface that wraps the basic WriteTag method.
type Writer interface {
// WriteTag writes the provided tag and value.
WriteTag(name string, value interface{}) error
}
// Closer is the interface that wraps the basic Close method.
//
// The behavior of Close after the first call is undefined.
// Specific implementations may document their own behavior.
type Closer interface {
Close() error
}
// FakeReadWriter is provided as an example ReadWriter implementation and for use in tests.
type FakeReadWriter map[string]interface{}
func (df FakeReadWriter) ReadTag(name string, value interface{}) error {
v, ok := df[name]
if !ok {
return fmt.Errorf("FakeReadWriter does not contain '%s'", name)
}
in := reflect.ValueOf(v)
out := reflect.Indirect(reflect.ValueOf(value))
switch {
case !out.CanSet():
return fmt.Errorf("FakeReadWriter for '%s', cannot set %s", name, out.Type().Name())
case out.Kind() != in.Kind():
return fmt.Errorf("FakeReadWriter for '%s', cannot set %s to %s (%v)", name, out.Type().Name(), in.Type().Name(), v)
}
out.Set(in)
return nil
}
func (df FakeReadWriter) WriteTag(name string, value interface{}) error {
df[name] = value
return nil
}