/
util.go
executable file
·43 lines (35 loc) · 1.04 KB
/
util.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
package rosetta
import (
"encoding/json"
"time"
crgerrs "github.com/cosmos/cosmos-sdk/server/rosetta/lib/errors"
)
// timeToMilliseconds converts time to milliseconds timestamp
func timeToMilliseconds(t time.Time) int64 {
return t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
}
// unmarshalMetadata unmarshals the given meta to the target
func unmarshalMetadata(meta map[string]interface{}, target interface{}) error {
b, err := json.Marshal(meta)
if err != nil {
return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
}
err = json.Unmarshal(b, target)
if err != nil {
return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
}
return nil
}
// marshalMetadata marshals the given interface to map[string]interface{}
func marshalMetadata(o interface{}) (meta map[string]interface{}, err error) {
b, err := json.Marshal(o)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
}
meta = make(map[string]interface{})
err = json.Unmarshal(b, &meta)
if err != nil {
return nil, err
}
return
}