2019-04-05 20:31:40 +00: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-14 06:55:40 +00:00
|
|
|
"google.golang.org/protobuf/internal/errors"
|
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
2019-04-05 20:31:40 +00: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 {
|
2019-04-09 22:57:05 +00:00
|
|
|
if err := methods.IsInitialized(m); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Fall through to the slow path, since the fast-path
|
|
|
|
// implementation doesn't produce nice errors.
|
2019-04-05 20:31:40 +00:00
|
|
|
//
|
2019-04-09 22:57:05 +00:00
|
|
|
// TODO: Consider producing better errors from the fast path.
|
2019-04-05 20:31:40 +00:00
|
|
|
}
|
|
|
|
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 19:29:25 +00:00
|
|
|
md := m.Descriptor()
|
2019-04-26 06:48:08 +00:00
|
|
|
fds := md.Fields()
|
2019-04-05 20:31:40 +00:00
|
|
|
for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ {
|
2019-04-26 06:48:08 +00:00
|
|
|
fd := fds.ByNumber(nums.Get(i))
|
|
|
|
if !m.Has(fd) {
|
|
|
|
stack = append(stack, fd.Name())
|
2019-04-05 20:31:40 +00:00
|
|
|
return newRequiredNotSetError(stack)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var err error
|
2019-04-26 06:48:08 +00: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 20:31:40 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-04-26 06:48:08 +00:00
|
|
|
for i, list := 0, v.List(); i < list.Len() && err == nil; i++ {
|
2019-04-05 20:31:40 +00:00
|
|
|
stack := append(stack, "[", i, "].")
|
|
|
|
err = isInitialized(list.Get(i).Message(), stack)
|
|
|
|
}
|
2019-04-26 06:48:08 +00:00
|
|
|
case fd.IsMap():
|
|
|
|
if fd.MapValue().Message() == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-05-13 21:32:56 +00: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 20:31:40 +00:00
|
|
|
default:
|
2019-04-26 06:48:08 +00:00
|
|
|
if fd.Message() == nil {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-05 20:31:40 +00: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 16:28:29 +00:00
|
|
|
return errors.RequiredNotSet(buf.String())
|
2019-04-05 20:31:40 +00:00
|
|
|
}
|