Переглянути джерело

refactored the way we get empty interfaces of *TypeInfo and renamed the function (*TypeInfo)New()

cwandrews 12 роки тому
батько
коміт
355de4242d
2 змінених файлів з 21 додано та 334 видалено
  1. BIN
      .helpers.go.swp
  2. 21 334
      helpers.go

BIN
.helpers.go.swp


+ 21 - 334
helpers.go

@@ -1,354 +1,41 @@
 package gocql
 
-func ptrOfType(t *TypeInfo) interface{} {
+import "reflect"
+
+func (t *TypeInfo) New() interface{} {
+	return reflect.New(goType(t)).Interface()
+}
+
+func goType(t *TypeInfo) reflect.Type {
 	switch t.Type {
 	case TypeVarchar, TypeAscii:
-		return new(string)
+		return reflect.TypeOf(*new(string))
 	case TypeBigInt, TypeCounter, TypeTimestamp:
-		return new(int64)
+		return reflect.TypeOf(*new(int64))
 	case TypeBlob:
-		return new([]byte)
+		return reflect.TypeOf(*new([]byte))
 	case TypeBoolean:
-		return new(bool)
+		return reflect.TypeOf(*new(bool))
 	case TypeFloat:
-		return new(float32)
+		return reflect.TypeOf(*new(float32))
 	case TypeDouble:
-		return new(float64)
+		return reflect.TypeOf(*new(float64))
 	case TypeInt:
-		return new(int32)
+		return reflect.TypeOf(*new(int32))
 	case TypeUUID, TypeTimeUUID:
-		return new(UUID)
+		return reflect.TypeOf(*new(UUID))
 	case TypeList, TypeSet:
-		switch t.Elem.Type {
-		case TypeVarchar, TypeAscii:
-			return new([]string)
-		case TypeBigInt, TypeCounter, TypeTimestamp:
-			return new([]int64)
-		case TypeBlob:
-			return new([][]byte)
-		case TypeBoolean:
-			return new([]bool)
-		case TypeFloat:
-			return new([]float32)
-		case TypeDouble:
-			return new([]float64)
-		case TypeInt:
-			return new([]int32)
-		case TypeUUID, TypeTimeUUID:
-			return new([]UUID)
-		}
-		return new([][]byte)
-
+		return reflect.SliceOf(goType(t.Elem))
 	case TypeMap:
-		switch t.Key.Type {
-		case TypeVarchar, TypeAscii:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[string]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[string]int64)
-			case TypeBlob:
-				return new(map[string][]byte)
-			case TypeBoolean:
-				return new(map[string]bool)
-			case TypeFloat:
-				return new(map[string]float32)
-			case TypeDouble:
-				return new(map[string]float64)
-			case TypeInt:
-				return new(map[string]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[string]UUID)
-			}			
-		case TypeBigInt, TypeCounter, TypeTimestamp:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[int64]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[int64]int64)
-			case TypeBlob:
-				return new(map[int64][]byte)
-			case TypeBoolean:
-				return new(map[int64]bool)
-			case TypeFloat:
-				return new(map[int64]float32)
-			case TypeDouble:
-				return new(map[int64]float64)
-			case TypeInt:
-				return new(map[int64]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[int64]UUID)
-			}
-		case TypeBlob:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[string]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[string]int64)
-			case TypeBlob:
-				return new(map[string][]byte)
-			case TypeBoolean:
-				return new(map[string]bool)
-			case TypeFloat:
-				return new(map[string]float32)
-			case TypeDouble:
-				return new(map[string]float64)
-			case TypeInt:
-				return new(map[string]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[string]UUID)
-			}
-		case TypeBoolean:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[bool]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[bool]int64)
-			case TypeBlob:
-				return new(map[bool][]byte)
-			case TypeBoolean:
-				return new(map[bool]bool)
-			case TypeFloat:
-				return new(map[bool]float32)
-			case TypeDouble:
-				return new(map[bool]float64)
-			case TypeInt:
-				return new(map[bool]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[bool]UUID)
-			}
-		case TypeFloat:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[float32]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[float32]int64)
-			case TypeBlob:
-				return new(map[float32][]byte)
-			case TypeBoolean:
-				return new(map[float32]bool)
-			case TypeFloat:
-				return new(map[float32]float32)
-			case TypeDouble:
-				return new(map[float32]float64)
-			case TypeInt:
-				return new(map[float32]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[float32]UUID)
-			}
-		case TypeDouble:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[float64]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[float64]int64)
-			case TypeBlob:
-				return new(map[float64][]byte)
-			case TypeBoolean:
-				return new(map[float64]bool)
-			case TypeFloat:
-				return new(map[float64]float32)
-			case TypeDouble:
-				return new(map[float64]float64)
-			case TypeInt:
-				return new(map[float64]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[float64]UUID)
-			}
-		case TypeInt:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[int32]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[int32]int64)
-			case TypeBlob:
-				return new(map[int32][]byte)
-			case TypeBoolean:
-				return new(map[int32]bool)
-			case TypeFloat:
-				return new(map[int32]float32)
-			case TypeDouble:
-				return new(map[int32]float64)
-			case TypeInt:
-				return new(map[int32]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[int32]UUID)
-			}
-		case TypeUUID, TypeTimeUUID:
-			switch t.Elem.Type {
-			case TypeVarchar, TypeAscii:
-				return new(map[UUID]string)
-			case TypeBigInt, TypeCounter, TypeTimestamp:
-				return new(map[UUID]int64)
-			case TypeBlob:
-				return new(map[UUID][]byte)
-			case TypeBoolean:
-				return new(map[UUID]bool)
-			case TypeFloat:
-				return new(map[UUID]float32)
-			case TypeDouble:
-				return new(map[UUID]float64)
-			case TypeInt:
-				return new(map[UUID]int32)
-			case TypeUUID, TypeTimeUUID:
-				return new(map[UUID]UUID)
-			}
-		}
+		return reflect.MapOf(goType(t.Key), goType(t.Elem))
 	default:
