This is my first question on StackOverflow. Please be kind. :)
I have a bunch of modules, each of which has a few getter functions. I am planning to unify the access of these getters by use of a factory.
type CommonGetterModule struct {
cf *ContextFactory
}
func (cm *CommonGetterModule) get(key KEYTYPE, context string) interface{} {
ctx := cm.cf.get(context)
return ctx.get(key)
}
The issue here is that the get calls implemented by each module differs by number of query parameters that together constitute the key. This would be solved best by use of named variables, but those are mysteriously absent from golang.
type Module1 struct {
}
func (c *Module1) get(key KEYTYPE) interface{} {
// interpret the type of key
keyType := determineKeyType(key)
switch keyType {
case keyType1: getForKeyType1(key[0], key[1])
case keyType2: getForKeyType2(key[1])
case keyType1: getForKeyType3(key[0], key[2])
}
KEYTYPE can be some number of strings that can be interpreted by individual modules as part of their get methods. The expected calls can be as follows:
var cm CommonGetterModule
// key1 := {userid: "A10", productid: 200}
cm.get(key1, USER_CONTEXT)
// key2 := {userid: "A10", productid: 200, expiry:"2022JAN"}
cm.get(key2, USER_CONTEXT)
// key3 := {productsymbol:"ABCD"}
cm.get(key3, PRODUCT_CONTEXT)
I have narrowed down my choices of KEYTYPE implementation to these:
- map[ParameterType]ParameterValue
- a struct with all possible values across modules
type AllKeyTypes struct {
userid string
expiry string
productid int
productsymbol string
}
- some form of variadic function
a. Which will be my best option?
b. Is it even worth doing?
Aucun commentaire:
Enregistrer un commentaire