readableString converts an arbitrary value v to a string. readableString does a basically same thing as fmt.Sprintf("%q", v), but the difference is that map keys are ordered in alphabetical order so that the results are deterministic.
(v interface{})
| 15 | // the difference is that map keys are ordered in alphabetical order so that |
| 16 | // the results are deterministic. |
| 17 | func readableString(v interface{}) string { |
| 18 | switch v := v.(type) { |
| 19 | case []interface{}: |
| 20 | vals := []string{} |
| 21 | for _, val := range v { |
| 22 | vals = append(vals, readableString(val)) |
| 23 | } |
| 24 | return "[" + strings.Join(vals, " ") + "]" |
| 25 | case map[interface{}]interface{}: |
| 26 | keys := []string{} |
| 27 | // Assume that keys are strings. |
| 28 | for k := range v { |
| 29 | keys = append(keys, k.(string)) |
| 30 | } |
| 31 | sort.Strings(keys) |
| 32 | vals := []string{} |
| 33 | for _, k := range keys { |
| 34 | val := v[k] |
| 35 | vals = append(vals, fmt.Sprintf("%q:", k)+readableString(val)) |
| 36 | } |
| 37 | return "map[" + strings.Join(vals, " ") + "]" |
| 38 | case string, []byte: |
| 39 | return fmt.Sprintf("%q", v) |
| 40 | case uint64: |
| 41 | return fmt.Sprintf("%d", v) |
| 42 | default: |
| 43 | panic(fmt.Sprintf("not supported type: %T", v)) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // CborBinaryToReadableString converts a CBOR binary to a readable string. |
| 48 | func CborBinaryToReadableString(b []byte) (string, error) { |
no outgoing calls
no test coverage detected