Bläddra i källkod

Improve error message when text unmarshaling a nested message with a required field.

Signed-off-by: David Symonds <dsymonds@golang.org>
Bryan Mills 10 år sedan
förälder
incheckning
78550bb1d0
3 ändrade filer med 17 tillägg och 1 borttagningar
  1. 5 0
      proto/testdata/test.proto
  2. 2 1
      proto/text_parser.go
  3. 10 0
      proto/text_parser_test.go

+ 5 - 0
proto/testdata/test.proto

@@ -237,6 +237,10 @@ message OtherMessage {
   extensions 100 to max;
 }
 
+message RequiredInnerMessage {
+  required InnerMessage leo_finally_won_an_oscar = 1;
+}
+
 message MyMessage {
   required int32 count = 1;
   optional string name = 2;
@@ -244,6 +248,7 @@ message MyMessage {
   repeated string pet = 4;
   optional InnerMessage inner = 5;
   repeated OtherMessage others = 6;
+  optional RequiredInnerMessage we_must_go_deeper = 13;
   repeated InnerMessage rep_inner = 12;
 
   enum Color {

+ 2 - 1
proto/text_parser.go

@@ -663,7 +663,8 @@ func (p *textParser) readStruct(sv reflect.Value, terminator string) error {
 				return err
 			}
 			reqFieldErr = err
-		} else if props.Required {
+		}
+		if props.Required {
 			reqCount--
 		}
 

+ 10 - 0
proto/text_parser_test.go

@@ -336,6 +336,16 @@ var unMarshalTextTests = []UnmarshalTextTest{
 		},
 	},
 
+	// Missing required field in a required submessage
+	{
+		in:  `count: 42 we_must_go_deeper < leo_finally_won_an_oscar <> >`,
+		err: `proto: required field "testdata.InnerMessage.host" not set`,
+		out: &MyMessage{
+			Count:          Int32(42),
+			WeMustGoDeeper: &RequiredInnerMessage{LeoFinallyWonAnOscar: &InnerMessage{}},
+		},
+	},
+
 	// Repeated non-repeated field
 	{
 		in:  `name: "Rob" name: "Russ"`,