protobuf-go/internal/impl/convert_map.go
Damien Neil d4f0800c42 all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.

Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.

Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.

Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)

Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-08 18:20:25 +00:00

100 lines
2.4 KiB
Go

// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package impl
import (
"fmt"
"reflect"
pref "google.golang.org/protobuf/reflect/protoreflect"
)
type mapConverter struct {
goType reflect.Type
keyConv, valConv Converter
}
func newMapConverter(t reflect.Type, fd pref.FieldDescriptor) Converter {
if t.Kind() != reflect.Map {
panic(fmt.Sprintf("invalid Go type %v for field %v", t, fd.FullName()))
}
return &mapConverter{
goType: t,
keyConv: newSingularConverter(t.Key(), fd.MapKey()),
valConv: newSingularConverter(t.Elem(), fd.MapValue()),
}
}
func (c *mapConverter) PBValueOf(v reflect.Value) pref.Value {
if v.Type() != c.goType {
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
}
return pref.ValueOf(&mapReflect{v, c.keyConv, c.valConv})
}
func (c *mapConverter) GoValueOf(v pref.Value) reflect.Value {
return v.Map().(*mapReflect).v
}
func (c *mapConverter) New() pref.Value {
return c.PBValueOf(reflect.MakeMap(c.goType))
}
func (c *mapConverter) Zero() pref.Value {
return c.PBValueOf(reflect.Zero(c.goType))
}
type mapReflect struct {
v reflect.Value // map[K]V
keyConv Converter
valConv Converter
}
func (ms *mapReflect) Len() int {
return ms.v.Len()
}
func (ms *mapReflect) Has(k pref.MapKey) bool {
rk := ms.keyConv.GoValueOf(k.Value())
rv := ms.v.MapIndex(rk)
return rv.IsValid()
}
func (ms *mapReflect) Get(k pref.MapKey) pref.Value {
rk := ms.keyConv.GoValueOf(k.Value())
rv := ms.v.MapIndex(rk)
if !rv.IsValid() {
return pref.Value{}
}
return ms.valConv.PBValueOf(rv)
}
func (ms *mapReflect) Set(k pref.MapKey, v pref.Value) {
rk := ms.keyConv.GoValueOf(k.Value())
rv := ms.valConv.GoValueOf(v)
ms.v.SetMapIndex(rk, rv)
}
func (ms *mapReflect) Clear(k pref.MapKey) {
rk := ms.keyConv.GoValueOf(k.Value())
ms.v.SetMapIndex(rk, reflect.Value{})
}
func (ms *mapReflect) Range(f func(pref.MapKey, pref.Value) bool) {
for _, k := range ms.v.MapKeys() {
if v := ms.v.MapIndex(k); v.IsValid() {
pk := ms.keyConv.PBValueOf(k).MapKey()
pv := ms.valConv.PBValueOf(v)
if !f(pk, pv) {
return
}
}
}
}
func (ms *mapReflect) NewMessage() pref.Message {
return ms.NewValue().Message()
}
func (ms *mapReflect) NewValue() pref.Value {
return ms.valConv.New()
}
func (ms *mapReflect) ProtoUnwrap() interface{} {
return ms.v.Interface()
}