viper changing data type on set

329 views Asked by At

I have folder with json files like:

  • schema file1.json file2.json

and I am trying to iterate and get all data from each file in map[string]interface{}

and I am getting data in such type successfully. but when I store it to the viper config it changed to map[json:map[]]

here is the snip

func iterateFiles(f string) {

    pwd, _ := os.Getwd()
    path := pwd + "/schema/"

    if f != path {

        file, err := ioutil.ReadFile(f)
        if err != nil {
            fmt.Println("error reading file: " + f)
        }

        var result map[string]interface{}
        json.Unmarshal([]byte(file), &result)

        _, filename := filepath.Split(f)

        fmt.Println(result)

        viper.Set(filename, result)
    }
}

fmt.Println(result) returns me:

map[callback_url:[required url] next_date:[required]]
map[ref:[required string]]
map[ref:[required string]]

but when I try to get the map from viper I receive:

result := viper.GetStringMap(action)

fmt.Println(result)

map[json:map[callback_url:[required url] next_date:[required]]]

Could someone point me in the right direction so I can receive the type which I stored?

1

There are 1 answers

0
Eugen Dubrovin On

resolved it like this

r := viper.GetStringMap(action)
result := r["json"].(map[string]interface{})

if you know more efficient way please let me know