github.com/Finschia/finschia-sdk@v0.48.1/server/rosetta/util.go (about)

     1  package rosetta
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	crgerrs "github.com/Finschia/finschia-sdk/server/rosetta/lib/errors"
     8  )
     9  
    10  // timeToMilliseconds converts time to milliseconds timestamp
    11  func timeToMilliseconds(t time.Time) int64 {
    12  	return t.UnixNano() / (int64(time.Millisecond) / int64(time.Nanosecond))
    13  }
    14  
    15  // unmarshalMetadata unmarshals the given meta to the target
    16  func unmarshalMetadata(meta map[string]interface{}, target interface{}) error {
    17  	b, err := json.Marshal(meta)
    18  	if err != nil {
    19  		return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
    20  	}
    21  
    22  	err = json.Unmarshal(b, target)
    23  	if err != nil {
    24  		return crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
    25  	}
    26  
    27  	return nil
    28  }
    29  
    30  // marshalMetadata marshals the given interface to map[string]interface{}
    31  func marshalMetadata(o interface{}) (meta map[string]interface{}, err error) {
    32  	b, err := json.Marshal(o)
    33  	if err != nil {
    34  		return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
    35  	}
    36  	meta = make(map[string]interface{})
    37  	err = json.Unmarshal(b, &meta)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  
    42  	return
    43  }