Kaynağa Gözat

Simplify some code.

David Symonds 10 yıl önce
ebeveyn
işleme
eed5e538da
1 değiştirilmiş dosya ile 37 ekleme ve 41 silme
  1. 37 41
      protoc-gen-go/generator/generator.go

+ 37 - 41
protoc-gen-go/generator/generator.go

@@ -136,10 +136,6 @@ func (d *Descriptor) TypeName() []string {
 	return s
 	return s
 }
 }
 
 
-func (d *Descriptor) allowOneof() bool {
-	return true
-}
-
 // EnumDescriptor describes an enum. If it's at top level, its parent will be nil.
 // EnumDescriptor describes an enum. If it's at top level, its parent will be nil.
 // Otherwise it will be the descriptor of the message in which it is defined.
 // Otherwise it will be the descriptor of the message in which it is defined.
 type EnumDescriptor struct {
 type EnumDescriptor struct {
@@ -1463,6 +1459,7 @@ func (g *Generator) goTag(message *Descriptor, field *descriptor.FieldDescriptor
 		if *field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES {
 		if *field.Type == descriptor.FieldDescriptorProto_TYPE_BYTES {
 			name += ",proto3"
 			name += ",proto3"
 		}
 		}
+
 	}
 	}
 	oneof := ""
 	oneof := ""
 	if field.OneofIndex != nil {
 	if field.OneofIndex != nil {
@@ -1556,7 +1553,7 @@ func (g *Generator) GoType(message *Descriptor, field *descriptor.FieldDescripto
 		typ = "[]" + typ
 		typ = "[]" + typ
 	} else if message != nil && message.proto3() {
 	} else if message != nil && message.proto3() {
 		return
 		return
-	} else if field.OneofIndex != nil && message != nil && message.allowOneof() {
+	} else if field.OneofIndex != nil && message != nil {
 		return
 		return
 	} else if needsStar(*field.Type) {
 	} else if needsStar(*field.Type) {
 		typ = "*" + typ
 		typ = "*" + typ
@@ -1629,7 +1626,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
 		fieldNames[field] = fieldName
 		fieldNames[field] = fieldName
 		fieldGetterNames[field] = fieldGetterName
 		fieldGetterNames[field] = fieldGetterName
 
 
-		oneof := field.OneofIndex != nil && message.allowOneof()
+		oneof := field.OneofIndex != nil
 		if oneof && oneofFieldName[*field.OneofIndex] == "" {
 		if oneof && oneofFieldName[*field.OneofIndex] == "" {
 			odp := message.OneofDecl[int(*field.OneofIndex)]
 			odp := message.OneofDecl[int(*field.OneofIndex)]
 			fname := allocName(odp.GetName())
 			fname := allocName(odp.GetName())
@@ -1864,47 +1861,46 @@ func (g *Generator) generateMessage(message *Descriptor) {
 	g.P()
 	g.P()
 
 
 	// Oneof per-field types, discriminants and getters.
 	// Oneof per-field types, discriminants and getters.
-	if message.allowOneof() {
-		// Generate unexported named types for the discriminant interfaces.
-		// We shouldn't have to do this, but there was (~19 Aug 2015) a compiler/linker bug
-		// that was triggered by using anonymous interfaces here.
-		// TODO: Revisit this and consider reverting back to anonymous interfaces.
-		for oi := range message.OneofDecl {
-			dname := oneofDisc[int32(oi)]
-			g.P("type ", dname, " interface { ", dname, "() }")
-		}
-		g.P()
-		for _, field := range message.Field {
-			if field.OneofIndex == nil {
-				continue
-			}
-			_, wiretype := g.GoType(message, field)
-			tag := "protobuf:" + g.goTag(message, field, wiretype)
-			g.P("type ", oneofTypeName[field], " struct{ ", fieldNames[field], " ", fieldTypes[field], " `", tag, "` }")
-			g.RecordTypeUse(field.GetTypeName())
-		}
-		g.P()
-		for _, field := range message.Field {
-			if field.OneofIndex == nil {
-				continue
-			}
-			g.P("func (*", oneofTypeName[field], ") ", oneofDisc[*field.OneofIndex], "() {}")
+	//
+	// Generate unexported named types for the discriminant interfaces.
+	// We shouldn't have to do this, but there was (~19 Aug 2015) a compiler/linker bug
+	// that was triggered by using anonymous interfaces here.
+	// TODO: Revisit this and consider reverting back to anonymous interfaces.
+	for oi := range message.OneofDecl {
+		dname := oneofDisc[int32(oi)]
+		g.P("type ", dname, " interface { ", dname, "() }")
+	}
+	g.P()
+	for _, field := range message.Field {
+		if field.OneofIndex == nil {
+			continue
 		}
 		}
-		g.P()
-		for oi := range message.OneofDecl {
-			fname := oneofFieldName[int32(oi)]
-			g.P("func (m *", ccTypeName, ") Get", fname, "() ", oneofDisc[int32(oi)], " {")
-			g.P("if m != nil { return m.", fname, " }")
-			g.P("return nil")
-			g.P("}")
+		_, wiretype := g.GoType(message, field)
+		tag := "protobuf:" + g.goTag(message, field, wiretype)
+		g.P("type ", oneofTypeName[field], " struct{ ", fieldNames[field], " ", fieldTypes[field], " `", tag, "` }")
+		g.RecordTypeUse(field.GetTypeName())
+	}
+	g.P()
+	for _, field := range message.Field {
+		if field.OneofIndex == nil {
+			continue
 		}
 		}
-		g.P()
+		g.P("func (*", oneofTypeName[field], ") ", oneofDisc[*field.OneofIndex], "() {}")
+	}
+	g.P()
+	for oi := range message.OneofDecl {
+		fname := oneofFieldName[int32(oi)]
+		g.P("func (m *", ccTypeName, ") Get", fname, "() ", oneofDisc[int32(oi)], " {")
+		g.P("if m != nil { return m.", fname, " }")
+		g.P("return nil")
+		g.P("}")
 	}
 	}
+	g.P()
 
 
 	// Field getters
 	// Field getters
 	var getters []getterSymbol
 	var getters []getterSymbol
 	for _, field := range message.Field {
 	for _, field := range message.Field {
-		oneof := field.OneofIndex != nil && message.allowOneof()
+		oneof := field.OneofIndex != nil
 
 
 		fname := fieldNames[field]
 		fname := fieldNames[field]
 		typename, _ := g.GoType(message, field)
 		typename, _ := g.GoType(message, field)
@@ -2048,7 +2044,7 @@ func (g *Generator) generateMessage(message *Descriptor) {
 	}
 	}
 
 
 	// Oneof functions
 	// Oneof functions
-	if len(message.OneofDecl) > 0 && message.allowOneof() {
+	if len(message.OneofDecl) > 0 {
 		fieldWire := make(map[*descriptor.FieldDescriptorProto]string)
 		fieldWire := make(map[*descriptor.FieldDescriptorProto]string)
 
 
 		// method
 		// method