-
Notifications
You must be signed in to change notification settings - Fork 2
/
mint_item_bson.go
40 lines (35 loc) · 1.04 KB
/
mint_item_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
package currency // nolint:dupl
import (
"github.com/ProtoconNet/mitum-currency/v3/common"
bsonenc "github.com/ProtoconNet/mitum-currency/v3/digest/util/bson"
"github.com/ProtoconNet/mitum2/util/hint"
"go.mongodb.org/mongo-driver/bson"
)
func (it MintItem) MarshalBSON() ([]byte, error) {
return bsonenc.Marshal(
bson.M{
"_hint": it.Hint().String(),
"receiver": it.receiver,
"amount": it.amount,
},
)
}
type MintItemBSONUnmarshaler struct {
Hint string `bson:"_hint"`
Receiver string `bson:"receiver"`
Amount bson.Raw `bson:"amount"`
}
func (it *MintItem) DecodeBSON(b []byte, enc *bsonenc.Encoder) error {
var uit MintItemBSONUnmarshaler
if err := bson.Unmarshal(b, &uit); err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *it)
}
ht, err := hint.ParseHint(uit.Hint)
if err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *it)
}
if err := it.unpack(enc, ht, uit.Receiver, uit.Amount); err != nil {
return common.DecorateError(err, common.ErrDecodeBson, *it)
}
return nil
}