package xorm import ( "fmt" "reflect" "sort" "strconv" "strings" "time" "github.com/go-xorm/core" ) func indexNoCase(s, sep string) int { return strings.Index(strings.ToLower(s), strings.ToLower(sep)) } func splitNoCase(s, sep string) []string { idx := indexNoCase(s, sep) if idx < 0 { return []string{s} } return strings.Split(s, s[idx:idx+len(sep)]) } func splitNNoCase(s, sep string, n int) []string { idx := indexNoCase(s, sep) if idx < 0 { return []string{s} } return strings.SplitN(s, s[idx:idx+len(sep)], n) } func makeArray(elem string, count int) []string { res := make([]string, count) for i := 0; i < count; i++ { res[i] = elem } return res } func rValue(bean interface{}) reflect.Value { return reflect.Indirect(reflect.ValueOf(bean)) } func rType(bean interface{}) reflect.Type { sliceValue := reflect.Indirect(reflect.ValueOf(bean)) //return reflect.TypeOf(sliceValue.Interface()) return sliceValue.Type() } func structName(v reflect.Type) string { for v.Kind() == reflect.Ptr { v = v.Elem() } return v.Name() } func sliceEq(left, right []string) bool { if len(left) != len(right) { return false } sort.Sort(sort.StringSlice(left)) sort.Sort(sort.StringSlice(right)) for i := 0; i < len(left); i++ { if left[i] != right[i] { return false } } return true } func reflect2value(rawValue *reflect.Value) (str string, err error) { aa := reflect.TypeOf((*rawValue).Interface()) vv := reflect.ValueOf((*rawValue).Interface()) switch aa.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: str = strconv.FormatInt(vv.Int(), 10) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: str = strconv.FormatUint(vv.Uint(), 10) case reflect.Float32, reflect.Float64: str = strconv.FormatFloat(vv.Float(), 'f', -1, 64) case reflect.String: str = vv.String() case reflect.Array, reflect.Slice: switch aa.Elem().Kind() { case reflect.Uint8: data := rawValue.Interface().([]byte) str = string(data) default: err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } //时间类型 case reflect.Struct: if aa == core.TimeType { str = rawValue.Interface().(time.Time).Format(time.RFC3339Nano) } else { err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } case reflect.Bool: str = strconv.FormatBool(vv.Bool()) case reflect.Complex128, reflect.Complex64: str = fmt.Sprintf("%v", vv.Complex()) /* TODO: unsupported types below case reflect.Map: case reflect.Ptr: case reflect.Uintptr: case reflect.UnsafePointer: case reflect.Chan, reflect.Func, reflect.Interface: */ default: err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name()) } return } func value2Bytes(rawValue *reflect.Value) (data []byte, err error) { var str string str, err = reflect2value(rawValue) if err != nil { return } data = []byte(str) return } func value2String(rawValue *reflect.Value) (data string, err error) { data, err = reflect2value(rawValue) if err != nil { return } return } func rows2Strings(rows *core.Rows) (resultsSlice []map[string]string, err error) { fields, err := rows.Columns() if err != nil { return nil, err } for rows.Next() { result, err := row2mapStr(rows, fields) if err != nil { return nil, err } resultsSlice = append(resultsSlice, result) } return resultsSlice, nil } func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) { fields, err := rows.Columns() if err != nil { return nil, err } for rows.Next() { result, err := row2map(rows, fields) if err != nil { return nil, err } resultsSlice = append(resultsSlice, result) } return resultsSlice, nil }