Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
Damien Neil | 0232edc | 2020-02-20 10:30:38 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 8 | "google.golang.org/protobuf/internal/errors" |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 9 | "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | 466dd77 | 2020-02-14 14:49:35 -0800 | [diff] [blame] | 10 | "google.golang.org/protobuf/runtime/protoiface" |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 13 | // CheckInitialized returns an error if any required fields in m are not set. |
| 14 | func CheckInitialized(m Message) error { |
Joe Tsai | 8cfc14f | 2020-04-17 10:55:19 -0700 | [diff] [blame] | 15 | // Treat a nil message interface as an "untyped" empty message, |
| 16 | // which we assume to have no required fields. |
| 17 | if m == nil { |
| 18 | return nil |
| 19 | } |
| 20 | |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 21 | return checkInitialized(m.ProtoReflect()) |
| 22 | } |
| 23 | |
| 24 | // CheckInitialized returns an error if any required fields in m are not set. |
| 25 | func checkInitialized(m protoreflect.Message) error { |
| 26 | if methods := protoMethods(m); methods != nil && methods.CheckInitialized != nil { |
| 27 | _, err := methods.CheckInitialized(protoiface.CheckInitializedInput{ |
Damien Neil | 466dd77 | 2020-02-14 14:49:35 -0800 | [diff] [blame] | 28 | Message: m, |
| 29 | }) |
| 30 | return err |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 31 | } |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 32 | return checkInitializedSlow(m) |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 35 | func checkInitializedSlow(m protoreflect.Message) error { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 36 | md := m.Descriptor() |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 37 | fds := md.Fields() |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 38 | for i, nums := 0, md.RequiredNumbers(); i < nums.Len(); i++ { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 39 | fd := fds.ByNumber(nums.Get(i)) |
| 40 | if !m.Has(fd) { |
Damien Neil | 3d0706a | 2019-07-09 11:40:49 -0700 | [diff] [blame] | 41 | return errors.RequiredNotSet(string(fd.FullName())) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | var err error |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 45 | m.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 46 | switch { |
| 47 | case fd.IsList(): |
| 48 | if fd.Message() == nil { |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 49 | return true |
| 50 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 51 | for i, list := 0, v.List(); i < list.Len() && err == nil; i++ { |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 52 | err = checkInitialized(list.Get(i).Message()) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 53 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 54 | case fd.IsMap(): |
| 55 | if fd.MapValue().Message() == nil { |
| 56 | return true |
| 57 | } |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 58 | v.Map().Range(func(key protoreflect.MapKey, v protoreflect.Value) bool { |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 59 | err = checkInitialized(v.Message()) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 60 | return err == nil |
| 61 | }) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 62 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 63 | if fd.Message() == nil { |
| 64 | return true |
| 65 | } |
Joe Tsai | f26a9e7 | 2020-02-20 10:05:37 -0800 | [diff] [blame] | 66 | err = checkInitialized(v.Message()) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 67 | } |
| 68 | return err == nil |
| 69 | }) |
| 70 | return err |
| 71 | } |