github.com/s7techlab/cckit@v0.10.5/convert/convert.go (about) 1 // Package convert for transforming between json serialized []byte and go structs 2 package convert 3 4 import ( 5 "time" 6 7 "github.com/golang/protobuf/ptypes/timestamp" 8 "github.com/pkg/errors" 9 ) 10 11 var ( 12 // ErrUnableToConvertNilToStruct - nil cannot be converted to struct 13 ErrUnableToConvertNilToStruct = errors.New(`unable to convert nil to [struct,array,slice,ptr]`) 14 // ErrUnableToConvertValueToStruct - value cannot be converted to struct 15 ErrUnableToConvertValueToStruct = errors.New(`unable to convert value to struct`) 16 ) 17 18 const TypeInt = 1 19 const TypeString = `` 20 const TypeBool = true 21 22 type ( 23 // FromByter interface supports FromBytes func for converting from slice of bytes to target type 24 FromByter interface { 25 FromBytes([]byte) (interface{}, error) 26 } 27 28 // ToByter interface supports ToBytes func for converting to slice of bytes from source type 29 ToByter interface { 30 ToBytes() ([]byte, error) 31 } 32 ) 33 34 // TimestampToTime converts timestamp to time.Time 35 func TimestampToTime(ts *timestamp.Timestamp) time.Time { 36 return time.Unix(ts.GetSeconds(), int64(ts.GetNanos())) 37 }