示例#1
0
文件: var.go 项目: MogeiWang/py
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
}
示例#2
0
文件: var.go 项目: MogeiWang/py
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
}