/
operation_value_bson.go
55 lines (46 loc) · 1.2 KB
/
operation_value_bson.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
package digest
import (
"time"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/util/encoder"
bsonenc "github.com/spikeekips/mitum/util/encoder/bson"
"go.mongodb.org/mongo-driver/bson"
)
func (va OperationValue) MarshalBSON() ([]byte, error) {
return bsonenc.Marshal(bsonenc.MergeBSONM(
bsonenc.NewHintedDoc(va.Hint()),
bson.M{
"op": va.op,
"height": va.height,
"confirmed_at": va.confirmedAt,
"in_state": va.inState,
"reason": va.reason,
"index": va.index,
},
))
}
type OperationValueBSONUnpacker struct {
OP bson.Raw `bson:"op"`
HT base.Height `bson:"height"`
CT time.Time `bson:"confirmed_at"`
IN bool `bson:"in_state"`
RS bson.Raw `bson:"reason"`
ID uint64 `bson:"index"`
}
func (va *OperationValue) UnpackBSON(b []byte, enc *bsonenc.Encoder) error {
var uva OperationValueBSONUnpacker
if err := enc.Unmarshal(b, &uva); err != nil {
return err
}
if err := encoder.Decode(uva.OP, enc, &va.op); err != nil {
return err
}
if err := encoder.Decode(uva.RS, enc, &va.reason); err != nil {
return err
}
va.height = uva.HT
va.confirmedAt = uva.CT
va.inState = uva.IN
va.index = uva.ID
return nil
}