You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 line
1.3 KiB

  1. package internal
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/redis/go-redis/v9/internal/util"
  7. )
  8. func AppendArg(b []byte, v interface{}) []byte {
  9. switch v := v.(type) {
  10. case nil:
  11. return append(b, "<nil>"...)
  12. case string:
  13. return appendUTF8String(b, util.StringToBytes(v))
  14. case []byte:
  15. return appendUTF8String(b, v)
  16. case int:
  17. return strconv.AppendInt(b, int64(v), 10)
  18. case int8:
  19. return strconv.AppendInt(b, int64(v), 10)
  20. case int16:
  21. return strconv.AppendInt(b, int64(v), 10)
  22. case int32:
  23. return strconv.AppendInt(b, int64(v), 10)
  24. case int64:
  25. return strconv.AppendInt(b, v, 10)
  26. case uint:
  27. return strconv.AppendUint(b, uint64(v), 10)
  28. case uint8:
  29. return strconv.AppendUint(b, uint64(v), 10)
  30. case uint16:
  31. return strconv.AppendUint(b, uint64(v), 10)
  32. case uint32:
  33. return strconv.AppendUint(b, uint64(v), 10)
  34. case uint64:
  35. return strconv.AppendUint(b, v, 10)
  36. case float32:
  37. return strconv.AppendFloat(b, float64(v), 'f', -1, 64)
  38. case float64:
  39. return strconv.AppendFloat(b, v, 'f', -1, 64)
  40. case bool:
  41. if v {
  42. return append(b, "true"...)
  43. }
  44. return append(b, "false"...)
  45. case time.Time:
  46. return v.AppendFormat(b, time.RFC3339Nano)
  47. default:
  48. return append(b, fmt.Sprint(v)...)
  49. }
  50. }
  51. func appendUTF8String(dst []byte, src []byte) []byte {
  52. dst = append(dst, src...)
  53. return dst
  54. }