ToString converts an interface to string in a quick way
(data interface{})
| 32 | |
| 33 | // ToString converts an interface to string in a quick way |
| 34 | func ToString(data interface{}) string { |
| 35 | switch s := data.(type) { |
| 36 | case nil: |
| 37 | return "" |
| 38 | case string: |
| 39 | return s |
| 40 | case bool: |
| 41 | return strconv.FormatBool(s) |
| 42 | case float64: |
| 43 | return strconv.FormatFloat(s, 'f', -1, 64) |
| 44 | case float32: |
| 45 | return strconv.FormatFloat(float64(s), 'f', -1, 32) |
| 46 | case int: |
| 47 | return strconv.Itoa(s) |
| 48 | case int64: |
| 49 | return strconv.FormatInt(s, 10) |
| 50 | case int32: |
| 51 | return strconv.Itoa(int(s)) |
| 52 | case int16: |
| 53 | return strconv.FormatInt(int64(s), 10) |
| 54 | case int8: |
| 55 | return strconv.FormatInt(int64(s), 10) |
| 56 | case uint: |
| 57 | return strconv.FormatUint(uint64(s), 10) |
| 58 | case uint64: |
| 59 | return strconv.FormatUint(s, 10) |
| 60 | case uint32: |
| 61 | return strconv.FormatUint(uint64(s), 10) |
| 62 | case uint16: |
| 63 | return strconv.FormatUint(uint64(s), 10) |
| 64 | case uint8: |
| 65 | return strconv.FormatUint(uint64(s), 10) |
| 66 | case []byte: |
| 67 | return string(s) |
| 68 | case severity.Holder: |
| 69 | return s.Severity.String() |
| 70 | case severity.Severity: |
| 71 | return s.String() |
| 72 | case fmt.Stringer: |
| 73 | return s.String() |
| 74 | case error: |
| 75 | return s.Error() |
| 76 | default: |
| 77 | return fmt.Sprintf("%v", data) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // ToStringNSlice converts an interface to string in a quick way or to a slice with strings |
| 82 | // if the input is a slice of interfaces. |
no test coverage detected