numeric.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "math"
  22. "strconv"
  23. )
  24. type Numeric interface {
  25. Int64() int64
  26. Int32() int32
  27. Int16() int16
  28. Byte() byte
  29. Int() int
  30. Float64() float64
  31. Float32() float32
  32. String() string
  33. isNull() bool
  34. }
  35. type numeric struct {
  36. iValue int64
  37. dValue float64
  38. sValue string
  39. isNil bool
  40. }
  41. var (
  42. INFINITY Numeric
  43. NEGATIVE_INFINITY Numeric
  44. NAN Numeric
  45. ZERO Numeric
  46. NUMERIC_NULL Numeric
  47. )
  48. func NewNumericFromDouble(dValue float64) Numeric {
  49. if math.IsInf(dValue, 1) {
  50. return INFINITY
  51. }
  52. if math.IsInf(dValue, -1) {
  53. return NEGATIVE_INFINITY
  54. }
  55. if math.IsNaN(dValue) {
  56. return NAN
  57. }
  58. iValue := int64(dValue)
  59. sValue := strconv.FormatFloat(dValue, 'g', 10, 64)
  60. isNil := false
  61. return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
  62. }
  63. func NewNumericFromI64(iValue int64) Numeric {
  64. dValue := float64(iValue)
  65. sValue := strconv.FormatInt(iValue, 10)
  66. isNil := false
  67. return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
  68. }
  69. func NewNumericFromI32(iValue int32) Numeric {
  70. dValue := float64(iValue)
  71. sValue := strconv.FormatInt(int64(iValue), 10)
  72. isNil := false
  73. return &numeric{iValue: int64(iValue), dValue: dValue, sValue: sValue, isNil: isNil}
  74. }
  75. func NewNumericFromString(sValue string) Numeric {
  76. if sValue == INFINITY.String() {
  77. return INFINITY
  78. }
  79. if sValue == NEGATIVE_INFINITY.String() {
  80. return NEGATIVE_INFINITY
  81. }
  82. if sValue == NAN.String() {
  83. return NAN
  84. }
  85. iValue, _ := strconv.ParseInt(sValue, 10, 64)
  86. dValue, _ := strconv.ParseFloat(sValue, 64)
  87. isNil := len(sValue) == 0
  88. return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNil}
  89. }
  90. func NewNumericFromJSONString(sValue string, isNull bool) Numeric {
  91. if isNull {
  92. return NewNullNumeric()
  93. }
  94. if sValue == JSON_INFINITY {
  95. return INFINITY
  96. }
  97. if sValue == JSON_NEGATIVE_INFINITY {
  98. return NEGATIVE_INFINITY
  99. }
  100. if sValue == JSON_NAN {
  101. return NAN
  102. }
  103. iValue, _ := strconv.ParseInt(sValue, 10, 64)
  104. dValue, _ := strconv.ParseFloat(sValue, 64)
  105. return &numeric{iValue: iValue, dValue: dValue, sValue: sValue, isNil: isNull}
  106. }
  107. func NewNullNumeric() Numeric {
  108. return &numeric{iValue: 0, dValue: 0.0, sValue: "", isNil: true}
  109. }
  110. func (p *numeric) Int64() int64 {
  111. return p.iValue
  112. }
  113. func (p *numeric) Int32() int32 {
  114. return int32(p.iValue)
  115. }
  116. func (p *numeric) Int16() int16 {
  117. return int16(p.iValue)
  118. }
  119. func (p *numeric) Byte() byte {
  120. return byte(p.iValue)
  121. }
  122. func (p *numeric) Int() int {
  123. return int(p.iValue)
  124. }
  125. func (p *numeric) Float64() float64 {
  126. return p.dValue
  127. }
  128. func (p *numeric) Float32() float32 {
  129. return float32(p.dValue)
  130. }
  131. func (p *numeric) String() string {
  132. return p.sValue
  133. }
  134. func (p *numeric) isNull() bool {
  135. return p.isNil
  136. }
  137. func init() {
  138. INFINITY = &numeric{iValue: 0, dValue: math.Inf(1), sValue: "Infinity", isNil: false}
  139. NEGATIVE_INFINITY = &numeric{iValue: 0, dValue: math.Inf(-1), sValue: "-Infinity", isNil: false}
  140. NAN = &numeric{iValue: 0, dValue: math.NaN(), sValue: "NaN", isNil: false}
  141. ZERO = &numeric{iValue: 0, dValue: 0, sValue: "0", isNil: false}
  142. NUMERIC_NULL = &numeric{iValue: 0, dValue: 0, sValue: "0", isNil: true}
  143. }