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

Method generateMessage

protoc-gen-go/generator/generator.go:2179–2428  ·  view source on GitHub ↗

Generate the type, methods and default constant definitions for this Descriptor.

(message *Descriptor)

Source from the content-addressed store, hash-verified

2177
2178// Generate the type, methods and default constant definitions for this Descriptor.
2179func (g *Generator) generateMessage(message *Descriptor) {
2180 topLevelFields := []topLevelField{}
2181 oFields := make(map[int32]*oneofField)
2182 // The full type name
2183 typeName := message.TypeName()
2184 // The full type name, CamelCased.
2185 goTypeName := CamelCaseSlice(typeName)
2186
2187 usedNames := make(map[string]bool)
2188 for _, n := range methodNames {
2189 usedNames[n] = true
2190 }
2191
2192 // allocNames finds a conflict-free variation of the given strings,
2193 // consistently mutating their suffixes.
2194 // It returns the same number of strings.
2195 allocNames := func(ns ...string) []string {
2196 Loop:
2197 for {
2198 for _, n := range ns {
2199 if usedNames[n] {
2200 for i := range ns {
2201 ns[i] += "_"
2202 }
2203 continue Loop
2204 }
2205 }
2206 for _, n := range ns {
2207 usedNames[n] = true
2208 }
2209 return ns
2210 }
2211 }
2212
2213 mapFieldTypes := make(map[*descriptor.FieldDescriptorProto]string) // keep track of the map fields to be added later
2214
2215 // Build a structure more suitable for generating the text in one pass
2216 for i, field := range message.Field {
2217 // Allocate the getter and the field at the same time so name
2218 // collisions create field/method consistent names.
2219 // TODO: This allocation occurs based on the order of the fields
2220 // in the proto file, meaning that a change in the field
2221 // ordering can change generated Method/Field names.
2222 base := CamelCase(*field.Name)
2223 ns := allocNames(base, "Get"+base)
2224 fieldName, fieldGetterName := ns[0], ns[1]
2225 typename, wiretype := g.GoType(message, field)
2226 jsonName := *field.Name
2227 tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty")
2228
2229 oneof := field.OneofIndex != nil
2230 if oneof && oFields[*field.OneofIndex] == nil {
2231 odp := message.OneofDecl[int(*field.OneofIndex)]
2232 base := CamelCase(odp.GetName())
2233 names := allocNames(base, "Get"+base)
2234 fname, gname := names[0], names[1]
2235
2236 // This is the first field of a oneof we haven't seen before.

Callers 1

generateMethod · 0.95

Calls 15

GoTypeMethod · 0.95
goTagMethod · 0.95
makeCommentsMethod · 0.95
ObjectNamedMethod · 0.95
RecordTypeUseMethod · 0.95
getterDefaultMethod · 0.95
generateMessageStructMethod · 0.95
PMethod · 0.95
generateCommonMethodsMethod · 0.95
generateGettersMethod · 0.95
generateSettersMethod · 0.95

Tested by

no test coverage detected