mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-02-19 03:39:48 +00:00
all: rename IsInitialized as CheckInitialized
An Is prefix implies it returns a boolean. A Check prefix better suggests that it could return an error. Change-Id: I6ffcb32099a944c656c07654c294a0980efb2d0e Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/220338 Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
parent
5e75048cc1
commit
f26a9e7e30
@ -75,7 +75,7 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
|
|||||||
if o.AllowPartial {
|
if o.AllowPartial {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
type decoder struct {
|
type decoder struct {
|
||||||
|
@ -123,7 +123,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
|||||||
if o.AllowPartial {
|
if o.AllowPartial {
|
||||||
return enc.Bytes(), nil
|
return enc.Bytes(), nil
|
||||||
}
|
}
|
||||||
return enc.Bytes(), proto.IsInitialized(m)
|
return enc.Bytes(), proto.CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
type encoder struct {
|
type encoder struct {
|
||||||
|
@ -66,7 +66,7 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
|
|||||||
if o.AllowPartial {
|
if o.AllowPartial {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
type decoder struct {
|
type decoder struct {
|
||||||
|
@ -118,7 +118,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
|
|||||||
if o.AllowPartial {
|
if o.AllowPartial {
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
return out, proto.IsInitialized(m)
|
return out, proto.CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
type encoder struct {
|
type encoder struct {
|
||||||
|
@ -39,7 +39,7 @@ func Fuzz(data []byte) (score int) {
|
|||||||
default:
|
default:
|
||||||
panic("unmarshal ok with validation status: " + valid.String())
|
panic("unmarshal ok with validation status: " + valid.String())
|
||||||
}
|
}
|
||||||
if proto.IsInitialized(m1) != nil && vinit {
|
if proto.CheckInitialized(m1) != nil && vinit {
|
||||||
panic("validation reports partial message is initialized")
|
panic("validation reports partial message is initialized")
|
||||||
}
|
}
|
||||||
data1, err := proto.MarshalOptions{
|
data1, err := proto.MarshalOptions{
|
||||||
|
@ -12,17 +12,17 @@ import (
|
|||||||
piface "google.golang.org/protobuf/runtime/protoiface"
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (mi *MessageInfo) isInitialized(in piface.IsInitializedInput) (piface.IsInitializedOutput, error) {
|
func (mi *MessageInfo) checkInitialized(in piface.CheckInitializedInput) (piface.CheckInitializedOutput, error) {
|
||||||
var p pointer
|
var p pointer
|
||||||
if ms, ok := in.Message.(*messageState); ok {
|
if ms, ok := in.Message.(*messageState); ok {
|
||||||
p = ms.pointer()
|
p = ms.pointer()
|
||||||
} else {
|
} else {
|
||||||
p = in.Message.(*messageReflectWrapper).pointer()
|
p = in.Message.(*messageReflectWrapper).pointer()
|
||||||
}
|
}
|
||||||
return piface.IsInitializedOutput{}, mi.isInitializedPointer(p)
|
return piface.CheckInitializedOutput{}, mi.checkInitializedPointer(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mi *MessageInfo) isInitializedPointer(p pointer) error {
|
func (mi *MessageInfo) checkInitializedPointer(p pointer) error {
|
||||||
mi.init()
|
mi.init()
|
||||||
if !mi.needsInitCheck {
|
if !mi.needsInitCheck {
|
||||||
return nil
|
return nil
|
@ -167,7 +167,7 @@ func makeWeakMessageFieldCoder(fd pref.FieldDescriptor) pointerCoderFuncs {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
},
|
},
|
||||||
merge: func(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
|
merge: func(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
|
||||||
sm, ok := src.WeakFields().get(f.num)
|
sm, ok := src.WeakFields().get(f.num)
|
||||||
@ -219,7 +219,7 @@ func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCode
|
|||||||
},
|
},
|
||||||
isInit: func(p pointer, f *coderFieldInfo) error {
|
isInit: func(p pointer, f *coderFieldInfo) error {
|
||||||
m := asMessage(p.AsValueOf(ft).Elem())
|
m := asMessage(p.AsValueOf(ft).Elem())
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
},
|
},
|
||||||
merge: mergeMessage,
|
merge: mergeMessage,
|
||||||
}
|
}
|
||||||
@ -257,7 +257,7 @@ func consumeMessageInfo(b []byte, p pointer, wtyp wire.Type, f *coderFieldInfo,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isInitMessageInfo(p pointer, f *coderFieldInfo) error {
|
func isInitMessageInfo(p pointer, f *coderFieldInfo) error {
|
||||||
return f.mi.isInitializedPointer(p.Elem())
|
return f.mi.checkInitializedPointer(p.Elem())
|
||||||
}
|
}
|
||||||
|
|
||||||
func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int {
|
func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int {
|
||||||
@ -307,7 +307,7 @@ func consumeMessageValue(b []byte, v pref.Value, _ wire.Number, wtyp wire.Type,
|
|||||||
|
|
||||||
func isInitMessageValue(v pref.Value) error {
|
func isInitMessageValue(v pref.Value) error {
|
||||||
m := v.Message().Interface()
|
m := v.Message().Interface()
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
var coderMessageValue = valueCoderFuncs{
|
var coderMessageValue = valueCoderFuncs{
|
||||||
@ -374,7 +374,7 @@ func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderF
|
|||||||
},
|
},
|
||||||
isInit: func(p pointer, f *coderFieldInfo) error {
|
isInit: func(p pointer, f *coderFieldInfo) error {
|
||||||
m := asMessage(p.AsValueOf(ft).Elem())
|
m := asMessage(p.AsValueOf(ft).Elem())
|
||||||
return proto.IsInitialized(m)
|
return proto.CheckInitialized(m)
|
||||||
},
|
},
|
||||||
merge: mergeMessage,
|
merge: mergeMessage,
|
||||||
}
|
}
|
||||||
@ -509,7 +509,7 @@ func consumeMessageSliceInfo(b []byte, p pointer, wtyp wire.Type, f *coderFieldI
|
|||||||
func isInitMessageSliceInfo(p pointer, f *coderFieldInfo) error {
|
func isInitMessageSliceInfo(p pointer, f *coderFieldInfo) error {
|
||||||
s := p.PointerSlice()
|
s := p.PointerSlice()
|
||||||
for _, v := range s {
|
for _, v := range s {
|
||||||
if err := f.mi.isInitializedPointer(v); err != nil {
|
if err := f.mi.checkInitializedPointer(v); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -567,7 +567,7 @@ func isInitMessageSlice(p pointer, goType reflect.Type) error {
|
|||||||
s := p.PointerSlice()
|
s := p.PointerSlice()
|
||||||
for _, v := range s {
|
for _, v := range s {
|
||||||
m := asMessage(v.AsValueOf(goType.Elem()))
|
m := asMessage(v.AsValueOf(goType.Elem()))
|
||||||
if err := proto.IsInitialized(m); err != nil {
|
if err := proto.CheckInitialized(m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -629,7 +629,7 @@ func isInitMessageSliceValue(listv pref.Value) error {
|
|||||||
list := listv.List()
|
list := listv.List()
|
||||||
for i, llen := 0, list.Len(); i < llen; i++ {
|
for i, llen := 0, list.Len(); i < llen; i++ {
|
||||||
m := list.Get(i).Message().Interface()
|
m := list.Get(i).Message().Interface()
|
||||||
if err := proto.IsInitialized(m); err != nil {
|
if err := proto.CheckInitialized(m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ func isInitMap(mapv reflect.Value, mapi *mapInfo, f *coderFieldInfo) error {
|
|||||||
iter := mapRange(mapv)
|
iter := mapRange(mapv)
|
||||||
for iter.Next() {
|
for iter.Next() {
|
||||||
val := pointerOfValue(iter.Value())
|
val := pointerOfValue(iter.Value())
|
||||||
if err := mi.isInitializedPointer(val); err != nil {
|
if err := mi.checkInitializedPointer(val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -148,8 +148,8 @@ func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
|
|||||||
mi.methods.Flags |= piface.SupportUnmarshalDiscardUnknown
|
mi.methods.Flags |= piface.SupportUnmarshalDiscardUnknown
|
||||||
mi.methods.Unmarshal = mi.unmarshal
|
mi.methods.Unmarshal = mi.unmarshal
|
||||||
}
|
}
|
||||||
if mi.methods.IsInitialized == nil {
|
if mi.methods.CheckInitialized == nil {
|
||||||
mi.methods.IsInitialized = mi.isInitialized
|
mi.methods.CheckInitialized = mi.checkInitialized
|
||||||
}
|
}
|
||||||
if mi.methods.Merge == nil {
|
if mi.methods.Merge == nil {
|
||||||
mi.methods.Merge = mi.merge
|
mi.methods.Merge = mi.merge
|
||||||
|
@ -10,23 +10,28 @@ import (
|
|||||||
"google.golang.org/protobuf/runtime/protoiface"
|
"google.golang.org/protobuf/runtime/protoiface"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsInitialized returns an error if any required fields in m are not set.
|
// Deprecated: Use CheckInitialized instead.
|
||||||
func IsInitialized(m Message) error {
|
func IsInitialized(m Message) error {
|
||||||
return isInitialized(m.ProtoReflect())
|
return CheckInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsInitialized returns an error if any required fields in m are not set.
|
// CheckInitialized returns an error if any required fields in m are not set.
|
||||||
func isInitialized(m protoreflect.Message) error {
|
func CheckInitialized(m Message) error {
|
||||||
if methods := protoMethods(m); methods != nil && methods.IsInitialized != nil {
|
return checkInitialized(m.ProtoReflect())
|
||||||
_, err := methods.IsInitialized(protoiface.IsInitializedInput{
|
}
|
||||||
|
|
||||||
|
// CheckInitialized returns an error if any required fields in m are not set.
|
||||||
|
func checkInitialized(m protoreflect.Message) error {
|
||||||
|
if methods := protoMethods(m); methods != nil && methods.CheckInitialized != nil {
|
||||||
|
_, err := methods.CheckInitialized(protoiface.CheckInitializedInput{
|
||||||
Message: m,
|
Message: m,
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return isInitializedSlow(m)
|
return checkInitializedSlow(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInitializedSlow(m protoreflect.Message) error {
|
func checkInitializedSlow(m protoreflect.Message) error {
|
||||||
md := m.Descriptor()
|
md := m.Descriptor()
|
||||||
fds := md.Fields()
|
fds := md.Fields()
|
||||||
for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ {
|
for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ {
|
||||||
@ -43,21 +48,21 @@ func isInitializedSlow(m protoreflect.Message) error {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
for i, list := 0, v.List(); i < list.Len() && err == nil; i++ {
|
for i, list := 0, v.List(); i < list.Len() && err == nil; i++ {
|
||||||
err = isInitialized(list.Get(i).Message())
|
err = checkInitialized(list.Get(i).Message())
|
||||||
}
|
}
|
||||||
case fd.IsMap():
|
case fd.IsMap():
|
||||||
if fd.MapValue().Message() == nil {
|
if fd.MapValue().Message() == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
v.Map().Range(func(key protoreflect.MapKey, v protoreflect.Value) bool {
|
v.Map().Range(func(key protoreflect.MapKey, v protoreflect.Value) bool {
|
||||||
err = isInitialized(v.Message())
|
err = checkInitialized(v.Message())
|
||||||
return err == nil
|
return err == nil
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
if fd.Message() == nil {
|
if fd.Message() == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
err = isInitialized(v.Message())
|
err = checkInitialized(v.Message())
|
||||||
}
|
}
|
||||||
return err == nil
|
return err == nil
|
||||||
})
|
})
|
@ -17,7 +17,7 @@ import (
|
|||||||
weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
|
weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsInitializedErrors(t *testing.T) {
|
func TestCheckInitializedErrors(t *testing.T) {
|
||||||
type test struct {
|
type test struct {
|
||||||
m proto.Message
|
m proto.Message
|
||||||
want string
|
want string
|
||||||
@ -76,13 +76,13 @@ func TestIsInitializedErrors(t *testing.T) {
|
|||||||
t.SkipNow()
|
t.SkipNow()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := proto.IsInitialized(tt.m)
|
err := proto.CheckInitialized(tt.m)
|
||||||
got := "<nil>"
|
got := "<nil>"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
got = fmt.Sprintf("%q", err)
|
got = fmt.Sprintf("%q", err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(got, tt.want) {
|
if !strings.Contains(got, tt.want) {
|
||||||
t.Errorf("IsInitialized(m):\n got: %v\nwant contains: %v\nMessage:\n%v", got, tt.want, prototext.Format(tt.m))
|
t.Errorf("CheckInitialized(m):\n got: %v\nwant contains: %v\nMessage:\n%v", got, tt.want, prototext.Format(tt.m))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -95,7 +95,7 @@ func (o UnmarshalOptions) unmarshal(b []byte, message Message) (out protoiface.U
|
|||||||
if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
|
if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
return out, isInitialized(m)
|
return out, checkInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
|
func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
|
||||||
|
@ -43,7 +43,7 @@
|
|||||||
//
|
//
|
||||||
// • Reset clears the content of a message.
|
// • Reset clears the content of a message.
|
||||||
//
|
//
|
||||||
// • IsInitialized reports whether all required fields in a message are set.
|
// • CheckInitialized reports whether all required fields in a message are set.
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Optional scalar constructors
|
// Optional scalar constructors
|
||||||
|
@ -136,7 +136,7 @@ func (o MarshalOptions) marshal(b []byte, message Message) (out protoiface.Marsh
|
|||||||
if allowPartial {
|
if allowPartial {
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
return out, isInitialized(m)
|
return out, checkInitialized(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
|
func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
|
||||||
|
@ -131,7 +131,7 @@ func TestSelfMarshalerWithDescriptor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecodeFastIsInitialized(t *testing.T) {
|
func TestDecodeFastCheckInitialized(t *testing.T) {
|
||||||
for _, test := range testValidMessages {
|
for _, test := range testValidMessages {
|
||||||
if !test.checkFastInit {
|
if !test.checkFastInit {
|
||||||
continue
|
continue
|
||||||
|
@ -17,12 +17,12 @@ import (
|
|||||||
type (
|
type (
|
||||||
methods = struct {
|
methods = struct {
|
||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
Flags supportFlags
|
Flags supportFlags
|
||||||
Size func(sizeInput) sizeOutput
|
Size func(sizeInput) sizeOutput
|
||||||
Marshal func(marshalInput) (marshalOutput, error)
|
Marshal func(marshalInput) (marshalOutput, error)
|
||||||
Unmarshal func(unmarshalInput) (unmarshalOutput, error)
|
Unmarshal func(unmarshalInput) (unmarshalOutput, error)
|
||||||
Merge func(mergeInput) mergeOutput
|
Merge func(mergeInput) mergeOutput
|
||||||
IsInitialized func(isInitializedInput) (isInitializedOutput, error)
|
CheckInitialized func(checkInitializedInput) (checkInitializedOutput, error)
|
||||||
}
|
}
|
||||||
supportFlags = uint64
|
supportFlags = uint64
|
||||||
sizeInput = struct {
|
sizeInput = struct {
|
||||||
@ -67,11 +67,11 @@ type (
|
|||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
Flags uint8
|
Flags uint8
|
||||||
}
|
}
|
||||||
isInitializedInput = struct {
|
checkInitializedInput = struct {
|
||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
Message Message
|
Message Message
|
||||||
}
|
}
|
||||||
isInitializedOutput = struct {
|
checkInitializedOutput = struct {
|
||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -37,8 +37,8 @@ type Methods = struct {
|
|||||||
// Merge merges the contents of a source message into a destination message.
|
// Merge merges the contents of a source message into a destination message.
|
||||||
Merge func(MergeInput) MergeOutput
|
Merge func(MergeInput) MergeOutput
|
||||||
|
|
||||||
// IsInitialized returns an error if any required fields in the message are not set.
|
// CheckInitialized returns an error if any required fields in the message are not set.
|
||||||
IsInitialized func(IsInitializedInput) (IsInitializedOutput, error)
|
CheckInitialized func(CheckInitializedInput) (CheckInitializedOutput, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SupportFlags indicate support for optional features.
|
// SupportFlags indicate support for optional features.
|
||||||
@ -154,14 +154,14 @@ const (
|
|||||||
MergeComplete MergeOutputFlags = 1 << iota
|
MergeComplete MergeOutputFlags = 1 << iota
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsInitializedInput is input to the IsInitialized method.
|
// CheckInitializedInput is input to the CheckInitialized method.
|
||||||
type IsInitializedInput = struct {
|
type CheckInitializedInput = struct {
|
||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
|
|
||||||
Message protoreflect.Message
|
Message protoreflect.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsInitializedOutput is output from the IsInitialized method.
|
// CheckInitializedOutput is output from the CheckInitialized method.
|
||||||
type IsInitializedOutput = struct {
|
type CheckInitializedOutput = struct {
|
||||||
pragma.NoUnkeyedLiterals
|
pragma.NoUnkeyedLiterals
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user