Skip to content

Commit c2a2497

Browse files
committed
refine for for better doc
1 parent 77585b4 commit c2a2497

File tree

6 files changed

+132
-132
lines changed

6 files changed

+132
-132
lines changed

bytes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ func (b Bytes) Bytes() []byte {
3434
}
3535

3636
func (b Bytes) Decode(encoding string) Str {
37-
return Cast[Str](b.Call("decode", MakeStr(encoding)))
37+
return cast[Str](b.Call("decode", MakeStr(encoding)))
3838
}

convert.go

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"unsafe"
1212
)
1313

14-
func From(v any) Object {
15-
switch v := v.(type) {
14+
func From(from any) Object {
15+
switch v := from.(type) {
1616
case Objecter:
1717
return newObject(v.Obj())
1818
case int8:
@@ -81,114 +81,114 @@ func From(v any) Object {
8181
}
8282
}
8383

84-
func ToValue(obj Object, v reflect.Value) bool {
85-
if !v.IsValid() || !v.CanSet() {
86-
panic(fmt.Errorf("value is not valid or cannot be set: %v\n", v))
84+
func ToValue(from Object, to reflect.Value) bool {
85+
if !to.IsValid() || !to.CanSet() {
86+
panic(fmt.Errorf("value is not valid or cannot be set: %v\n", to))
8787
}
8888

89-
switch v.Kind() {
89+
switch to.Kind() {
9090
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
91-
if obj.IsLong() {
92-
v.SetInt(Cast[Long](obj).Int64())
91+
if from.IsLong() {
92+
to.SetInt(cast[Long](from).Int64())
9393
} else {
9494
return false
9595
}
9696
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint:
97-
if obj.IsLong() {
98-
v.SetUint(Cast[Long](obj).Uint64())
97+
if from.IsLong() {
98+
to.SetUint(cast[Long](from).Uint64())
9999
} else {
100100
return false
101101
}
102102
case reflect.Float32, reflect.Float64:
103-
if obj.IsFloat() || obj.IsLong() {
104-
v.SetFloat(Cast[Float](obj).Float64())
103+
if from.IsFloat() || from.IsLong() {
104+
to.SetFloat(cast[Float](from).Float64())
105105
} else {
106106
return false
107107
}
108108
case reflect.Complex64, reflect.Complex128:
109-
if obj.IsComplex() {
110-
v.SetComplex(Cast[Complex](obj).Complex128())
109+
if from.IsComplex() {
110+
to.SetComplex(cast[Complex](from).Complex128())
111111
} else {
112112
return false
113113
}
114114
case reflect.String:
115-
if obj.IsStr() {
116-
v.SetString(Cast[Str](obj).String())
115+
if from.IsStr() {
116+
to.SetString(cast[Str](from).String())
117117
} else {
118118
return false
119119
}
120120
case reflect.Bool:
121-
if obj.IsBool() {
122-
v.SetBool(Cast[Bool](obj).Bool())
121+
if from.IsBool() {
122+
to.SetBool(cast[Bool](from).Bool())
123123
} else {
124124
return false
125125
}
126126
case reflect.Slice:
127-
if v.Type().Elem().Kind() == reflect.Uint8 { // []byte
128-
if obj.IsBytes() {
129-
v.SetBytes(Cast[Bytes](obj).Bytes())
127+
if to.Type().Elem().Kind() == reflect.Uint8 { // []byte
128+
if from.IsBytes() {
129+
to.SetBytes(cast[Bytes](from).Bytes())
130130
} else {
131131
return false
132132
}
133133
} else {
134-
if obj.IsList() {
135-
list := Cast[List](obj)
134+
if from.IsList() {
135+
list := cast[List](from)
136136
l := list.Len()
137-
slice := reflect.MakeSlice(v.Type(), l, l)
137+
slice := reflect.MakeSlice(to.Type(), l, l)
138138
for i := 0; i < l; i++ {
139139
item := list.GetItem(i)
140140
ToValue(item, slice.Index(i))
141141
}
142-
v.Set(slice)
142+
to.Set(slice)
143143
} else {
144144
return false
145145
}
146146
}
147147
case reflect.Map:
148-
if obj.IsDict() {
149-
t := v.Type()
150-
v.Set(reflect.MakeMap(t))
151-
dict := Cast[Dict](obj)
148+
if from.IsDict() {
149+
t := to.Type()
150+
to.Set(reflect.MakeMap(t))
151+
dict := cast[Dict](from)
152152
for key, value := range dict.Items() {
153153
vk := reflect.New(t.Key()).Elem()
154154
vv := reflect.New(t.Elem()).Elem()
155155
if !ToValue(key, vk) || !ToValue(value, vv) {
156156
return false
157157
}
158-
v.SetMapIndex(vk, vv)
158+
to.SetMapIndex(vk, vv)
159159
}
160160
return true
161161
} else {
162162
return false
163163
}
164164
case reflect.Struct:
165-
if obj.IsDict() {
166-
dict := Cast[Dict](obj)
167-
t := v.Type()
165+
if from.IsDict() {
166+
dict := cast[Dict](from)
167+
t := to.Type()
168168
for i := 0; i < t.NumField(); i++ {
169169
field := t.Field(i)
170170
key := goNameToPythonName(field.Name)
171171
if !dict.HasKey(MakeStr(key)) {
172172
continue
173173
}
174174
value := dict.Get(MakeStr(key))
175-
if !ToValue(value, v.Field(i)) {
175+
if !ToValue(value, to.Field(i)) {
176176
SetTypeError(fmt.Errorf("failed to convert value to %v", field.Name))
177177
return false
178178
}
179179
}
180180
} else {
181181
maps := getGlobalData()
182-
tyMeta := maps.typeMetas[obj.Type().Obj()]
182+
tyMeta := maps.typeMetas[from.Type().Obj()]
183183
if tyMeta == nil {
184184
return false
185185
}
186-
wrapper := (*wrapperType)(unsafe.Pointer(obj.Obj()))
187-
v.Set(reflect.ValueOf(wrapper.goObj).Elem())
186+
wrapper := (*wrapperType)(unsafe.Pointer(from.Obj()))
187+
to.Set(reflect.ValueOf(wrapper.goObj).Elem())
188188
return true
189189
}
190190
default:
191-
panic(fmt.Errorf("unsupported type conversion from Python object to %v", v.Type()))
191+
panic(fmt.Errorf("unsupported type conversion from Python object to %v", to.Type()))
192192
}
193193
return true
194194
}

float.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ func (f Float) Float32() float32 {
2828
}
2929

3030
func (f Float) IsInteger() Bool {
31-
fn := Cast[Func](f.Attr("is_integer"))
32-
return Cast[Bool](fn.callNoArgs())
31+
fn := cast[Func](f.Attr("is_integer"))
32+
return cast[Bool](fn.callNoArgs())
3333
}

0 commit comments

Comments
 (0)