protobuf-go/proto/isinit_test.go
Damien Neil 4686e239b6 proto: add IsInitialized
Move all checks for required fields into a proto.IsInitialized function.

Initial testing makes me confident that we can provide a fast-path
implementation of IsInitialized which will perform more than
acceptably.  (In the degenerate-but-common case where a message
transitively contains no required fields, this check can be nearly
zero cost.)

Unifying checks into a single function provides consistent behavior
between the wire, text, and json codecs.

Performing the check after decoding eliminates the wire decoder bug
where a split message is incorrectly seen as missing required fields.

Performing the check after decoding also provides consistent and
arguably more correct behavior when the target message was partially
prepopulated.

Change-Id: I9478b7bebb263af00c0d9f66a1f26e31ff553522
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/170787
Reviewed-by: Herbie Ong <herbie@google.com>
2019-04-05 22:21:46 +00:00

61 lines
1.4 KiB
Go

// Copyright 2019 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 proto_test
import (
"fmt"
"testing"
"github.com/golang/protobuf/v2/internal/scalar"
"github.com/golang/protobuf/v2/proto"
testpb "github.com/golang/protobuf/v2/internal/testprotos/test"
)
func TestIsInitializedErrors(t *testing.T) {
for _, test := range []struct {
m proto.Message
want string
}{
{
&testpb.TestRequired{},
`proto: required field required_field not set`,
},
{
&testpb.TestRequiredForeign{
OptionalMessage: &testpb.TestRequired{},
},
`proto: required field optional_message.required_field not set`,
},
{
&testpb.TestRequiredForeign{
RepeatedMessage: []*testpb.TestRequired{
{RequiredField: scalar.Int32(1)},
{},
},
},
`proto: required field repeated_message[1].required_field not set`,
},
{
&testpb.TestRequiredForeign{
MapMessage: map[int32]*testpb.TestRequired{
1: {},
},
},
`proto: required field map_message[1].required_field not set`,
},
} {
err := proto.IsInitialized(test.m)
got := "<nil>"
if err != nil {
got = fmt.Sprintf("%q", err)
}
want := fmt.Sprintf("%q", test.want)
if got != want {
t.Errorf("IsInitialized(m):\n got: %v\nwant: %v\nMessage:\n%v", got, want, marshalText(test.m))
}
}
}