2018-11-05 19:42:22 +00:00
|
|
|
// 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.
|
|
|
|
|
2019-07-17 23:52:10 +00:00
|
|
|
package impl
|
2018-11-05 19:42:22 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-17 23:52:10 +00:00
|
|
|
"fmt"
|
2018-11-05 19:42:22 +00:00
|
|
|
"reflect"
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
2018-11-05 19:42:22 +00:00
|
|
|
)
|
|
|
|
|
2019-07-17 23:52:10 +00:00
|
|
|
type mapConverter struct {
|
2020-04-22 20:49:10 +00:00
|
|
|
goType reflect.Type // map[K]V
|
2019-07-17 23:52:10 +00:00
|
|
|
keyConv, valConv Converter
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func newMapConverter(t reflect.Type, fd protoreflect.FieldDescriptor) *mapConverter {
|
2019-07-17 23:52:10 +00:00
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *mapConverter) PBValueOf(v reflect.Value) protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
if v.Type() != c.goType {
|
|
|
|
panic(fmt.Sprintf("invalid type: got %v, want %v", v.Type(), c.goType))
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.ValueOfMap(&mapReflect{v, c.keyConv, c.valConv})
|
2019-07-17 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *mapConverter) GoValueOf(v protoreflect.Value) reflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
return v.Map().(*mapReflect).v
|
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *mapConverter) IsValidPB(v protoreflect.Value) bool {
|
2019-08-29 21:08:28 +00:00
|
|
|
mapv, ok := v.Interface().(*mapReflect)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
2020-04-22 20:49:10 +00:00
|
|
|
return mapv.v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mapConverter) IsValidGo(v reflect.Value) bool {
|
2020-04-22 20:49:10 +00:00
|
|
|
return v.IsValid() && v.Type() == c.goType
|
2019-08-29 21:08:28 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *mapConverter) New() protoreflect.Value {
|
2019-07-17 23:52:10 +00:00
|
|
|
return c.PBValueOf(reflect.MakeMap(c.goType))
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 20:12:23 +00:00
|
|
|
func (c *mapConverter) Zero() protoreflect.Value {
|
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-07 19:21:41 +00:00
|
|
|
return c.PBValueOf(reflect.Zero(c.goType))
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:42:22 +00:00
|
|
|
type mapReflect struct {
|
2019-07-17 23:52:10 +00:00
|
|
|
v reflect.Value // map[K]V
|
2018-11-05 19:42:22 +00:00
|
|
|
keyConv Converter
|
|
|
|
valConv Converter
|
|
|
|
}
|
|
|
|
|
2019-04-26 06:48:08 +00:00
|
|
|
func (ms *mapReflect) Len() int {
|
2019-07-17 23:52:10 +00:00
|
|
|
return ms.v.Len()
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Has(k protoreflect.MapKey) bool {
|
2018-11-05 19:42:22 +00:00
|
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
2019-07-17 23:52:10 +00:00
|
|
|
rv := ms.v.MapIndex(rk)
|
2018-11-05 19:42:22 +00:00
|
|
|
return rv.IsValid()
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Get(k protoreflect.MapKey) protoreflect.Value {
|
2018-11-05 19:42:22 +00:00
|
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
2019-07-17 23:52:10 +00:00
|
|
|
rv := ms.v.MapIndex(rk)
|
2018-11-05 19:42:22 +00:00
|
|
|
if !rv.IsValid() {
|
2022-05-23 20:12:23 +00:00
|
|
|
return protoreflect.Value{}
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
|
|
|
return ms.valConv.PBValueOf(rv)
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Set(k protoreflect.MapKey, v protoreflect.Value) {
|
2018-11-05 19:42:22 +00:00
|
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
|
|
|
rv := ms.valConv.GoValueOf(v)
|
2019-07-17 23:52:10 +00:00
|
|
|
ms.v.SetMapIndex(rk, rv)
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Clear(k protoreflect.MapKey) {
|
2018-11-05 19:42:22 +00:00
|
|
|
rk := ms.keyConv.GoValueOf(k.Value())
|
2019-07-17 23:52:10 +00:00
|
|
|
ms.v.SetMapIndex(rk, reflect.Value{})
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Mutable(k protoreflect.MapKey) protoreflect.Value {
|
2020-02-19 18:48:36 +00:00
|
|
|
if _, ok := ms.valConv.(*messageConverter); !ok {
|
|
|
|
panic("invalid Mutable on map with non-message value type")
|
|
|
|
}
|
|
|
|
v := ms.Get(k)
|
|
|
|
if !v.IsValid() {
|
|
|
|
v = ms.NewValue()
|
|
|
|
ms.Set(k, v)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) Range(f func(protoreflect.MapKey, protoreflect.Value) bool) {
|
2019-08-22 18:41:32 +00:00
|
|
|
iter := mapRange(ms.v)
|
|
|
|
for iter.Next() {
|
|
|
|
k := ms.keyConv.PBValueOf(iter.Key()).MapKey()
|
|
|
|
v := ms.valConv.PBValueOf(iter.Value())
|
|
|
|
if !f(k, v) {
|
|
|
|
return
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-23 20:12:23 +00:00
|
|
|
func (ms *mapReflect) NewValue() protoreflect.Value {
|
all: add NewField, NewElement, NewValue
Add methods to protoreflect.{Message,List,Map} to constrict values
assignable to a message field, list element, or map value. These
methods return the default value for scalar fields, the zero value for
scalar list elements and map values, and an empty, mutable value for
messages, lists, and maps.
Deprecate the NewMessage methods on these types, which are superseded.
Updates golang/protobuf#879
Change-Id: I0f064f60c89a239330ccea81523f559f14fd2c4f
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/188997
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-05 17:48:38 +00:00
|
|
|
return ms.valConv.New()
|
2018-12-07 22:28:33 +00:00
|
|
|
}
|
2019-11-26 21:27:24 +00:00
|
|
|
func (ms *mapReflect) IsValid() bool {
|
|
|
|
return !ms.v.IsNil()
|
|
|
|
}
|
2019-09-03 23:30:39 +00:00
|
|
|
func (ms *mapReflect) protoUnwrap() interface{} {
|
2018-12-01 12:57:09 +00:00
|
|
|
return ms.v.Interface()
|
2018-11-05 19:42:22 +00:00
|
|
|
}
|