-
Notifications
You must be signed in to change notification settings - Fork 46
/
results.go
63 lines (55 loc) · 1.39 KB
/
results.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
package app
import (
"errors"
"github.com/iov-one/weave"
)
// ResultsFromKeys returns a ResultSet of all keys
// given a set of models
func ResultsFromKeys(models []weave.Model) *ResultSet {
res := make([][]byte, len(models))
for i, m := range models {
res[i] = m.Key
}
return &ResultSet{Results: res}
}
// ResultsFromValues returns a ResultSet of all values
// given a set of models
func ResultsFromValues(models []weave.Model) *ResultSet {
res := make([][]byte, len(models))
for i, m := range models {
res[i] = m.Value
}
return &ResultSet{Results: res}
}
// JoinResults inverts ResultsFromKeys and ResultsFromValues
// and makes then a consistent whole again
func JoinResults(keys, values *ResultSet) ([]weave.Model, error) {
kref, vref := keys.Results, values.Results
if len(kref) != len(vref) {
return nil, errors.New("Mismatches result set size")
}
mods := make([]weave.Model, len(kref))
for i := range mods {
mods[i] = weave.Model{
Key: kref[i],
Value: vref[i],
}
}
return mods, nil
}
// UnmarshalOneResult will parse a resultset, and
// it if is not empty, unmarshal the first result into o
func UnmarshalOneResult(bz []byte, o weave.Persistent) error {
// get the resultset
var res ResultSet
err := res.Unmarshal(bz)
if err != nil {
return err
}
// no results, do nothing
if len(res.Results) == 0 {
return nil
}
err = o.Unmarshal(res.Results[0])
return err
}