enum.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package column
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/ClickHouse/clickhouse-go/lib/binary"
  7. )
  8. type Enum struct {
  9. iv map[string]interface{}
  10. vi map[interface{}]string
  11. base
  12. baseType interface{}
  13. }
  14. func (enum *Enum) Read(decoder *binary.Decoder, isNull bool) (interface{}, error) {
  15. var (
  16. err error
  17. ident interface{}
  18. )
  19. switch enum.baseType.(type) {
  20. case int16:
  21. if ident, err = decoder.Int16(); err != nil {
  22. return nil, err
  23. }
  24. default:
  25. if ident, err = decoder.Int8(); err != nil {
  26. return nil, err
  27. }
  28. }
  29. if ident, found := enum.vi[ident]; found || isNull {
  30. return ident, nil
  31. }
  32. return nil, fmt.Errorf("invalid Enum value: %v", ident)
  33. }
  34. func (enum *Enum) Write(encoder *binary.Encoder, v interface{}) error {
  35. switch v := v.(type) {
  36. case string:
  37. return enum.encodeFromString(v, encoder)
  38. case uint8:
  39. if _, ok := enum.baseType.(int8); ok {
  40. return encoder.Int8(int8(v))
  41. }
  42. case int8:
  43. if _, ok := enum.baseType.(int8); ok {
  44. return encoder.Int8(v)
  45. }
  46. case uint16:
  47. if _, ok := enum.baseType.(int16); ok {
  48. return encoder.Int16(int16(v))
  49. }
  50. case int16:
  51. if _, ok := enum.baseType.(int16); ok {
  52. return encoder.Int16(v)
  53. }
  54. case int64:
  55. switch enum.baseType.(type) {
  56. case int8:
  57. return encoder.Int8(int8(v))
  58. case int16:
  59. return encoder.Int16(int16(v))
  60. }
  61. // nullable enums
  62. case *string:
  63. return enum.encodeFromString(*v, encoder)
  64. case *uint8:
  65. if _, ok := enum.baseType.(int8); ok {
  66. return encoder.Int8(int8(*v))
  67. }
  68. case *int8:
  69. if _, ok := enum.baseType.(int8); ok {
  70. return encoder.Int8(*v)
  71. }
  72. case *uint16:
  73. if _, ok := enum.baseType.(int16); ok {
  74. return encoder.Int16(int16(*v))
  75. }
  76. case *int16:
  77. if _, ok := enum.baseType.(int16); ok {
  78. return encoder.Int16(*v)
  79. }
  80. case *int64:
  81. switch enum.baseType.(type) {
  82. case int8:
  83. return encoder.Int8(int8(*v))
  84. case int16:
  85. return encoder.Int16(int16(*v))
  86. }
  87. }
  88. return &ErrUnexpectedType{
  89. T: v,
  90. Column: enum,
  91. }
  92. }
  93. func (enum *Enum) encodeFromString(v string, encoder *binary.Encoder) error {
  94. ident, found := enum.iv[v]
  95. if !found {
  96. return fmt.Errorf("invalid Enum ident: %s", v)
  97. }
  98. switch ident := ident.(type) {
  99. case int8:
  100. return encoder.Int8(ident)
  101. case int16:
  102. return encoder.Int16(ident)
  103. default:
  104. return &ErrUnexpectedType{
  105. T: ident,
  106. Column: enum,
  107. }
  108. }
  109. }
  110. func (enum *Enum) defaultValue() interface{} {
  111. return enum.baseType
  112. }
  113. func parseEnum(name, chType string) (*Enum, error) {
  114. var (
  115. data string
  116. isEnum16 bool
  117. )
  118. if len(chType) < 8 {
  119. return nil, fmt.Errorf("invalid Enum format: %s", chType)
  120. }
  121. switch {
  122. case strings.HasPrefix(chType, "Enum8"):
  123. data = chType[6:]
  124. case strings.HasPrefix(chType, "Enum16"):
  125. data = chType[7:]
  126. isEnum16 = true
  127. default:
  128. return nil, fmt.Errorf("'%s' is not Enum type", chType)
  129. }
  130. enum := Enum{
  131. base: base{
  132. name: name,
  133. chType: chType,
  134. valueOf: columnBaseTypes[string("")],
  135. },
  136. iv: make(map[string]interface{}),
  137. vi: make(map[interface{}]string),
  138. }
  139. for _, block := range strings.Split(data[:len(data)-1], ",") {
  140. parts := strings.Split(block, "=")
  141. if len(parts) != 2 {
  142. return nil, fmt.Errorf("invalid Enum format: %s", chType)
  143. }
  144. var (
  145. ident = strings.TrimSpace(parts[0])
  146. value, err = strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 16)
  147. )
  148. if err != nil {
  149. return nil, fmt.Errorf("invalid Enum value: %v", chType)
  150. }
  151. {
  152. var (
  153. ident = ident[1 : len(ident)-1]
  154. value interface{} = int16(value)
  155. )
  156. if !isEnum16 {
  157. value = int8(value.(int16))
  158. }
  159. if enum.baseType == nil {
  160. enum.baseType = value
  161. }
  162. enum.iv[ident] = value
  163. enum.vi[value] = ident
  164. }
  165. }
  166. return &enum, nil
  167. }