marshal.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "fmt"
  7. "time"
  8. )
  9. // Marshaler is the interface implemented by objects that can marshal
  10. // themselves into values understood by Cassandra.
  11. type Marshaler interface {
  12. MarshalCQL(info *TypeInfo, value interface{}) ([]byte, error)
  13. }
  14. // Unmarshaler is the interface implemented by objects that can unmarshal
  15. // a Cassandra specific description of themselves.
  16. type Unmarshaler interface {
  17. UnmarshalCQL(info *TypeInfo, data []byte, value interface{}) error
  18. }
  19. // Marshal returns the CQL encoding of the value for the Cassandra
  20. // internal type described by the info parameter.
  21. func Marshal(info *TypeInfo, value interface{}) ([]byte, error) {
  22. if v, ok := value.(Marshaler); ok {
  23. return v.MarshalCQL(info, value)
  24. }
  25. switch info.Type {
  26. case TypeVarchar, TypeAscii, TypeBlob:
  27. switch v := value.(type) {
  28. case string:
  29. return []byte(v), nil
  30. case []byte:
  31. return v, nil
  32. }
  33. case TypeBoolean:
  34. if v, ok := value.(bool); ok {
  35. if v {
  36. return []byte{1}, nil
  37. } else {
  38. return []byte{0}, nil
  39. }
  40. }
  41. case TypeInt:
  42. switch v := value.(type) {
  43. case int:
  44. x := int32(v)
  45. return []byte{byte(x >> 24), byte(x >> 16), byte(x >> 8), byte(x)}, nil
  46. }
  47. case TypeTimestamp:
  48. if v, ok := value.(time.Time); ok {
  49. x := v.In(time.UTC).UnixNano() / int64(time.Millisecond)
  50. return []byte{byte(x >> 56), byte(x >> 48), byte(x >> 40),
  51. byte(x >> 32), byte(x >> 24), byte(x >> 16),
  52. byte(x >> 8), byte(x)}, nil
  53. }
  54. }
  55. // TODO(tux21b): add reflection and a lot of other types
  56. return nil, fmt.Errorf("can not marshal %T into %s", value, info)
  57. }
  58. // Unmarshal parses the CQL encoded data based on the info parameter that
  59. // describes the Cassandra internal data type and stores the result in the
  60. // value pointed by value.
  61. func Unmarshal(info *TypeInfo, data []byte, value interface{}) error {
  62. if v, ok := value.(Unmarshaler); ok {
  63. return v.UnmarshalCQL(info, data, value)
  64. }
  65. switch info.Type {
  66. case TypeVarchar, TypeAscii, TypeBlob:
  67. switch v := value.(type) {
  68. case *string:
  69. *v = string(data)
  70. return nil
  71. case *[]byte:
  72. val := make([]byte, len(data))
  73. copy(val, data)
  74. *v = val
  75. return nil
  76. }
  77. case TypeBoolean:
  78. if v, ok := value.(*bool); ok && len(data) == 1 {
  79. *v = data[0] != 0
  80. return nil
  81. }
  82. case TypeBigInt:
  83. if v, ok := value.(*int); ok && len(data) == 8 {
  84. *v = int(data[0])<<56 | int(data[1])<<48 | int(data[2])<<40 |
  85. int(data[3])<<32 | int(data[4])<<24 | int(data[5])<<16 |
  86. int(data[6])<<8 | int(data[7])
  87. return nil
  88. }
  89. case TypeInt:
  90. if v, ok := value.(*int); ok && len(data) == 4 {
  91. *v = int(data[0])<<24 | int(data[1])<<16 | int(data[2])<<8 |
  92. int(data[3])
  93. return nil
  94. }
  95. case TypeTimestamp:
  96. if v, ok := value.(*time.Time); ok && len(data) == 8 {
  97. x := int64(data[0])<<56 | int64(data[1])<<48 |
  98. int64(data[2])<<40 | int64(data[3])<<32 |
  99. int64(data[4])<<24 | int64(data[5])<<16 |
  100. int64(data[6])<<8 | int64(data[7])
  101. sec := x / 1000
  102. nsec := (x - sec*1000) * 1000000
  103. *v = time.Unix(sec, nsec)
  104. return nil
  105. }
  106. }
  107. // TODO(tux21b): add reflection and a lot of other basic types
  108. return fmt.Errorf("can not unmarshal %s into %T", info, value)
  109. }
  110. // TypeInfo describes a Cassandra specific data type.
  111. type TypeInfo struct {
  112. Type Type
  113. Key *TypeInfo // only used for TypeMap
  114. Value *TypeInfo // only used for TypeMap, TypeList and TypeSet
  115. Custom string // only used for TypeCostum
  116. }
  117. // String returns a human readable name for the Cassandra datatype
  118. // described by t.
  119. func (t TypeInfo) String() string {
  120. switch t.Type {
  121. case TypeMap:
  122. return fmt.Sprintf("%s(%s, %s)", t.Type, t.Key, t.Value)
  123. case TypeList, TypeSet:
  124. return fmt.Sprintf("%s(%s)", t.Type, t.Value)
  125. case TypeCustom:
  126. return fmt.Sprintf("%s(%s)", t.Type, t.Custom)
  127. }
  128. return t.Type.String()
  129. }
  130. // Type is the identifier of a Cassandra internal datatype.
  131. type Type int
  132. const (
  133. TypeCustom Type = 0x0000
  134. TypeAscii Type = 0x0001
  135. TypeBigInt Type = 0x0002
  136. TypeBlob Type = 0x0003
  137. TypeBoolean Type = 0x0004
  138. TypeCounter Type = 0x0005
  139. TypeDecimal Type = 0x0006
  140. TypeDouble Type = 0x0007
  141. TypeFloat Type = 0x0008
  142. TypeInt Type = 0x0009
  143. TypeTimestamp Type = 0x000B
  144. TypeUUID Type = 0x000C
  145. TypeVarchar Type = 0x000D
  146. TypeVarint Type = 0x000E
  147. TypeTimeUUID Type = 0x000F
  148. TypeInet Type = 0x0010
  149. TypeList Type = 0x0020
  150. TypeMap Type = 0x0021
  151. TypeSet Type = 0x0022
  152. )
  153. // String returns the name of the identifier.
  154. func (t Type) String() string {
  155. switch t {
  156. case TypeCustom:
  157. return "custom"
  158. case TypeAscii:
  159. return "ascii"
  160. case TypeBigInt:
  161. return "bigint"
  162. case TypeBlob:
  163. return "blob"
  164. case TypeBoolean:
  165. return "boolean"
  166. case TypeCounter:
  167. return "counter"
  168. case TypeDecimal:
  169. return "decimal"
  170. case TypeFloat:
  171. return "float"
  172. case TypeInt:
  173. return "int"
  174. case TypeTimestamp:
  175. return "timestamp"
  176. case TypeUUID:
  177. return "uuid"
  178. case TypeVarchar:
  179. return "varchar"
  180. case TypeTimeUUID:
  181. return "timeuuid"
  182. case TypeInet:
  183. return "inet"
  184. case TypeList:
  185. return "list"
  186. case TypeMap:
  187. return "map"
  188. case TypeSet:
  189. return "set"
  190. default:
  191. return "unknown"
  192. }
  193. }