-
Notifications
You must be signed in to change notification settings - Fork 458
/
uint64.go
44 lines (36 loc) · 1.06 KB
/
uint64.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
package types
import (
"strconv"
"strings"
"github.com/filecoin-project/go-leb128"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/polydawn/refmt/obj/atlas"
)
func init() {
cbor.RegisterCborType(uint64AtlasEntry)
}
var uint64AtlasEntry = atlas.BuildEntry(Uint64(0)).Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(u Uint64) ([]byte, error) {
return leb128.FromUInt64(uint64(u)), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x []byte) (Uint64, error) {
return Uint64(leb128.ToUInt64(x)), nil
})).
Complete()
// Uint64 is an unsigned 64-bit variable-length-encoded integer.
type Uint64 uint64
// MarshalJSON converts a Uint64 to a json string and returns it.
func (u Uint64) MarshalJSON() ([]byte, error) {
return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil
}
// UnmarshalJSON converts a json string to a Uint64.
func (u *Uint64) UnmarshalJSON(b []byte) error {
val, err := strconv.ParseUint(strings.Trim(string(b), `"`), 10, 64)
if err != nil {
return err
}
*u = Uint64(val)
return nil
}