I have a config file that looks like:
{
"auth": {
"client_id": "foo",
"client_secret": "bar",
"account": "[email protected]",
"secrets_type": "auto"
},
"jobs": [
{
"source_folder": "folder1",
"create_albums": "off",
"delete_after_upload": false,
"include": [
"*.jpg"
],
"exclude": []
},
{
"source_folder": "folder2",
"create_albums": "folderName",
"delete_after_upload": true,
"include": [
"*.jpg", "*.webp"
],
"exclude": [
"abc*"
]
}
]
}
And I'm reading it using the viper package in golang.
I'm having problems to get the jobs settings:
type FolderUpload struct {
sourceFolder string `mapstructure:"source_folder"`
createAlbums string `mapstructure:"create_albums"`
deleteAfterUpload bool `mapstructure:"delete_after_upload"`
include []string `mapstructure:"include"`
exclude []string `mapstructure:"exclude"`
}
// Read the "jobsSettings" section as a slice of maps
jobsSettings := viper.Get("jobs").([]interface{})
// Iterate over each job item
for _, job := range jobsSettings {
jobMap := job.(map[string]interface{})
j := FolderUploadJob{
sourceFolder: jobMap["source_folder"].(string),
createAlbums: jobMap["create_albums"].(string),
deleteAfterUpload: jobMap["delete_after_upload"].(bool),
include: jobMap["include"].([]string),
exclude: jobMap["exclude"].([]string),
}
}
I've tried with viper.UnmarshalKey() too, but I always receive a panic when trying to read the include and exclude.
Can anyone point me to the right way?
viper.UnmarshalKeyworks and please note that the fields ofFolderUploadshould be exported. See the demo below: