I am wondering if it is possible in the below scenario to be able to remove the two "Balance" structs in each package and just use the interface based "Balance" struct somehow for unmarshalling. The issue that I have is that the json structure that comes back from the individual api's for Balance is different so as of right now I am just converting the local Balance structs into the global interface based Balance struct and returning that. This method is not ideal especially for more complex but very similar functions that I have. I am sure there is a better way to do this but could not find anything similar. Thanks in advance.
//******************** API Inteface *****************************
//API Interface path: /global/interfaces/apiinterface/apiinterface.go && structs.go
// /global/interfaces/apiinterface/apiinterface.go
type APIInterface interface {
//.... other interface func(s)
GetBalance(account string) (balance Balance, err error)
}
// /global/interfaces/apiinterface/structs.go
type Balance struct{
Available float64
Unconfirmed float64
}
//******************** Library 1 *****************************
//Library 1 path: /library1/library1.go
type jsonResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Result json.RawMessage `json:"result"`
}
//Library 1 Balance struct /library1/structs.go
type Balance struct {
Available float64 `json:"available"` //json name difference <----------
Unconfirmed float64 `json:"unconfirmed"` //json name difference <----------
}
//Library 1 GetBalance /library1/library1.go
func (c *Library1) GetBalance(account string) (balance apiinterface.Balance, err error) {
r, err := c.client.do("getbalance", []interface{}{account})
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
var tmpBalance Balance //library 1 Balance struct
err = json.Unmarshal(response.Result, &tmpBalance)
if err != nil{
return
}
//convert to global apiinterface struct
balance = apiinterface.Balance{
Available:tmpBalance.Available,
Unconfirmed:tmpBalance.Unconfirmed,
}
return
}
//******************** Library 2 *****************************
//Library 2 path: /library2/library2.go
type jsonResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
Result json.RawMessage `json:"result"`
}
//Library 2 Balance struct
type Balance struct {
Available float64 `json:"total"` //json name difference <----------
Unconfirmed float64 `json:"pending"` //json name difference <----------
}
//Library 2 GetBalance
func (c *Library2) GetBalance(account string) (balance apiinterface.Balance, err error) {
r, err := c.client.do("getbalance", []interface{}{account})
if err != nil {
return
}
var response jsonResponse
if err = json.Unmarshal(r, &response); err != nil {
return
}
var tmpBalance Balance //library 2 Balance struct
err = json.Unmarshal(response.Result, &tmpBalance)
if err != nil{
return
}
//convert to global apiinterface struct
balance = apiinterface.Balance{
Available:tmpBalance.Available,
Unconfirmed:tmpBalance.Unconfirmed,
}
return
}