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.

57 lines
1.2 KiB

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