mirror of
https://github.com/uptrace/go-clickhouse.git
synced 2025-06-08 23:26:11 +02:00
75 lines
1.2 KiB
Go
75 lines
1.2 KiB
Go
package chschema
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
func indirect(v reflect.Value) reflect.Value {
|
|
switch v.Kind() {
|
|
case reflect.Interface:
|
|
return indirect(v.Elem())
|
|
case reflect.Ptr:
|
|
return v.Elem()
|
|
default:
|
|
return v
|
|
}
|
|
}
|
|
|
|
func indirectType(t reflect.Type) reflect.Type {
|
|
if t.Kind() == reflect.Ptr {
|
|
t = t.Elem()
|
|
}
|
|
return t
|
|
}
|
|
|
|
func fieldByIndex(v reflect.Value, index []int) (_ reflect.Value, ok bool) {
|
|
if len(index) == 1 {
|
|
return v.Field(index[0]), true
|
|
}
|
|
|
|
for i, idx := range index {
|
|
if i > 0 {
|
|
if v.Kind() == reflect.Ptr {
|
|
if v.IsNil() {
|
|
return v, false
|
|
}
|
|
v = v.Elem()
|
|
}
|
|
}
|
|
v = v.Field(idx)
|
|
}
|
|
return v, true
|
|
}
|
|
|
|
func fieldByIndexAlloc(v reflect.Value, index []int) reflect.Value {
|
|
if len(index) == 1 {
|
|
return v.Field(index[0])
|
|
}
|
|
|
|
for i, idx := range index {
|
|
if i > 0 {
|
|
v = indirectNil(v)
|
|
}
|
|
v = v.Field(idx)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func indirectNil(v reflect.Value) reflect.Value {
|
|
if v.Kind() == reflect.Ptr {
|
|
if v.IsNil() {
|
|
v.Set(reflect.New(v.Type().Elem()))
|
|
}
|
|
v = v.Elem()
|
|
}
|
|
return v
|
|
}
|
|
|
|
func sliceElemType(v reflect.Value) reflect.Type {
|
|
elemType := v.Type().Elem()
|
|
if elemType.Kind() == reflect.Interface && v.Len() > 0 {
|
|
return indirect(v.Index(0).Elem()).Type()
|
|
}
|
|
return indirectType(elemType)
|
|
}
|