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:
Joe Tsai 2020-02-20 10:05:37 -08:00 committed by Joe Tsai
parent 5e75048cc1
commit f26a9e7e30
17 changed files with 56 additions and 51 deletions

View File

@ -75,7 +75,7 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
if o.AllowPartial {
return nil
}
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
}
type decoder struct {

View File

@ -123,7 +123,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
if o.AllowPartial {
return enc.Bytes(), nil
}
return enc.Bytes(), proto.IsInitialized(m)
return enc.Bytes(), proto.CheckInitialized(m)
}
type encoder struct {

View File

@ -66,7 +66,7 @@ func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
if o.AllowPartial {
return nil
}
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
}
type decoder struct {

View File

@ -118,7 +118,7 @@ func (o MarshalOptions) Marshal(m proto.Message) ([]byte, error) {
if o.AllowPartial {
return out, nil
}
return out, proto.IsInitialized(m)
return out, proto.CheckInitialized(m)
}
type encoder struct {

View File

@ -39,7 +39,7 @@ func Fuzz(data []byte) (score int) {
default:
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")
}
data1, err := proto.MarshalOptions{

View File

@ -12,17 +12,17 @@ import (
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
if ms, ok := in.Message.(*messageState); ok {
p = ms.pointer()
} else {
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()
if !mi.needsInitCheck {
return nil

View File

@ -167,7 +167,7 @@ func makeWeakMessageFieldCoder(fd pref.FieldDescriptor) pointerCoderFuncs {
if !ok {
return nil
}
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
},
merge: func(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
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 {
m := asMessage(p.AsValueOf(ft).Elem())
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
},
merge: mergeMessage,
}
@ -257,7 +257,7 @@ func consumeMessageInfo(b []byte, p pointer, wtyp wire.Type, f *coderFieldInfo,
}
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 {
@ -307,7 +307,7 @@ func consumeMessageValue(b []byte, v pref.Value, _ wire.Number, wtyp wire.Type,
func isInitMessageValue(v pref.Value) error {
m := v.Message().Interface()
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
}
var coderMessageValue = valueCoderFuncs{
@ -374,7 +374,7 @@ func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderF
},
isInit: func(p pointer, f *coderFieldInfo) error {
m := asMessage(p.AsValueOf(ft).Elem())
return proto.IsInitialized(m)
return proto.CheckInitialized(m)
},
merge: mergeMessage,
}
@ -509,7 +509,7 @@ func consumeMessageSliceInfo(b []byte, p pointer, wtyp wire.Type, f *coderFieldI
func isInitMessageSliceInfo(p pointer, f *coderFieldInfo) error {
s := p.PointerSlice()
for _, v := range s {
if err := f.mi.isInitializedPointer(v); err != nil {
if err := f.mi.checkInitializedPointer(v); err != nil {
return err
}
}
@ -567,7 +567,7 @@ func isInitMessageSlice(p pointer, goType reflect.Type) error {
s := p.PointerSlice()
for _, v := range s {
m := asMessage(v.AsValueOf(goType.Elem()))
if err := proto.IsInitialized(m); err != nil {
if err := proto.CheckInitialized(m); err != nil {
return err
}
}
@ -629,7 +629,7 @@ func isInitMessageSliceValue(listv pref.Value) error {
list := listv.List()
for i, llen := 0, list.Len(); i < llen; i++ {
m := list.Get(i).Message().Interface()
if err := proto.IsInitialized(m); err != nil {
if err := proto.CheckInitialized(m); err != nil {
return err
}
}

View File

@ -320,7 +320,7 @@ func isInitMap(mapv reflect.Value, mapi *mapInfo, f *coderFieldInfo) error {
iter := mapRange(mapv)
for iter.Next() {
val := pointerOfValue(iter.Value())
if err := mi.isInitializedPointer(val); err != nil {
if err := mi.checkInitializedPointer(val); err != nil {
return err
}
}

View File

@ -148,8 +148,8 @@ func (mi *MessageInfo) makeCoderMethods(t reflect.Type, si structInfo) {
mi.methods.Flags |= piface.SupportUnmarshalDiscardUnknown
mi.methods.Unmarshal = mi.unmarshal
}
if mi.methods.IsInitialized == nil {
mi.methods.IsInitialized = mi.isInitialized
if mi.methods.CheckInitialized == nil {
mi.methods.CheckInitialized = mi.checkInitialized
}
if mi.methods.Merge == nil {
mi.methods.Merge = mi.merge

View File

@ -10,23 +10,28 @@ import (
"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 {
return isInitialized(m.ProtoReflect())
return CheckInitialized(m)
}
// IsInitialized returns an error if any required fields in m are not set.
func isInitialized(m protoreflect.Message) error {
if methods := protoMethods(m); methods != nil && methods.IsInitialized != nil {
_, err := methods.IsInitialized(protoiface.IsInitializedInput{
// CheckInitialized returns an error if any required fields in m are not set.
func CheckInitialized(m Message) error {
return checkInitialized(m.ProtoReflect())
}
// 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,
})
return err
}
return isInitializedSlow(m)
return checkInitializedSlow(m)
}
func isInitializedSlow(m protoreflect.Message) error {
func checkInitializedSlow(m protoreflect.Message) error {
md := m.Descriptor()
fds := md.Fields()
for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ {
@ -43,21 +48,21 @@ func isInitializedSlow(m protoreflect.Message) error {
return true
}
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():
if fd.MapValue().Message() == nil {
return true
}
v.Map().Range(func(key protoreflect.MapKey, v protoreflect.Value) bool {
err = isInitialized(v.Message())
err = checkInitialized(v.Message())
return err == nil
})
default:
if fd.Message() == nil {
return true
}
err = isInitialized(v.Message())
err = checkInitialized(v.Message())
}
return err == nil
})

View File

@ -17,7 +17,7 @@ import (
weakpb "google.golang.org/protobuf/internal/testprotos/test/weak1"
)
func TestIsInitializedErrors(t *testing.T) {
func TestCheckInitializedErrors(t *testing.T) {
type test struct {
m proto.Message
want string
@ -76,13 +76,13 @@ func TestIsInitializedErrors(t *testing.T) {
t.SkipNow()
}
err := proto.IsInitialized(tt.m)
err := proto.CheckInitialized(tt.m)
got := "<nil>"
if err != nil {
got = fmt.Sprintf("%q", err)
}
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))
}
})
}

View File

@ -95,7 +95,7 @@ func (o UnmarshalOptions) unmarshal(b []byte, message Message) (out protoiface.U
if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
return out, nil
}
return out, isInitialized(m)
return out, checkInitialized(m)
}
func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {

View File

@ -43,7 +43,7 @@
//
// • 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

View File

@ -136,7 +136,7 @@ func (o MarshalOptions) marshal(b []byte, message Message) (out protoiface.Marsh
if allowPartial {
return out, nil
}
return out, isInitialized(m)
return out, checkInitialized(m)
}
func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {

View File

@ -131,7 +131,7 @@ func TestSelfMarshalerWithDescriptor(t *testing.T) {
}
}
func TestDecodeFastIsInitialized(t *testing.T) {
func TestDecodeFastCheckInitialized(t *testing.T) {
for _, test := range testValidMessages {
if !test.checkFastInit {
continue

View File

@ -17,12 +17,12 @@ import (
type (
methods = struct {
pragma.NoUnkeyedLiterals
Flags supportFlags
Size func(sizeInput) sizeOutput
Marshal func(marshalInput) (marshalOutput, error)
Unmarshal func(unmarshalInput) (unmarshalOutput, error)
Merge func(mergeInput) mergeOutput
IsInitialized func(isInitializedInput) (isInitializedOutput, error)
Flags supportFlags
Size func(sizeInput) sizeOutput
Marshal func(marshalInput) (marshalOutput, error)
Unmarshal func(unmarshalInput) (unmarshalOutput, error)
Merge func(mergeInput) mergeOutput
CheckInitialized func(checkInitializedInput) (checkInitializedOutput, error)
}
supportFlags = uint64
sizeInput = struct {
@ -67,11 +67,11 @@ type (
pragma.NoUnkeyedLiterals
Flags uint8
}
isInitializedInput = struct {
checkInitializedInput = struct {
pragma.NoUnkeyedLiterals
Message Message
}
isInitializedOutput = struct {
checkInitializedOutput = struct {
pragma.NoUnkeyedLiterals
}
)

View File

@ -37,8 +37,8 @@ type Methods = struct {
// Merge merges the contents of a source message into a destination message.
Merge func(MergeInput) MergeOutput
// IsInitialized returns an error if any required fields in the message are not set.
IsInitialized func(IsInitializedInput) (IsInitializedOutput, error)
// CheckInitialized returns an error if any required fields in the message are not set.
CheckInitialized func(CheckInitializedInput) (CheckInitializedOutput, error)
}
// SupportFlags indicate support for optional features.
@ -154,14 +154,14 @@ const (
MergeComplete MergeOutputFlags = 1 << iota
)
// IsInitializedInput is input to the IsInitialized method.
type IsInitializedInput = struct {
// CheckInitializedInput is input to the CheckInitialized method.
type CheckInitializedInput = struct {
pragma.NoUnkeyedLiterals
Message protoreflect.Message
}
// IsInitializedOutput is output from the IsInitialized method.
type IsInitializedOutput = struct {
// CheckInitializedOutput is output from the CheckInitialized method.
type CheckInitializedOutput = struct {
pragma.NoUnkeyedLiterals
}