MCPcopy
hub / github.com/gogo/protobuf / Generate

Method Generate

plugin/union/union.go:135–205  ·  view source on GitHub ↗
(file *generator.FileDescriptor)

Source from the content-addressed store, hash-verified

133}
134
135func (p *union) Generate(file *generator.FileDescriptor) {
136 p.PluginImports = generator.NewPluginImports(p.Generator)
137
138 for _, message := range file.Messages() {
139 if !gogoproto.IsUnion(file.FileDescriptorProto, message.DescriptorProto) {
140 continue
141 }
142 if message.DescriptorProto.HasExtension() {
143 panic("onlyone does not currently support extensions")
144 }
145 if message.DescriptorProto.GetOptions().GetMapEntry() {
146 continue
147 }
148
149 ccTypeName := generator.CamelCaseSlice(message.TypeName())
150 p.P(`func (this *`, ccTypeName, `) GetValue() interface{} {`)
151 p.In()
152 for _, field := range message.Field {
153 fieldname := p.GetFieldName(message, field)
154 if fieldname == "Value" {
155 panic("cannot have a onlyone message " + ccTypeName + " with a field named Value")
156 }
157 p.P(`if this.`, fieldname, ` != nil {`)
158 p.In()
159 p.P(`return this.`, fieldname)
160 p.Out()
161 p.P(`}`)
162 }
163 p.P(`return nil`)
164 p.Out()
165 p.P(`}`)
166 p.P(``)
167 p.P(`func (this *`, ccTypeName, `) SetValue(value interface{}) bool {`)
168 p.In()
169 p.P(`switch vt := value.(type) {`)
170 p.In()
171 for _, field := range message.Field {
172 fieldname := p.GetFieldName(message, field)
173 goTyp, _ := p.GoType(message, field)
174 p.P(`case `, goTyp, `:`)
175 p.In()
176 p.P(`this.`, fieldname, ` = vt`)
177 p.Out()
178 }
179 p.P(`default:`)
180 p.In()
181 for _, field := range message.Field {
182 fieldname := p.GetFieldName(message, field)
183 if field.IsMessage() {
184 goTyp, _ := p.GoType(message, field)
185 obj := p.ObjectNamed(field.GetTypeName()).(*generator.Descriptor)
186
187 if gogoproto.IsUnion(obj.File().FileDescriptorProto, obj.DescriptorProto) {
188 p.P(`this.`, fieldname, ` = new(`, generator.GoTypeToName(goTyp), `)`)
189 p.P(`if set := this.`, fieldname, `.SetValue(value); set {`)
190 p.In()
191 p.P(`return true`)
192 p.Out()

Callers

nothing calls this directly

Calls 15

NewPluginImportsFunction · 0.92
IsUnionFunction · 0.92
CamelCaseSliceFunction · 0.92
GoTypeToNameFunction · 0.92
MessagesMethod · 0.80
HasExtensionMethod · 0.80
GetMapEntryMethod · 0.80
InMethod · 0.80
GetFieldNameMethod · 0.80
OutMethod · 0.80
GoTypeMethod · 0.80
IsMessageMethod · 0.80

Tested by

no test coverage detected