MCPcopy
hub / github.com/golang/protobuf / generateEnum

Method generateEnum

protoc-gen-go/generator/generator.go:1341–1432  ·  view source on GitHub ↗

Generate the enum definitions for this EnumDescriptor.

(enum *EnumDescriptor)

Source from the content-addressed store, hash-verified

1339
1340// Generate the enum definitions for this EnumDescriptor.
1341func (g *Generator) generateEnum(enum *EnumDescriptor) {
1342 // The full type name
1343 typeName := enum.TypeName()
1344 // The full type name, CamelCased.
1345 ccTypeName := CamelCaseSlice(typeName)
1346 ccPrefix := enum.prefix()
1347
1348 deprecatedEnum := ""
1349 if enum.GetOptions().GetDeprecated() {
1350 deprecatedEnum = deprecationComment
1351 }
1352 g.PrintComments(enum.path)
1353 g.P("type ", Annotate(enum.file, enum.path, ccTypeName), " int32", deprecatedEnum)
1354 g.file.addExport(enum, enumSymbol{ccTypeName, enum.proto3()})
1355 g.P("const (")
1356 for i, e := range enum.Value {
1357 etorPath := fmt.Sprintf("%s,%d,%d", enum.path, enumValuePath, i)
1358 g.PrintComments(etorPath)
1359
1360 deprecatedValue := ""
1361 if e.GetOptions().GetDeprecated() {
1362 deprecatedValue = deprecationComment
1363 }
1364
1365 name := ccPrefix + *e.Name
1366 g.P(Annotate(enum.file, etorPath, name), " ", ccTypeName, " = ", e.Number, " ", deprecatedValue)
1367 g.file.addExport(enum, constOrVarSymbol{name, "const", ccTypeName})
1368 }
1369 g.P(")")
1370 g.P()
1371 g.P("var ", ccTypeName, "_name = map[int32]string{")
1372 generated := make(map[int32]bool) // avoid duplicate values
1373 for _, e := range enum.Value {
1374 duplicate := ""
1375 if _, present := generated[*e.Number]; present {
1376 duplicate = "// Duplicate value: "
1377 }
1378 g.P(duplicate, e.Number, ": ", strconv.Quote(*e.Name), ",")
1379 generated[*e.Number] = true
1380 }
1381 g.P("}")
1382 g.P()
1383 g.P("var ", ccTypeName, "_value = map[string]int32{")
1384 for _, e := range enum.Value {
1385 g.P(strconv.Quote(*e.Name), ": ", e.Number, ",")
1386 }
1387 g.P("}")
1388 g.P()
1389
1390 if !enum.proto3() {
1391 g.P("func (x ", ccTypeName, ") Enum() *", ccTypeName, " {")
1392 g.P("p := new(", ccTypeName, ")")
1393 g.P("*p = x")
1394 g.P("return p")
1395 g.P("}")
1396 g.P()
1397 }
1398

Callers 1

generateMethod · 0.95

Calls 11

PrintCommentsMethod · 0.95
PMethod · 0.95
CamelCaseSliceFunction · 0.85
AnnotateFunction · 0.85
prefixMethod · 0.80
addExportMethod · 0.80
proto3Method · 0.80
VarNameMethod · 0.80
TypeNameMethod · 0.65
GetNameMethod · 0.45

Tested by

no test coverage detected