/
base_operation_json.go
101 lines (76 loc) · 2.2 KB
/
base_operation_json.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
99
100
101
package base
import (
"encoding/json"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/encoder"
"github.com/spikeekips/mitum/util/hint"
"github.com/spikeekips/mitum/util/valuehash"
)
type BaseOperationJSONMarshaler struct {
Hash util.Hash `json:"hash"`
Fact Fact `json:"fact"`
Signs []Sign `json:"signs"`
hint.BaseHinter
}
func (op BaseOperation) JSONMarshaler() BaseOperationJSONMarshaler {
return BaseOperationJSONMarshaler{
BaseHinter: op.BaseHinter,
Hash: op.h,
Fact: op.fact,
Signs: op.signs,
}
}
func (op BaseOperation) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(op.JSONMarshaler())
}
type BaseOperationJSONUnmarshaler struct {
Hash valuehash.HashDecoder `json:"hash"`
Fact json.RawMessage `json:"fact"`
Signs []json.RawMessage `json:"signs"`
}
func (op *BaseOperation) decodeJSON(b []byte, enc encoder.Encoder, u *BaseOperationJSONUnmarshaler) error {
if err := enc.Unmarshal(b, u); err != nil {
return err
}
op.h = u.Hash.Hash()
if err := encoder.Decode(enc, u.Fact, &op.fact); err != nil {
return errors.WithMessage(err, "decode fact")
}
return nil
}
func (op *BaseOperation) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode BaseOperation")
var u BaseOperationJSONUnmarshaler
if err := op.decodeJSON(b, enc, &u); err != nil {
return e.Wrap(err)
}
op.signs = make([]Sign, len(u.Signs))
for i := range u.Signs {
var ub BaseSign
if err := ub.DecodeJSON(u.Signs[i], enc); err != nil {
return e.WithMessage(err, "decode sign")
}
op.signs[i] = ub
}
return nil
}
func (op BaseNodeOperation) MarshalJSON() ([]byte, error) {
return util.MarshalJSON(op.JSONMarshaler())
}
func (op *BaseNodeOperation) DecodeJSON(b []byte, enc encoder.Encoder) error {
e := util.StringError("decode BaseNodeOperation")
var u BaseOperationJSONUnmarshaler
if err := op.decodeJSON(b, enc, &u); err != nil {
return e.Wrap(err)
}
op.signs = make([]Sign, len(u.Signs))
for i := range u.Signs {
var ub BaseNodeSign
if err := ub.DecodeJSON(u.Signs[i], enc); err != nil {
return e.WithMessage(err, "decode sign")
}
op.signs[i] = ub
}
return nil
}