getter for a oneof field will print additional discriminators and interfaces for the oneof, also it prints all the getters for the sub fields.
(g *Generator, mc *msgCtx)
| 1897 | // getter for a oneof field will print additional discriminators and interfaces for the oneof, |
| 1898 | // also it prints all the getters for the sub fields. |
| 1899 | func (f *oneofField) getter(g *Generator, mc *msgCtx) { |
| 1900 | // The discriminator type |
| 1901 | g.P("type ", f.goType, " interface {") |
| 1902 | g.P(f.goType, "()") |
| 1903 | g.P("}") |
| 1904 | g.P() |
| 1905 | // The subField types, fulfilling the discriminator type contract |
| 1906 | for _, sf := range f.subFields { |
| 1907 | g.P("type ", Annotate(mc.message.file, sf.fullPath, sf.oneofTypeName), " struct {") |
| 1908 | g.P(Annotate(mc.message.file, sf.fullPath, sf.goName), " ", sf.goType, " `", sf.tags, "`") |
| 1909 | g.P("}") |
| 1910 | g.P() |
| 1911 | } |
| 1912 | for _, sf := range f.subFields { |
| 1913 | g.P("func (*", sf.oneofTypeName, ") ", f.goType, "() {}") |
| 1914 | g.P() |
| 1915 | } |
| 1916 | // Getter for the oneof field |
| 1917 | g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, f.fullPath, f.getterName), "() ", f.goType, " {") |
| 1918 | g.P("if m != nil { return m.", f.goName, " }") |
| 1919 | g.P("return nil") |
| 1920 | g.P("}") |
| 1921 | g.P() |
| 1922 | // Getters for each oneof |
| 1923 | for _, sf := range f.subFields { |
| 1924 | if sf.deprecated != "" { |
| 1925 | g.P(sf.deprecated) |
| 1926 | } |
| 1927 | g.P("func (m *", mc.goName, ") ", Annotate(mc.message.file, sf.fullPath, sf.getterName), "() "+sf.goType+" {") |
| 1928 | g.P("if x, ok := m.", f.getterName, "().(*", sf.oneofTypeName, "); ok {") |
| 1929 | g.P("return x.", sf.goName) |
| 1930 | g.P("}") |
| 1931 | g.P("return ", sf.getterDef) |
| 1932 | g.P("}") |
| 1933 | g.P() |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | // setter prints the setter method of the field. |
| 1938 | func (f *oneofField) setter(g *Generator, mc *msgCtx) { |