Beispiel #1
0
func parse() {
	fmt.Println("parse struct")

	var u1 User
	var u2 = map[string]string{
		"Name":   "tom",
		"Age":    "11",
		"Single": "true"}

	v1 := reflect.ValueOf(&u1).Elem()
	var src kiss.StrGetFunc = func(name string) (string, bool) {
		x, ok := u2[name]
		return x, ok
	}
	kiss.ParseStruct(v1, src)

	fmt.Println(u1)
}
Beispiel #2
0
func TestParseStruct(t *testing.T) {
	var t1 SimpleType
	var t2 map[string]string = strMap
	v1 := reflect.ValueOf(&t1).Elem()
	var src kiss.StrGetFunc = func(name string) (string, bool) {
		x, ok := t2[name]
		return x, ok
	}
	kiss.ParseStruct(v1, src)

	for i := 0; i < v1.Type().NumField(); i++ {
		f := v1.Type().Field(i)
		except, ok := t2[f.Name]
		actual := fmt.Sprint(v1.FieldByName(f.Name).Interface())
		if !ok || except != actual {
			t.Errorf("extend field %s fail, except %s, actual %s", f.Name, except, actual)
		}
	}
}