How do I create a function generic enough that it doesn't have to use many switch statements within it to check what type of database the user chose?
My current approach is:
-
If a database type is supported, have a generic WriteData() function that handles all the credential details of that specific database type that were passed by the user.
-
Have a struct for each database type: mysql, postgres, connection string, etc...
-
Have a struct to represent each type of credential information used by the specific database
-
Marshal data into the struct depending which database type was chosen
-
Use maps like this:
var GetNewDB = map[string]interface{}{ "dbType1": dbType1{}, "dbType2": dbType2{}, "dbType3": dbType3{}, "dbType4": dbType4{}, "dbType5": dbType5{}, } var GetCredentials = map[string]interface{}{ "dbType1": Type1Creds{}, "dbType2": Type2Creds{}, "dbType3": Type3Creds{}, "dbType4": Type4Creds{}, "dbType5": Type5Creds{}, }
-
Access generically the details of whatever database is chosen:
whateverDatabase := GetNewDB[dbTypeUserChose] dbCredentials := GetCredentials[dbTypeUserChose]
In the above example, it doesn't necessarily matter that the variables are of type interface{}
Ultimately, this isn't working because each database needs specifics at certain points during the function - e.g. that one type of database needs a username and password, while another may not. It seems to be only solvable by dumping in many type switches or switch statements to give the specifics.
Aucun commentaire:
Enregistrer un commentaire