Re: [go-nuts] db interface design
On Monday, May 21, 2018 at 3:47:59 AM UTC-4, Sergey Kamardin wrote: > > > 1. there are a lot of similar functions, for example NewBook and > NewAuthor. > > The difference is type of argument passed into the function. Is it a > good > > idea to combine those 2 functions with a generic New(interface{}) > (string, > > error) function and reflect the actual type inside? > > I would not recommend to do this. Eventually logic of insertion may > become much different and your generic function's code will become a > very hard to read. Also, there is a little bit overhead on using > interfaces for non-pointer structs like `Book` and so on. > Yes I agree. It might get messy in future. So I guess creating insertion function for each struct type might make sense here. > > > 2. Sometimes we query based on one or more conditions (where clause), > for > > example find books based on author/release data/ price. Should I create > a > > generic function like: > > func FindBooks(map[string]interface{}) ([]Book, error) > > It can be done in this way, but I could suggest to use some struct > `BookCriteria` with non-interface fields, say `Author string` and so on. > Then, your `FindBooks` implementation may accept that struct instance. > > Or, for example, `FindBooks` may accept variadic functional options that > can fill appropriate fields of criteria. For more about functional > options you could find in Dave Cheney's article: > > https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis > I didn't thought of functional option previously. Thanks for the hint. I will try to refactor the code base. Thanks! -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [go-nuts] db interface design
> 1. there are a lot of similar functions, for example NewBook and NewAuthor. > The difference is type of argument passed into the function. Is it a good > idea to combine those 2 functions with a generic New(interface{}) (string, > error) function and reflect the actual type inside? I would not recommend to do this. Eventually logic of insertion may become much different and your generic function's code will become a very hard to read. Also, there is a little bit overhead on using interfaces for non-pointer structs like `Book` and so on. > 2. Sometimes we query based on one or more conditions (where clause), for > example find books based on author/release data/ price. Should I create a > generic function like: > func FindBooks(map[string]interface{}) ([]Book, error) It can be done in this way, but I could suggest to use some struct `BookCriteria` with non-interface fields, say `Author string` and so on. Then, your `FindBooks` implementation may accept that struct instance. Or, for example, `FindBooks` may accept variadic functional options that can fill appropriate fields of criteria. For more about functional options you could find in Dave Cheney's article: https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] db interface design
Hi I am trying to practice golang and have some questions regarding to interface design. Say I want to create a simple app to store the book and its author info. I am planning to create interfaces so that I can use different db driver (mysql/mongo/redis...) as long as if they implemented DB interface: https://play.golang.org/p/DOw83_OqBOe I have a couple questions: 1. there are a lot of similar functions, for example NewBook and NewAuthor. The difference is type of argument passed into the function. Is it a good idea to combine those 2 functions with a generic New(interface{}) (string, error) function and reflect the actual type inside? 2. Sometimes we query based on one or more conditions (where clause), for example find books based on author/release data/ price. Should I create a generic function like: func FindBooks(map[string]interface{}) ([]Book, error) that passed in a map of condition kv and do the typer assertion of each condition key values? Thanks for any advice -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.