default_enc.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2012-2018 The NATS Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package builtin
  14. import (
  15. "bytes"
  16. "fmt"
  17. "reflect"
  18. "strconv"
  19. "unsafe"
  20. )
  21. // DefaultEncoder implementation for EncodedConn.
  22. // This encoder will leave []byte and string untouched, but will attempt to
  23. // turn numbers into appropriate strings that can be decoded. It will also
  24. // propely encoded and decode bools. If will encode a struct, but if you want
  25. // to properly handle structures you should use JsonEncoder.
  26. type DefaultEncoder struct {
  27. // Empty
  28. }
  29. var trueB = []byte("true")
  30. var falseB = []byte("false")
  31. var nilB = []byte("")
  32. // Encode
  33. func (je *DefaultEncoder) Encode(subject string, v interface{}) ([]byte, error) {
  34. switch arg := v.(type) {
  35. case string:
  36. bytes := *(*[]byte)(unsafe.Pointer(&arg))
  37. return bytes, nil
  38. case []byte:
  39. return arg, nil
  40. case bool:
  41. if arg {
  42. return trueB, nil
  43. } else {
  44. return falseB, nil
  45. }
  46. case nil:
  47. return nilB, nil
  48. default:
  49. var buf bytes.Buffer
  50. fmt.Fprintf(&buf, "%+v", arg)
  51. return buf.Bytes(), nil
  52. }
  53. }
  54. // Decode
  55. func (je *DefaultEncoder) Decode(subject string, data []byte, vPtr interface{}) error {
  56. // Figure out what it's pointing to...
  57. sData := *(*string)(unsafe.Pointer(&data))
  58. switch arg := vPtr.(type) {
  59. case *string:
  60. *arg = sData
  61. return nil
  62. case *[]byte:
  63. *arg = data
  64. return nil
  65. case *int:
  66. n, err := strconv.ParseInt(sData, 10, 64)
  67. if err != nil {
  68. return err
  69. }
  70. *arg = int(n)
  71. return nil
  72. case *int32:
  73. n, err := strconv.ParseInt(sData, 10, 64)
  74. if err != nil {
  75. return err
  76. }
  77. *arg = int32(n)
  78. return nil
  79. case *int64:
  80. n, err := strconv.ParseInt(sData, 10, 64)
  81. if err != nil {
  82. return err
  83. }
  84. *arg = int64(n)
  85. return nil
  86. case *float32:
  87. n, err := strconv.ParseFloat(sData, 32)
  88. if err != nil {
  89. return err
  90. }
  91. *arg = float32(n)
  92. return nil
  93. case *float64:
  94. n, err := strconv.ParseFloat(sData, 64)
  95. if err != nil {
  96. return err
  97. }
  98. *arg = float64(n)
  99. return nil
  100. case *bool:
  101. b, err := strconv.ParseBool(sData)
  102. if err != nil {
  103. return err
  104. }
  105. *arg = b
  106. return nil
  107. default:
  108. vt := reflect.TypeOf(arg).Elem()
  109. return fmt.Errorf("nats: Default Encoder can't decode to type %s", vt)
  110. }
  111. }