arg.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }