-
Notifications
You must be signed in to change notification settings - Fork 4
/
value.go
61 lines (51 loc) · 1.41 KB
/
value.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 scp
import (
"fmt"
"strings"
)
// Value is the abstract type of values being voted on by the network.
type Value interface {
// Less tells whether this value is less than another. Values must be totally ordered.
Less(Value) bool
// Combine combines this value with another to produce a third
// (which may be the same as either of the inputs). The operation
// should be deterministic and commutative.
Combine(Value, SlotID) Value
// IsNil tells whether this should be considered a nil value.
IsNil() bool
// Bytes produces a byte-string representation of the value, not
// meant for human consumption.
Bytes() []byte
// String produces a readable representation of the value.
String() string
}
// VString calls a Value's String method. If the value is nil, returns
// the string "<nil>".
func VString(v Value) string {
if isNilVal(v) {
return "<nil>"
}
return v.String()
}
// Combine reduces the members of vs to a single value using
// Value.Combine. The result is nil if vs is empty.
func (vs ValueSet) Combine(slotID SlotID) Value {
if len(vs) == 0 {
return nil
}
result := vs[0]
for _, v := range vs[1:] {
result = result.Combine(v, slotID)
}
return result
}
func (vs ValueSet) String() string {
var strs []string
for _, v := range vs {
strs = append(strs, VString(v))
}
return fmt.Sprintf("[%s]", strings.Join(strs, " "))
}
func isNilVal(v Value) bool {
return v == nil || v.IsNil()
}