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

Method generateCommonMethods

protoc-gen-go/generator/generator.go:2112–2176  ·  view source on GitHub ↗

generateCommonMethods adds methods to the message that are not on a per field basis.

(mc *msgCtx)

Source from the content-addressed store, hash-verified

2110
2111// generateCommonMethods adds methods to the message that are not on a per field basis.
2112func (g *Generator) generateCommonMethods(mc *msgCtx) {
2113 // Reset, String and ProtoMessage methods.
2114 g.P("func (m *", mc.goName, ") Reset() { *m = ", mc.goName, "{} }")
2115 g.P("func (m *", mc.goName, ") String() string { return ", g.Pkg["proto"], ".CompactTextString(m) }")
2116 g.P("func (*", mc.goName, ") ProtoMessage() {}")
2117 var indexes []string
2118 for m := mc.message; m != nil; m = m.parent {
2119 indexes = append([]string{strconv.Itoa(m.index)}, indexes...)
2120 }
2121 g.P("func (*", mc.goName, ") Descriptor() ([]byte, []int) {")
2122 g.P("return ", g.file.VarName(), ", []int{", strings.Join(indexes, ", "), "}")
2123 g.P("}")
2124 g.P()
2125 // TODO: Revisit the decision to use a XXX_WellKnownType method
2126 // if we change proto.MessageName to work with multiple equivalents.
2127 if mc.message.file.GetPackage() == "google.protobuf" && wellKnownTypes[mc.message.GetName()] {
2128 g.P("func (*", mc.goName, `) XXX_WellKnownType() string { return "`, mc.message.GetName(), `" }`)
2129 g.P()
2130 }
2131
2132 // Extension support methods
2133 if len(mc.message.ExtensionRange) > 0 {
2134 g.P()
2135 g.P("var extRange_", mc.goName, " = []", g.Pkg["proto"], ".ExtensionRange{")
2136 for _, r := range mc.message.ExtensionRange {
2137 end := fmt.Sprint(*r.End - 1) // make range inclusive on both ends
2138 g.P("{Start: ", r.Start, ", End: ", end, "},")
2139 }
2140 g.P("}")
2141 g.P("func (*", mc.goName, ") ExtensionRangeArray() []", g.Pkg["proto"], ".ExtensionRange {")
2142 g.P("return extRange_", mc.goName)
2143 g.P("}")
2144 g.P()
2145 }
2146
2147 // TODO: It does not scale to keep adding another method for every
2148 // operation on protos that we want to switch over to using the
2149 // table-driven approach. Instead, we should only add a single method
2150 // that allows getting access to the *InternalMessageInfo struct and then
2151 // calling Unmarshal, Marshal, Merge, Size, and Discard directly on that.
2152
2153 // Wrapper for table-driven marshaling and unmarshaling.
2154 g.P("func (m *", mc.goName, ") XXX_Unmarshal(b []byte) error {")
2155 g.P("return xxx_messageInfo_", mc.goName, ".Unmarshal(m, b)")
2156 g.P("}")
2157
2158 g.P("func (m *", mc.goName, ") XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {")
2159 g.P("return xxx_messageInfo_", mc.goName, ".Marshal(b, m, deterministic)")
2160 g.P("}")
2161
2162 g.P("func (m *", mc.goName, ") XXX_Merge(src ", g.Pkg["proto"], ".Message) {")
2163 g.P("xxx_messageInfo_", mc.goName, ".Merge(m, src)")
2164 g.P("}")
2165
2166 g.P("func (m *", mc.goName, ") XXX_Size() int {") // avoid name clash with "Size" field in some message
2167 g.P("return xxx_messageInfo_", mc.goName, ".Size(m)")
2168 g.P("}")
2169

Callers 1

generateMessageMethod · 0.95

Calls 3

PMethod · 0.95
VarNameMethod · 0.80
GetNameMethod · 0.45

Tested by

no test coverage detected