func ConvertToTypedMap()

in cassandra-bigtable-migration-tools/cassandra-bigtable-proxy/collectiondecoder/collectiondecoder.go [250:436]


func ConvertToTypedMap(decodedMap map[interface{}]interface{}, keyType, valueType datatype.DataType) (interface{}, error) {
	switch keyType.GetDataTypeCode() {
	case primitive.DataTypeCodeAscii, primitive.DataTypeCodeVarchar:
		switch valueType.GetDataTypeCode() {
		case primitive.DataTypeCodeBigint, primitive.DataTypeCodeCounter:
			typedMap := make(map[string]int64, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int64)
			}
			return typedMap, nil
		case primitive.DataTypeCodeInt:
			typedMap := make(map[string]int32, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int32)
			}
			return typedMap, nil
		case primitive.DataTypeCodeFloat:
			typedMap := make(map[string]float32, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(float32)
			}
			return typedMap, nil
		case primitive.DataTypeCodeDouble:
			typedMap := make(map[string]float64, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(float64)
			}
			return typedMap, nil
		case primitive.DataTypeCodeBoolean:
			typedMap := make(map[string]bool, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(bool)
			}
			return typedMap, nil
		case primitive.DataTypeCodeTimestamp, primitive.DataTypeCodeDate, primitive.DataTypeCodeTime:
			typedMap := make(map[string]int64, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int64)
			}
			return typedMap, nil
		case primitive.DataTypeCodeUuid, primitive.DataTypeCodeTimeuuid:
			typedMap := make(map[string]string, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(string)
			}
			return typedMap, nil
		case primitive.DataTypeCodeDecimal, primitive.DataTypeCodeVarint:
			typedMap := make(map[string]string, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(string)
			}
			return typedMap, nil
		case primitive.DataTypeCodeBlob, primitive.DataTypeCodeInet:
			typedMap := make(map[string][]byte, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.([]byte)
			}
			return typedMap, nil
		case primitive.DataTypeCodeSmallint:
			typedMap := make(map[string]int16, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int16)
			}
			return typedMap, nil
		case primitive.DataTypeCodeTinyint:
			typedMap := make(map[string]int8, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int8)
			}
			return typedMap, nil
		case primitive.DataTypeCodeDuration:
			typedMap := make(map[string]int64, len(decodedMap))
			for key, value := range decodedMap {
				typedMap[key.(string)] = value.(int64)
			}
			return typedMap, nil
		default:
			return nil, fmt.Errorf("unsupported map value data type: %v", valueType.GetDataTypeCode())
		}
	case primitive.DataTypeCodeTimestamp:
		switch valueType.GetDataTypeCode() {
		case primitive.DataTypeCodeAscii, primitive.DataTypeCodeVarchar:
			typedMap := make(map[time.Time]string, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				stringValue, ok := value.(string)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect string but got %T", value)
				}
				typedMap[timestampKey] = stringValue
			}
			return typedMap, nil
		case primitive.DataTypeCodeInt:
			typedMap := make(map[time.Time]int32, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				int32value, ok := value.(int32)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect int but got %T", value)
				}
				typedMap[timestampKey] = int32value
			}
			return typedMap, nil
		case primitive.DataTypeCodeBigint:
			typedMap := make(map[time.Time]int64, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				int64value, ok := value.(int64)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect bigint but got %T", value)
				}
				typedMap[timestampKey] = int64value
			}
			return typedMap, nil
		case primitive.DataTypeCodeFloat:
			typedMap := make(map[time.Time]float32, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				float32value, ok := value.(float32)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect float but got %T", value)
				}
				typedMap[timestampKey] = float32value
			}
			return typedMap, nil
		case primitive.DataTypeCodeDouble:
			typedMap := make(map[time.Time]float64, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				float64value, ok := value.(float64)
				if !ok {
					return nil, fmt.Errorf("invalid value type, double string but got %T", value)
				}
				typedMap[timestampKey] = float64value
			}
			return typedMap, nil
		case primitive.DataTypeCodeBoolean:
			typedMap := make(map[time.Time]bool, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				boolValue, ok := value.(bool)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect boolean but got %T", value)
				}
				typedMap[timestampKey] = boolValue
			}
			return typedMap, nil
		case primitive.DataTypeCodeTimestamp:
			typedMap := make(map[time.Time]time.Time, len(decodedMap))
			for key, value := range decodedMap {
				timestampKey, ok := key.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid key type, expect time.Time but got %T", key)
				}
				timeValue, ok := value.(time.Time)
				if !ok {
					return nil, fmt.Errorf("invalid value type, expect timestamp but got %T", value)
				}
				typedMap[timestampKey] = timeValue
			}
			return typedMap, nil
		default:
			return nil, fmt.Errorf("unsupported map value data type: %v", valueType.GetDataTypeCode())
		}

	default:
		return nil, fmt.Errorf("unsupported map key data type: %v", keyType.GetDataTypeCode())
	}
}