(file *generator.FileDescriptor)
| 133 | } |
| 134 | |
| 135 | func (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() |
nothing calls this directly
no test coverage detected