MCPcopy Create free account
hub / github.com/google/go-cloud / stringifyMapKey

Function stringifyMapKey

docstore/driver/codec.go:252–272  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

250// Only strings, integers (signed or unsigned), and types that implement
251// encoding.TextMarshaler are supported.
252func 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
274func encodeStructWithFields(v reflect.Value, fields fields.List, e Encoder) error {
275 e2 := e.EncodeMap(len(fields))

Callers 1

encodeMapFunction · 0.85

Calls 4

NewfFunction · 0.92
StringMethod · 0.65
MarshalTextMethod · 0.45
TypeMethod · 0.45

Tested by

no test coverage detected