2017-01-13 12 views
1

行くに新しいイムとイムを構造体への私が持っている現在、どのようなすべての私の設定はYAMLは国番号はそれができる動的であることに注意することがマッピングネストされたコンフィグYAMLはマムシを使用して

countryQueries: 
    sg: 
    - qtype: gmap 
     qplacetype: postal_code 
    - qtype: gmap 
     qplacetype: address 
    - qtype: geocode 
     qplacetype: street_address 

    hk: 
    - qtype: gmap 
     qplacetype: postal_code 
    - qtype: gmap 
     qplacetype: address 
    - qtype: geocode 
     qplacetype: street_address 

以下のように見えるで読み込みますか任意の国にいつでも追加できます。そういえばどこ技術的に私は構造体にこれをマッピングしますどのように私はそれをループすることによって、それを自分自身をcontructが、すべてのヘルプははるかに高く評価され、ここで

for country, queries := range viper.GetStringMap("countryQueries") { 
    // i cant seem to do anything with queries, which i wish to loop it 
    for _,query := range queries {} //here error 
} 

stuckedイムしよう

for _, query := range countryQueries["sg"] { } 

行うことができます

答えて

0

ここgo-yamlといくつかの簡単なコード:

package main 

import (
    "fmt" 
    "gopkg.in/yaml.v2" 
    "log" 
) 

var data = ` 
countryQueries: 
    sg: 
    - qtype: gmap 
     qplacetype: postal_code 
    - qtype: gmap 
     qplacetype: address 
    - qtype: geocode 
     qplacetype: street_address 

    hk: 
    - qtype: gmap 
     qplacetype: postal_code 
    - qtype: gmap 
     qplacetype: address 
    - qtype: geocode 
     qplacetype: street_address 
` 

func main() { 
    m := make(map[interface{}]interface{}) 

    err := yaml.Unmarshal([]byte(data), &m) 
    if err != nil { 
     log.Fatalf("error: %v", err) 
    } 

    fmt.Printf("%v\n", m) 
} 
1

あなたは厳格なgolang structにごyaml構造をマッピングするために探しているなら、あなたは下にネストされたキー/値ペアをマップするためにmapstructureライブラリを利用することができそれぞれの国。例えば

:行われた後

package main 

import (
    "github.com/spf13/viper" 
    "github.com/mitchellh/mapstructure" 
    "fmt" 
    "log" 
) 

type CountryConfig struct { 
    Qtype string 
    Qplacetype string 
} 
type QueryConfig struct { 
    CountryQueries map[string][]CountryConfig; 
} 
func NewQueryConfig() QueryConfig { 
    queryConfig := QueryConfig{} 
    queryConfig.CountryQueries = map[string][]CountryConfig{} 
    return queryConfig 
} 
func main() { 

    viper.SetConfigName("test") 
    viper.AddConfigPath(".") 
    err := viper.ReadInConfig() 
    queryConfig := NewQueryConfig() 
    if err != nil { 
     log.Panic("error:", err) 
    } else { 
     mapstructure.Decode(viper.AllSettings(), &queryConfig) 
    } 
    for _, config := range queryConfig.CountryQueries["sg"] { 

     fmt.Println("qtype:", config.Qtype, "qplacetype:", config.Qplacetype) 
    } 
} 
+1

をやったhttps://github.com/spf13/viper#unmarshaling

そのバイパーは、魅力のように動作する独自のマーシャリングを持っているので、私はそれを使用することに決めました。 –

1

は、いくつかの読書はマムシが素晴らしい作品、それ自身の非整列化能力を持っていることに気づいたので、ここで実現somemore読書をした後、私は

type Configuration struct { 
    Countries map[string][]CountryQuery `mapstructure:"countryQueries"` 
} 

type CountryQuery struct { 
    QType  string 
    QPlaceType string 
} 

func BuildConfig() { 
    viper.SetConfigName("configFileName") 
    viper.AddConfigPath("./config") 
    err := viper.ReadInConfig() 
    if err != nil { 
     panic(fmt.Errorf("Error config file: %s \n", err)) 
    } 

    var config Configuration 

    err = viper.Unmarshal(&config) 
    if err != nil { 
     panic(fmt.Errorf("Unable to decode Config: %s \n", err)) 
    } 
} 
関連する問題