-
Notifications
You must be signed in to change notification settings - Fork 3
/
bson.go
87 lines (67 loc) · 1.78 KB
/
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
83
84
85
86
87
package key
import (
"github.com/pkg/errors"
bsonenc "github.com/spikeekips/mitum/util/encoder/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
func unmarshalbson(t bsontype.Type, b []byte, parser func(string) (Key, error)) (Key, error) {
if t != bsontype.String {
return nil, errors.Errorf("invalid marshaled type for privatekey, %v", t)
}
s, _, ok := bsoncore.ReadString(b)
if !ok {
return nil, errors.Errorf("can not read string")
}
return parser(s)
}
func (k BasePrivatekey) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bsontype.String, bsoncore.AppendString(nil, k.String()), nil
}
func (k *BasePrivatekey) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
i, err := unmarshalbson(t, b, func(s string) (Key, error) {
return ParseBasePrivatekey(s)
})
if err != nil {
return err
}
uk, ok := i.(BasePrivatekey)
if !ok {
return errors.Errorf("not privatekey: %T", uk)
}
*k = uk
return nil
}
func (k *BasePrivatekey) UnpackBSON(b []byte, _ *bsonenc.Encoder) error {
uk, err := LoadBasePrivatekey(string(b))
if err != nil {
return err
}
*k = uk
return nil
}
func (k BasePublickey) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bsontype.String, bsoncore.AppendString(nil, k.String()), nil
}
func (k *BasePublickey) UnmarshalBSONValue(t bsontype.Type, b []byte) error {
i, err := unmarshalbson(t, b, func(s string) (Key, error) {
return ParseBasePublickey(s)
})
if err != nil {
return err
}
uk, ok := i.(BasePublickey)
if !ok {
return errors.Errorf("not publickey: %T", uk)
}
*k = uk
return nil
}
func (k *BasePublickey) UnpackBSON(b []byte, _ *bsonenc.Encoder) error {
uk, err := LoadBasePublickey(string(b))
if err != nil {
return err
}
*k = uk
return nil
}