number.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package msgp
  2. import (
  3. "math"
  4. "strconv"
  5. )
  6. // The portable parts of the Number implementation
  7. // Number can be
  8. // an int64, uint64, float32,
  9. // or float64 internally.
  10. // It can decode itself
  11. // from any of the native
  12. // messagepack number types.
  13. // The zero-value of Number
  14. // is Int(0). Using the equality
  15. // operator with Number compares
  16. // both the type and the value
  17. // of the number.
  18. type Number struct {
  19. // internally, this
  20. // is just a tagged union.
  21. // the raw bits of the number
  22. // are stored the same way regardless.
  23. bits uint64
  24. typ Type
  25. }
  26. // AsInt sets the number to an int64.
  27. func (n *Number) AsInt(i int64) {
  28. // we always store int(0)
  29. // as {0, InvalidType} in
  30. // order to preserve
  31. // the behavior of the == operator
  32. if i == 0 {
  33. n.typ = InvalidType
  34. n.bits = 0
  35. return
  36. }
  37. n.typ = IntType
  38. n.bits = uint64(i)
  39. }
  40. // AsUint sets the number to a uint64.
  41. func (n *Number) AsUint(u uint64) {
  42. n.typ = UintType
  43. n.bits = u
  44. }
  45. // AsFloat32 sets the value of the number
  46. // to a float32.
  47. func (n *Number) AsFloat32(f float32) {
  48. n.typ = Float32Type
  49. n.bits = uint64(math.Float32bits(f))
  50. }
  51. // AsFloat64 sets the value of the
  52. // number to a float64.
  53. func (n *Number) AsFloat64(f float64) {
  54. n.typ = Float64Type
  55. n.bits = math.Float64bits(f)
  56. }
  57. // Int casts the number as an int64, and
  58. // returns whether or not that was the
  59. // underlying type.
  60. func (n *Number) Int() (int64, bool) {
  61. return int64(n.bits), n.typ == IntType || n.typ == InvalidType
  62. }
  63. // Uint casts the number as a uint64, and returns
  64. // whether or not that was the underlying type.
  65. func (n *Number) Uint() (uint64, bool) {
  66. return n.bits, n.typ == UintType
  67. }
  68. // Float casts the number to a float64, and
  69. // returns whether or not that was the underlying
  70. // type (either a float64 or a float32).
  71. func (n *Number) Float() (float64, bool) {
  72. switch n.typ {
  73. case Float32Type:
  74. return float64(math.Float32frombits(uint32(n.bits))), true
  75. case Float64Type:
  76. return math.Float64frombits(n.bits), true
  77. default:
  78. return 0.0, false
  79. }
  80. }
  81. // Type will return one of:
  82. // Float64Type, Float32Type, UintType, or IntType.
  83. func (n *Number) Type() Type {
  84. if n.typ == InvalidType {
  85. return IntType
  86. }
  87. return n.typ
  88. }
  89. // DecodeMsg implements msgp.Decodable
  90. func (n *Number) DecodeMsg(r *Reader) error {
  91. typ, err := r.NextType()
  92. if err != nil {
  93. return err
  94. }
  95. switch typ {
  96. case Float32Type:
  97. f, err := r.ReadFloat32()
  98. if err != nil {
  99. return err
  100. }
  101. n.AsFloat32(f)
  102. return nil
  103. case Float64Type:
  104. f, err := r.ReadFloat64()
  105. if err != nil {
  106. return err
  107. }
  108. n.AsFloat64(f)
  109. return nil
  110. case IntType:
  111. i, err := r.ReadInt64()
  112. if err != nil {
  113. return err
  114. }
  115. n.AsInt(i)
  116. return nil
  117. case UintType:
  118. u, err := r.ReadUint64()
  119. if err != nil {
  120. return err
  121. }
  122. n.AsUint(u)
  123. return nil
  124. default:
  125. return TypeError{Encoded: typ, Method: IntType}
  126. }
  127. }
  128. // UnmarshalMsg implements msgp.Unmarshaler
  129. func (n *Number) UnmarshalMsg(b []byte) ([]byte, error) {
  130. typ := NextType(b)
  131. switch typ {
  132. case IntType:
  133. i, o, err := ReadInt64Bytes(b)
  134. if err != nil {
  135. return b, err
  136. }
  137. n.AsInt(i)
  138. return o, nil
  139. case UintType:
  140. u, o, err := ReadUint64Bytes(b)
  141. if err != nil {
  142. return b, err
  143. }
  144. n.AsUint(u)
  145. return o, nil
  146. case Float64Type:
  147. f, o, err := ReadFloat64Bytes(b)
  148. if err != nil {
  149. return b, err
  150. }
  151. n.AsFloat64(f)
  152. return o, nil
  153. case Float32Type:
  154. f, o, err := ReadFloat32Bytes(b)
  155. if err != nil {
  156. return b, err
  157. }
  158. n.AsFloat32(f)
  159. return o, nil
  160. default:
  161. return b, TypeError{Method: IntType, Encoded: typ}
  162. }
  163. }
  164. // MarshalMsg implements msgp.Marshaler
  165. func (n *Number) MarshalMsg(b []byte) ([]byte, error) {
  166. switch n.typ {
  167. case IntType:
  168. return AppendInt64(b, int64(n.bits)), nil
  169. case UintType:
  170. return AppendUint64(b, uint64(n.bits)), nil
  171. case Float64Type:
  172. return AppendFloat64(b, math.Float64frombits(n.bits)), nil
  173. case Float32Type:
  174. return AppendFloat32(b, math.Float32frombits(uint32(n.bits))), nil
  175. default:
  176. return AppendInt64(b, 0), nil
  177. }
  178. }
  179. // EncodeMsg implements msgp.Encodable
  180. func (n *Number) EncodeMsg(w *Writer) error {
  181. switch n.typ {
  182. case IntType:
  183. return w.WriteInt64(int64(n.bits))
  184. case UintType:
  185. return w.WriteUint64(n.bits)
  186. case Float64Type:
  187. return w.WriteFloat64(math.Float64frombits(n.bits))
  188. case Float32Type:
  189. return w.WriteFloat32(math.Float32frombits(uint32(n.bits)))
  190. default:
  191. return w.WriteInt64(0)
  192. }
  193. }
  194. // Msgsize implements msgp.Sizer
  195. func (n *Number) Msgsize() int {
  196. switch n.typ {
  197. case Float32Type:
  198. return Float32Size
  199. case Float64Type:
  200. return Float64Size
  201. case IntType:
  202. return Int64Size
  203. case UintType:
  204. return Uint64Size
  205. default:
  206. return 1 // fixint(0)
  207. }
  208. }
  209. // MarshalJSON implements json.Marshaler
  210. func (n *Number) MarshalJSON() ([]byte, error) {
  211. t := n.Type()
  212. if t == InvalidType {
  213. return []byte{'0'}, nil
  214. }
  215. out := make([]byte, 0, 32)
  216. switch t {
  217. case Float32Type, Float64Type:
  218. f, _ := n.Float()
  219. return strconv.AppendFloat(out, f, 'f', -1, 64), nil
  220. case IntType:
  221. i, _ := n.Int()
  222. return strconv.AppendInt(out, i, 10), nil
  223. case UintType:
  224. u, _ := n.Uint()
  225. return strconv.AppendUint(out, u, 10), nil
  226. default:
  227. panic("(*Number).typ is invalid")
  228. }
  229. }
  230. // String implements fmt.Stringer
  231. func (n *Number) String() string {
  232. switch n.typ {
  233. case InvalidType:
  234. return "0"
  235. case Float32Type, Float64Type:
  236. f, _ := n.Float()
  237. return strconv.FormatFloat(f, 'f', -1, 64)
  238. case IntType:
  239. i, _ := n.Int()
  240. return strconv.FormatInt(i, 10)
  241. case UintType:
  242. u, _ := n.Uint()
  243. return strconv.FormatUint(u, 10)
  244. default:
  245. panic("(*Number).typ is invalid")
  246. }
  247. }