-
Notifications
You must be signed in to change notification settings - Fork 2
/
register_currency_bson.go
82 lines (65 loc) · 2 KB
/
register_currency_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
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
package currency // nolint: dupl
import (
"github.com/ProtoconNet/mitum-currency/v3/common"
"go.mongodb.org/mongo-driver/bson"
bsonenc "github.com/ProtoconNet/mitum-currency/v3/digest/util/bson"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/ProtoconNet/mitum2/util/valuehash"
)
func (fact RegisterCurrencyFact) MarshalBSON() ([]byte, error) {
return bsonenc.Marshal(
bson.M{
"_hint": fact.Hint().String(),
"currency": fact.currency,
"hash": fact.BaseFact.Hash().String(),
"token": fact.BaseFact.Token(),
},
)
}
type RegisterCurrencyFactBSONUnmarshaler struct {
Hint string `bson:"_hint"`
Currency bson.Raw `bson:"currency"`
}
func (fact *RegisterCurrencyFact) DecodeBSON(b []byte, enc *bsonenc.Encoder) error {
var u common.BaseFactBSONUnmarshaler
err := enc.Unmarshal(b, &u)
if err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *fact)
}
h := valuehash.NewBytesFromString(u.Hash)
fact.BaseFact.SetHash(h)
err = fact.BaseFact.SetToken(u.Token)
if err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *fact)
}
var uf RegisterCurrencyFactBSONUnmarshaler
if err := bson.Unmarshal(b, &uf); err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *fact)
}
ht, err := hint.ParseHint(uf.Hint)
if err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *fact)
}
fact.BaseHinter = hint.NewBaseHinter(ht)
if err := fact.unpack(enc, uf.Currency); err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *fact)
}
return nil
}
func (op RegisterCurrency) MarshalBSON() ([]byte, error) {
return bsonenc.Marshal(
bson.M{
"_hint": op.Hint().String(),
"hash": op.Hash(),
"fact": op.Fact(),
"signs": op.Signs(),
})
}
func (op *RegisterCurrency) DecodeBSON(b []byte, enc *bsonenc.Encoder) error {
var ubo common.BaseNodeOperation
if err := ubo.DecodeBSON(b, enc); err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *op)
}
op.BaseNodeOperation = ubo
return nil
}