-		return new([]byte)
+		return nil
 	}
-	return new(map[string]string)
 }
 
 func dereference(i interface{}) interface{} {
-	switch i.(type) {
-	case *string:
-		return *i.(*string)
-	case *int64:
-		return *i.(*int64)
-	case *[]byte:
-		return *i.(*[]byte)
-	case *bool:
-		return *i.(*bool)
-	case *float32:
-		return *i.(*float32)
-	case *float64:
-		return *i.(*float64)
-	case *int32:
-		return *i.(*int32)
-	case *UUID:
-		return *i.(*UUID)
-	case *[]string:
-		return *i.(*[]string)
-	case *[]int64:
-		return *i.(*[]int64)
-	case *[][]byte:
-		return *i.(*[][]byte)
-	case *[]bool:
-		return *i.(*[]bool)
-	case *[]float32:
-		return *i.(*[]float32)
-	case *[]float64:
-		return *i.(*[]float64)
-	case *[]int32:
-		return *i.(*[]int32)
-	case *[]UUID:
-		return *i.(*[]UUID)
-	case *map[string]string:
-		return *i.(*map[string]string)
-	case *map[string]int64:
-		return *i.(*map[string]int64)
-	case *map[string][]byte:
-		return *i.(*map[string][]byte)
-	case *map[string]bool:
-		return *i.(*map[string]bool)
-	case *map[string]float32:
-		return *i.(*map[string]float32)
-	case *map[string]float64:
-		return *i.(*map[string]float64)
-	case *map[string]int32:
-		return *i.(*map[string]int32)
-	case *map[string]UUID:
-		return *i.(*map[string]UUID)
-	case *map[int64]string:
-		return *i.(*map[int64]string)
-	case *map[int64]int64:
-		return *i.(*map[int64]int64)
-	case *map[int64][]byte:
-		return *i.(*map[int64][]byte)
-	case *map[int64]bool:
-		return *i.(*map[int64]bool)
-	case *map[int64]float32:
-		return *i.(*map[int64]float32)
-	case *map[int64]float64:
-		return *i.(*map[int64]float64)
-	case *map[int64]int32:
-		return *i.(*map[int64]int32)
-	case *map[int64]UUID:
-		return *i.(*map[int64]UUID)
-	case *map[bool]string:
-		return *i.(*map[bool]string)
-	case *map[bool]int64:
-		return *i.(*map[bool]int64)
-	case *map[bool][]byte:
-		return *i.(*map[bool][]byte)
-	case *map[bool]bool:
-		return *i.(*map[bool]bool)
-	case *map[bool]float32:
-		return *i.(*map[bool]float32)
-	case *map[bool]float64:
-		return *i.(*map[bool]float64)
-	case *map[bool]int32:
-		return *i.(*map[bool]int32)
-	case *map[bool]UUID:
-		return *i.(*map[bool]UUID)
-	case *map[float32]string:
-		return *i.(*map[float32]string)
-	case *map[float32]int64:
-		return *i.(*map[float32]int64)
-	case *map[float32][]byte:
-		return *i.(*map[float32][]byte)
-	case *map[float32]bool:
-		return *i.(*map[float32]bool)
-	case *map[float32]float32:
-		return *i.(*map[float32]float32)
-	case *map[float32]float64:
-		return *i.(*map[float32]float64)
-	case *map[float32]int32:
-		return *i.(*map[float32]int32)
-	case *map[float32]UUID:
-		return *i.(*map[float32]UUID)
-	case *map[float64]string:
-		return *i.(*map[float64]string)
-	case *map[float64]int64:
-		return *i.(*map[float64]int64)
-	case *map[float64][]byte:
-		return *i.(*map[float64][]byte)
-	case *map[float64]bool:
-		return *i.(*map[float64]bool)
-	case *map[float64]float32:
-		return *i.(*map[float64]float32)
-	case *map[float64]float64:
-		return *i.(*map[float64]float64)
-	case *map[float64]int32:
-		return *i.(*map[float64]int32)
-	case *map[float64]UUID:
-		return *i.(*map[float64]UUID)
-	case *map[int32]string:
-		return *i.(*map[int32]string)
-	case *map[int32]int64:
-		return *i.(*map[int32]int64)
-	case *map[int32][]byte:
-		return *i.(*map[int32][]byte)
-	case *map[int32]bool:
-		return *i.(*map[int32]bool)
-	case *map[int32]float32:
-		return *i.(*map[int32]float32)
-	case *map[int32]float64:
-		return *i.(*map[int32]float64)
-	case *map[int32]int32:
-		return *i.(*map[int32]int32)
-	case *map[int32]UUID:
-		return *i.(*map[int32]UUID)
-	case *map[UUID]string:
-		return *i.(*map[UUID]string)
-	case *map[UUID]int64:
-		return *i.(*map[UUID]int64)
-	case *map[UUID][]byte:
-		return *i.(*map[UUID][]byte)
-	case *map[UUID]bool:
-		return *i.(*map[UUID]bool)
-	case *map[UUID]float32:
-		return *i.(*map[UUID]float32)
-	case *map[UUID]float64:
-		return *i.(*map[UUID]float64)
-	case *map[UUID]int32:
-		return *i.(*map[UUID]int32)
-	case *map[UUID]UUID:
-		return *i.(*map[UUID]UUID)
-	default:
-		return i
-	}
+	return reflect.Indirect(reflect.ValueOf(i)).Interface()
+
 }
 
 // SliceMap is a helper function to make the API easier to use
@@ -359,7 +46,7 @@ func (iter *Iter) SliceMap() ([]map[string]interface{}, error) {
 	}
 	interfacesToScan := make([]interface{}, 0)
 	for _, column := range iter.Columns() {
-		i := ptrOfType(column.TypeInfo)
+		i := column.TypeInfo.New()
 		interfacesToScan = append(interfacesToScan, i)
 	}
 	dataToReturn := make([]map[string]interface{}, 0)