I'm trying to write a configuration package that takes a json filename and a configuration struct. It should unmarshal the json into the passed-in struct and return it. I'm trying to work with interfaces so I can pass any struct I want
The error is:
panic: interface conversion: interface {} is map[string]interface {}, not *main.ConfigurationData
I'm not quite sure how to solve this issue.
Here is my main package
package main
import (
"config"
"commons"
)
type ConfigurationData struct {
S3ARN string `json:"S3ARN"`
SQSQueueUrl string `json:"SQSQueueUrl"`
}
var configData *ConfigurationData
func main(){
configData=config.Load("aws.config.json",configData).(*ConfigurationData)
commons.Dump(configData)
}
Here is my config package
package config
import (
"os"
"encoding/json"
"sync"
"commons"
)
var configLock = new(sync.RWMutex)
func Load(filename string,config interface{})interface{} {
file, err := os.Open(filename)
commons.CheckErrorf(err, "Config Open Error")
defer file.Close()
decoder := json.NewDecoder(file)
configLock.Lock()
err = decoder.Decode(&config)
commons.CheckErrorf(err, "Config Decode Error")
configLock.Unlock()
return config
}