-
Notifications
You must be signed in to change notification settings - Fork 36
/
Results.go
46 lines (40 loc) · 1.25 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
package types
import (
"fmt"
"strings"
"github.com/itering/scale.go/utiles"
)
type Result struct {
ScaleDecoder
}
func (b *Result) Process() {
optionValue := utiles.BytesToHex(b.NextBytes(1))
subType := strings.Split(b.SubType, ",")
if len(subType) != 2 {
panic("Result subType not illegal")
}
if optionValue == "00" || optionValue == "" {
b.Value = map[string]interface{}{"Ok": b.ProcessAndUpdateData(subType[0])}
} else if optionValue == "01" {
b.Value = map[string]interface{}{"Error": b.ProcessAndUpdateData(subType[1])}
} else {
panic("illegal Result data")
}
}
func (b *Result) Encode(value map[string]interface{}) string {
subType := strings.Split(b.SubType, ",")
if len(subType) != 2 {
panic("Result subType not illegal")
}
if data, ok := value["Ok"]; ok {
return "00" + EncodeWithOpt(subType[0], data, &ScaleDecoderOption{Spec: b.Spec, Metadata: b.Metadata})
}
if data, ok := value["Error"]; ok {
return "01" + EncodeWithOpt(subType[1], data, &ScaleDecoderOption{Spec: b.Spec, Metadata: b.Metadata})
}
panic("illegal Result data")
}
func (b *Result) TypeStructString() string {
subType := strings.Split(b.SubType, ",")
return fmt.Sprintf("Results<%s,%s>", getTypeStructString(subType[0], 0), getTypeStructString(subType[1], 0))
}