/
string_address.go
57 lines (44 loc) · 1.47 KB
/
string_address.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
package types
import (
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
)
var StringAddressHint = hint.MustNewHint("sas-v2")
type StringAddress struct {
BaseStringAddress
}
func NewStringAddress(s string) StringAddress {
return StringAddress{
BaseStringAddress: NewBaseStringAddressWithHint(StringAddressHint, s),
}
}
func ParseStringAddress(s string) (StringAddress, error) {
b, t, err := hint.ParseFixedTypedString(s, base.AddressTypeSize)
switch {
case err != nil:
return StringAddress{}, errors.Wrap(err, "parse StringAddress")
case t != StringAddressHint.Type():
return StringAddress{}, util.ErrInvalid.Errorf("wrong hint type in StringAddress")
}
return NewStringAddress(b), nil
}
func (ad StringAddress) IsValid([]byte) error {
if err := ad.BaseHinter.IsValid(StringAddressHint.Type().Bytes()); err != nil {
return util.ErrInvalid.WithMessage(err, "wrong hint in StringAddress")
}
if err := ad.BaseStringAddress.IsValid(nil); err != nil {
return errors.Wrap(err, "invalid StringAddress")
}
return nil
}
func (ad *StringAddress) UnmarshalText(b []byte) error {
ad.s = string(b) + StringAddressHint.Type().String()
return nil
}
func (ad StringAddress) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bsontype.String, bsoncore.AppendString(nil, ad.s), nil
}