Просмотр исходного кода

Add a oneof example to the test proto in the package document.

Dave Day 10 лет назад
Родитель
Сommit
ef197e1a5f
1 измененных файлов с 55 добавлено и 6 удалено
  1. 55 6
      proto/lib.go

+ 55 - 6
proto/lib.go

@@ -84,6 +84,10 @@ Given file test.proto, containing
 	  optional group OptionalGroup = 4 {
 	    required string RequiredField = 5;
 	  }
+	  oneof union {
+	    int32 number = 6;
+	    string name = 7;
+	  }
 	}
 
 The resulting file, test.pb.go, is:
@@ -122,15 +126,40 @@ The resulting file, test.pb.go, is:
 	}
 
 	type Test struct {
-		Label            *string             `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`
-		Type             *int32              `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`
-		Reps             []int64             `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`
-		Optionalgroup    *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`
-		XXX_unrecognized []byte              `json:"-"`
+		Label         *string             `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`
+		Type          *int32              `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`
+		Reps          []int64             `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`
+		Optionalgroup *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup" json:"optionalgroup,omitempty"`
+		// Types that are valid to be assigned to Union:
+		//	*Test_Number
+		//	*Test_Name
+		Union            isTest_Union `protobuf_oneof:"union"`
+		XXX_unrecognized []byte       `json:"-"`
 	}
 	func (m *Test) Reset()         { *m = Test{} }
 	func (m *Test) String() string { return proto.CompactTextString(m) }
-	func (*Test) ProtoMessage()    {}
+	func (*Test) ProtoMessage() {}
+
+	type isTest_Union interface {
+		isTest_Union()
+	}
+
+	type Test_Number struct {
+		Number int32 `protobuf:"varint,6,opt,name=number"`
+	}
+	type Test_Name struct {
+		Name string `protobuf:"bytes,7,opt,name=name"`
+	}
+
+	func (*Test_Number) isTest_Union() {}
+	func (*Test_Name) isTest_Union()   {}
+
+	func (m *Test) GetUnion() isTest_Union {
+		if m != nil {
+			return m.Union
+		}
+		return nil
+	}
 	const Default_Test_Type int32 = 77
 
 	func (m *Test) GetLabel() string {
@@ -167,6 +196,20 @@ The resulting file, test.pb.go, is:
 		return ""
 	}
 
+	func (m *Test) GetNumber() int32 {
+		if x, ok := m.GetUnion().(*Test_Number); ok {
+			return x.Number
+		}
+		return 0
+	}
+
+	func (m *Test) GetName() string {
+		if x, ok := m.GetUnion().(*Test_Name); ok {
+			return x.Name
+		}
+		return ""
+	}
+
 	func init() {
 		proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
 	}
@@ -189,6 +232,7 @@ package main
 			Optionalgroup: &pb.Test_OptionalGroup{
 				RequiredField: proto.String("good bye"),
 			},
+			Union: &pb.Test_Name{"fred"},
 		}
 		data, err := proto.Marshal(test)
 		if err != nil {
@@ -203,6 +247,11 @@ package main
 		if test.GetLabel() != newTest.GetLabel() {
 			log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
 		}
+		// Use a type switch to determine which oneof was set.
+		switch u := test.Union.(type) {
+		case *pb.Test_Number: // u.Number contains the number.
+		case *pb.Test_Name: // u.Name contains the string.
+		}
 		// etc.
 	}
 */