2019-04-05 13:31:40 -07:00
|
|
|
// 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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
"google.golang.org/protobuf/internal/errors"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
2019-04-05 13:31:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// IsInitialized returns an error if any required fields in m are not set.
|
|
|
|
func IsInitialized(m Message) error {
|
|
|
|
if methods := protoMethods(m); methods != nil && methods.IsInitialized != nil {
|
|
|
|
// TODO: Do we need a way to disable the fast path here?
|
|
|
|
//
|
|
|
|
// TODO: Should detailed information about missing
|
|
|
|
// fields always be provided by the slow-but-informative
|
|
|
|
// reflective implementation?
|
|
|
|
return methods.IsInitialized(m)
|
|
|
|
}
|
|
|
|
return isInitialized(m.ProtoReflect(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInitialized returns an error if any required fields in m are not set.
|
|
|
|
func isInitialized(m pref.Message, stack []interface{}) error {
|
2019-05-01 12:29:25 -07:00
|
|
|
md := m.Descriptor()
|
2019-04-25 23:48:08 -07:00
|
|
|
fds := md.Fields()
|
2019-04-05 13:31:40 -07:00
|
|
|
for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ {
|
2019-04-25 23:48:08 -07:00
|
|
|
fd := fds.ByNumber(nums.Get(i))
|
|
|
|
if !m.Has(fd) {
|
|
|
|
stack = append(stack, fd.Name())
|
2019-04-05 13:31:40 -07:00
|
|
|
return newRequiredNotSetError(stack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
2019-04-25 23:48:08 -07:00
|
|
|
m.Range(func(fd pref.FieldDescriptor, v pref.Value) bool {
|
|
|
|
// Recurse into fields containing message values.
|
|
|
|
stack := append(stack, fd.Name())
|
|
|
|
switch {
|
|
|
|
case fd.IsList():
|
|
|
|
if fd.Message() == nil {
|
2019-04-05 13:31:40 -07:00
|
|
|
return true
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
for i, list := 0, v.List(); i < list.Len() && err == nil; i++ {
|
2019-04-05 13:31:40 -07:00
|
|
|
stack := append(stack, "[", i, "].")
|
|
|
|
err = isInitialized(list.Get(i).Message(), stack)
|
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
case fd.IsMap():
|
|
|
|
if fd.MapValue().Message() == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-05-13 14:32:56 -07:00
|
|
|
v.Map().Range(func(key pref.MapKey, v pref.Value) bool {
|
|
|
|
stack := append(stack, "[", key, "].")
|
|
|
|
err = isInitialized(v.Message(), stack)
|
|
|
|
return err == nil
|
|
|
|
})
|
2019-04-05 13:31:40 -07:00
|
|
|
default:
|
2019-04-25 23:48:08 -07:00
|
|
|
if fd.Message() == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-05 13:31:40 -07:00
|
|
|
stack := append(stack, ".")
|
|
|
|
err = isInitialized(v.Message(), stack)
|
|
|
|
}
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequiredNotSetError(stack []interface{}) error {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, s := range stack {
|
|
|
|
fmt.Fprint(&buf, s)
|
|
|
|
}
|
2019-06-19 09:28:29 -07:00
|
|
|
return errors.RequiredNotSet(buf.String())
|
2019-04-05 13:31:40 -07:00
|
|
|
}
|