func TestPluginConfig(t *testing.T) { conf, err := wk.ReadDefaultConfigFile() if err != nil { t.Error(err) return } node := conf.PluginConfig.MustChild("session_debug") dump := node.Dump() t.Log("session_debug", dump) option := &Option{} kson.Unmarshal([]byte(dump), option) if err := conf.PluginConfig.MustChild("session_debug").Value(option); err != nil { t.Errorf("plugin config %s error %v", "session_debug", err) } else { equal(t, "session_debug.Driver", 1024, option.Int) equal(t, "session_debug.String", "string demo", option.String) equal(t, "session_debug.Float32", 3.14, option.Float64) equal(t, "session_debug.Bool", true, option.Bool) equal(t, "session_debug.Map.key1", "key1 value", option.Map["key1"]) equal(t, "session_debug.Map.key2", "key2 value", option.Map["key2"]) } }
func TestParseConfig(t *testing.T) { config := defaultConfigString n, err := kson.Parse([]byte(config)) t.Log(n) if err != nil { t.Error("config parse", err) return } ktest.Equal(t, "Log_Level", "debug", n.ChildString("Log_Level")) ktest.Equal(t, "Listen", 8000, n.ChildInt("Listen")) ktest.Equal(t, "role-user", "user", n.MustChild("Roles").List[0].ChildString("Name")) ktest.Equal(t, "role-user-allow", "/user", n.MustChild("Roles").List[0].MustChild("Allow").List[0].Literal) ktest.Equal(t, "dblog-host", "127.0.0.1", n.MustChild("Db_Log").ChildString("Host")) ktest.Equal(t, "env-auth", "http://auth.io", n.MustChild("Env").Hash["auth"].Literal) var newConfig Config err = kson.Unmarshal([]byte(config), &newConfig) if err != nil { t.Error("config unmarshal error", err) return } ktest.Equal(t, "Log_Level", "debug", newConfig.Log_Level) ktest.Equal(t, "Listen", 8000, newConfig.Listen) ktest.Equal(t, "role-user", "user", newConfig.Roles[0].Name) ktest.Equal(t, "role-user-allow", "/user", newConfig.Roles[0].Allow[0]) ktest.Equal(t, "dblog-host", "127.0.0.1", newConfig.Db_Log.Host) ktest.Equal(t, "env-auth", "http://auth.io", newConfig.Env["auth"]) }
func BenchmarkUnmarshal(b *testing.B) { var data string = defaultConfigString var newConfig Config for i := 0; i < b.N; i++ { err := kson.Unmarshal([]byte(data), &newConfig) if err != nil { b.Error("config unmarshal error", err) } } }
func (d *DebugDriver) Init(options string) error { log.Println("Init", options) kson.Unmarshal([]byte(options), d.Option) log.Printf("option: %#v", d.Option) return nil }
func TestUnmarshalStruct(t *testing.T) { data := ` { T1_bool:true T1_int:10 T1_map:{ a:{ A_array_T3:[ { T3_int:2031 T3_string:T3_string_2031 } { T3_int:2032 T3_string:T3_string_2032 } ] T2_array_uint8:[ 2001 2002 2003 ] T2_float:2000 } b:{ A_array_T3:[ { T3_int:2131 T3_string:T3_string_2131 } { T3_int:2132 T3_string:T3_string_2132 } ] T2_array_uint8:[ 2101 2102 2103 ] T2_float:2100 } } T1_t3: { T3_int:31 T3_string:T3_string_31 } T1_string:T1_string } ` var v T1 err := kson.Unmarshal([]byte(data), &v) if err != nil { t.Error("Unmarshal struct", err) return } ktest.Equal(t, "T1_bool", true, v.T1_bool) ktest.Equal(t, "T1_int", 10, v.T1_int) ktest.Equal(t, "T1_string", "T1_string", v.T1_string) ktest.Equal(t, "T1_t3.T3_int", 31, v.T1_t3.T3_int) ktest.Equal(t, "T1_t3.T3_string", "T3_string_31", v.T1_t3.T3_string) ktest.Equal(t, "T1_map_a.T2_float", float32(2000), v.T1_map["a"].T2_float) ktest.Equal(t, "T1_map_a.T2_array_uint8", [3]uint{2001, 2002, 2003}, v.T1_map["a"].T2_array_uint8) ktest.Equal(t, "T1_map_a.A_array_T3.T3_int", 2031, v.T1_map["a"].A_array_T3[0].T3_int) ktest.Equal(t, "T1_map_a.A_array_T3.T3_string", "T3_string_2031", v.T1_map["a"].A_array_T3[0].T3_string) }