k is the key of a map. Encode it as a string. Only strings, integers (signed or unsigned), and types that implement encoding.TextMarshaler are supported.
(k reflect.Value)
| 250 | // Only strings, integers (signed or unsigned), and types that implement |
| 251 | // encoding.TextMarshaler are supported. |
| 252 | func stringifyMapKey(k reflect.Value) (string, error) { |
| 253 | // This is basically reflectWithString.resolve, from encoding/json/encode.go. |
| 254 | if k.Kind() == reflect.String { |
| 255 | return k.String(), nil |
| 256 | } |
| 257 | if tm, ok := k.Interface().(encoding.TextMarshaler); ok { |
| 258 | b, err := tm.MarshalText() |
| 259 | if err != nil { |
| 260 | return "", err |
| 261 | } |
| 262 | return string(b), nil |
| 263 | } |
| 264 | switch k.Kind() { |
| 265 | case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 266 | return strconv.FormatInt(k.Int(), 10), nil |
| 267 | case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 268 | return strconv.FormatUint(k.Uint(), 10), nil |
| 269 | default: |
| 270 | return "", gcerr.Newf(gcerr.InvalidArgument, nil, "cannot encode key %v of type %s", k, k.Type()) |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | func encodeStructWithFields(v reflect.Value, fields fields.List, e Encoder) error { |
| 275 | e2 := e.EncodeMap(len(fields)) |
no test coverage detected