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