Exemple #1
0
func newMap(v reflect.Value, cfg *Config) (ret *py.Base, ok bool) {

	dict := py.NewDict()

	keys := v.MapKeys()
	for _, key := range keys {
		key1, ok1 := NewVarEx(key.Interface(), cfg)
		if !ok1 {
			dict.Decref()
			return nil, false
		}
		val1, ok1 := NewVarEx(v.MapIndex(key).Interface(), cfg)
		if !ok1 {
			key1.Decref()
			dict.Decref()
			return nil, false
		}
		dict.SetItem(key1, val1)
		key1.Decref()
		val1.Decref()
	}

	return dict.Obj(), true
}
Exemple #2
0
func newStruct(sv reflect.Value, cfg *Config) (ret *py.Base, ok bool) {

	dict := py.NewDict()

	st := sv.Type()
	for i := 0; i < sv.NumField(); i++ {
		sf := st.Field(i)
		tag := sf.Tag.Get(cfg.Cate)
		if tag == "" {
			return nil, false
		}
		name := tagName(tag)
		val := sv.Field(i)
		val1, ok1 := NewVarEx(val.Interface(), cfg)
		if !ok1 {
			dict.Decref()
			return nil, false
		}
		dict.SetItemString(name, val1)
		val1.Decref()
	}

	return dict.Obj(), true
}