Is it possible to eliminate my duplicated code and make it work on multiple model types? #1251
-
Suppose I have two models I am wondering if there is a way I can write a function in my own code that allows me to pass in either a I thought that it might be possible to do this if there was a way that the generated model code provided accessor methods for the fields like func (c Cat) GetName() string {
return c.Name
} or maybe func (c *Cat) GetName() string {
if c == nil {
return ""
}
return c.Name
} If similar methods were generated for the type HasName interface {
GetName() string
} that was implemented by both of my model types, and then I could make a function like func doStuff(n HasName) {
fmt.Printf("Hello I have name %s!\n", n.GetName())
} I am not sure that this approach is correct or that it is possible using the current model generation but I don't see it as being available. Instead I have to write two pretty-nearly identical funcs, one that operates on each type: func doCatStuff(c models.Cat) {
fmt.Printf("Hello I have name %s!\n", c.Name)
}
func doDogStuff(d models.Dog) {
fmt.Printf("Hello I have name %s!\n", d.Name)
} Is there a way around this code duplication? Thank you for your help. I am still getting my feet wet with SQLBoiler and it is quite possible that I am missing something. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You could pass custom templates to SQLBoiler to generate these, but there is little documentation on how to write SQLBoiler templates. |
Beta Was this translation helpful? Give feedback.
You could pass custom templates to SQLBoiler to generate these, but there is little documentation on how to write SQLBoiler templates.