all: remove use of deprecated NewMessage

Replace NewMessage calls with NewField, NewElement, or NewValue.

Change-Id: I6d2bb4f11f0eb2ba7a52308b1addb111137ad4b9
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/193266
Reviewed-by: Joe Tsai <joetsai@google.com>
This commit is contained in:
Damien Neil 2019-09-04 10:46:00 -07:00
parent dfae831ab7
commit d91c422d95
9 changed files with 51 additions and 53 deletions

View File

@ -61,9 +61,9 @@ func fillMessage(m pref.Message, level int) {
func setScalarField(m pref.Message, fd pref.FieldDescriptor, level int) {
switch fd.Kind() {
case pref.MessageKind, pref.GroupKind:
m2 := m.NewMessage(fd)
fillMessage(m2, level+1)
m.Set(fd, pref.ValueOf(m2))
val := m.NewField(fd)
fillMessage(val.Message(), level+1)
m.Set(fd, val)
default:
m.Set(fd, scalarField(fd.Kind()))
}
@ -109,9 +109,9 @@ func setList(list pref.List, fd pref.FieldDescriptor, level int) {
switch fd.Kind() {
case pref.MessageKind, pref.GroupKind:
for i := 0; i < 10; i++ {
m := list.NewMessage()
fillMessage(m, level+1)
list.Append(pref.ValueOf(m))
val := list.NewElement()
fillMessage(val.Message(), level+1)
list.Append(val)
}
default:
for i := 0; i < 100; i++ {
@ -128,9 +128,9 @@ func setMap(mmap pref.Map, fd pref.FieldDescriptor, level int) {
pkey := scalarField(keyDesc.Kind())
switch kind := valDesc.Kind(); kind {
case pref.MessageKind, pref.GroupKind:
m := mmap.NewMessage()
fillMessage(m, level+1)
mmap.Set(pkey.MapKey(), pref.ValueOf(m))
val := mmap.NewValue()
fillMessage(val.Message(), level+1)
mmap.Set(pkey.MapKey(), val)
default:
mmap.Set(pkey.MapKey(), scalarField(kind))
}

View File

@ -276,9 +276,8 @@ func (o UnmarshalOptions) unmarshalSingular(m pref.Message, fd pref.FieldDescrip
var err error
switch fd.Kind() {
case pref.MessageKind, pref.GroupKind:
m2 := m.NewMessage(fd)
err = o.unmarshalMessage(m2, false)
val = pref.ValueOf(m2)
val = m.NewField(fd)
err = o.unmarshalMessage(val.Message(), false)
default:
val, err = o.unmarshalScalar(fd)
}
@ -528,8 +527,8 @@ func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor)
switch fd.Kind() {
case pref.MessageKind, pref.GroupKind:
for {
m := list.NewMessage()
err := o.unmarshalMessage(m, false)
val := list.NewElement()
err := o.unmarshalMessage(val.Message(), false)
if err != nil {
if e, ok := err.(unexpectedJSONError); ok {
if e.value.Type() == json.EndArray {
@ -539,7 +538,7 @@ func (o UnmarshalOptions) unmarshalList(list pref.List, fd pref.FieldDescriptor)
}
return err
}
list.Append(pref.ValueOf(m))
list.Append(val)
}
default:
for {
@ -575,11 +574,11 @@ func (o UnmarshalOptions) unmarshalMap(mmap pref.Map, fd pref.FieldDescriptor) e
switch fd.MapValue().Kind() {
case pref.MessageKind, pref.GroupKind:
unmarshalMapValue = func() (pref.Value, error) {
m := mmap.NewMessage()
if err := o.unmarshalMessage(m, false); err != nil {
val := mmap.NewValue()
if err := o.unmarshalMessage(val.Message(), false); err != nil {
return pref.Value{}, err
}
return pref.ValueOf(m), nil
return val, nil
}
default:
unmarshalMapValue = func() (pref.Value, error) {

View File

@ -587,19 +587,19 @@ func (o UnmarshalOptions) unmarshalKnownValue(m pref.Message) error {
case json.StartObject:
fd := m.Descriptor().Fields().ByNumber(fieldnum.Value_StructValue)
m2 := m.NewMessage(fd)
if err := o.unmarshalStruct(m2); err != nil {
val := m.NewField(fd)
if err := o.unmarshalStruct(val.Message()); err != nil {
return err
}
m.Set(fd, pref.ValueOf(m2))
m.Set(fd, val)
case json.StartArray:
fd := m.Descriptor().Fields().ByNumber(fieldnum.Value_ListValue)
m2 := m.NewMessage(fd)
if err := o.unmarshalListValue(m2); err != nil {
val := m.NewField(fd)
if err := o.unmarshalListValue(val.Message()); err != nil {
return err
}
m.Set(fd, pref.ValueOf(m2))
m.Set(fd, val)
default:
jval, err := o.decoder.Read()

View File

@ -209,11 +209,10 @@ func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescr
if input.Type() != text.Message {
return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
}
m2 := m.NewMessage(fd)
if err := o.unmarshalMessage(input.Message(), m2); err != nil {
val = m.NewField(fd)
if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
return err
}
val = pref.ValueOf(m2)
default:
var err error
val, err = unmarshalScalar(input, fd)
@ -299,11 +298,11 @@ func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDes
if input.Type() != text.Message {
return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
}
m := list.NewMessage()
if err := o.unmarshalMessage(input.Message(), m); err != nil {
val := list.NewElement()
if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
return err
}
list.Append(pref.ValueOf(m))
list.Append(val)
}
default:
for _, input := range inputList {
@ -402,11 +401,11 @@ func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.M
if input.Type() != 0 {
value = input.Message()
}
m := mmap.NewMessage()
if err := o.unmarshalMessage(value, m); err != nil {
val := mmap.NewValue()
if err := o.unmarshalMessage(value, val.Message()); err != nil {
return err
}
mmap.Set(pkey, pref.ValueOf(m))
mmap.Set(pkey, val)
return nil
}

View File

@ -330,11 +330,11 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, list protorefl
}
{{end -}}
{{if or (eq .Name "Message") (eq .Name "Group") -}}
m := list.NewMessage()
if err := o.unmarshalMessage(v, m); err != nil {
m := list.NewElement()
if err := o.unmarshalMessage(v, m.Message()); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
list.Append(m)
{{- else -}}
list.Append({{.ToValue}})
{{- end}}

View File

@ -1326,9 +1326,9 @@ func testLists(t *testing.T, p path, v pref.List, tt listOps) {
v.Append(e)
}
case appendMessageList:
m := v.NewMessage()
v.Append(V(m))
testMessage(t, p, m, messageOps(op))
e := v.NewElement()
v.Append(e)
testMessage(t, p, e.Message(), messageOps(op))
case truncList:
v.Truncate(int(op))
default:
@ -1380,7 +1380,7 @@ func testMaps(t *testing.T, p path, m pref.Map, tt mapOps) {
for k, tt := range op {
mk := V(k).MapKey()
if !m.Has(mk) {
m.Set(mk, V(m.NewMessage()))
m.Set(mk, m.NewValue())
}
testMessage(t, p, m.Get(mk).Message(), tt)
}

View File

@ -154,7 +154,7 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protorefle
)
switch valField.Kind() {
case protoreflect.GroupKind, protoreflect.MessageKind:
val = protoreflect.ValueOf(mapv.NewMessage())
val = mapv.NewValue()
}
// Map entries are represented as a two-element message with fields
// containing the key and value.

View File

@ -574,11 +574,11 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, list protorefl
if n < 0 {
return 0, wire.ParseError(n)
}
m := list.NewMessage()
if err := o.unmarshalMessage(v, m); err != nil {
m := list.NewElement()
if err := o.unmarshalMessage(v, m.Message()); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
list.Append(m)
return n, nil
case protoreflect.GroupKind:
if wtyp != wire.StartGroupType {
@ -588,11 +588,11 @@ func (o UnmarshalOptions) unmarshalList(b []byte, wtyp wire.Type, list protorefl
if n < 0 {
return 0, wire.ParseError(n)
}
m := list.NewMessage()
if err := o.unmarshalMessage(v, m); err != nil {
m := list.NewElement()
if err := o.unmarshalMessage(v, m.Message()); err != nil {
return 0, err
}
list.Append(protoreflect.ValueOf(m))
list.Append(m)
return n, nil
default:
return 0, errUnknown

View File

@ -46,9 +46,9 @@ func mergeList(dst, src protoreflect.List, fd protoreflect.FieldDescriptor) {
for i := 0; i < src.Len(); i++ {
switch v := src.Get(i); {
case fd.Message() != nil:
m := dst.NewMessage()
mergeMessage(m, v.Message())
dst.Append(protoreflect.ValueOf(m))
dstv := dst.NewElement()
mergeMessage(dstv.Message(), v.Message())
dst.Append(dstv)
case fd.Kind() == protoreflect.BytesKind:
dst.Append(cloneBytes(v))
default:
@ -61,9 +61,9 @@ func mergeMap(dst, src protoreflect.Map, fd protoreflect.FieldDescriptor) {
src.Range(func(k protoreflect.MapKey, v protoreflect.Value) bool {
switch {
case fd.Message() != nil:
m := dst.NewMessage()
mergeMessage(m, v.Message())
dst.Set(k, protoreflect.ValueOf(m)) // may replace existing entry
dstv := dst.NewValue()
mergeMessage(dstv.Message(), v.Message())
dst.Set(k, dstv) // may replace existing entry
case fd.Kind() == protoreflect.BytesKind:
dst.Set(k, cloneBytes(v))
default: