func constructRequestValues()

in cassandra-bigtable-migration-tools/cassandra-bigtable-proxy/bigtable/select_operation.go [170:239]


func constructRequestValues(value interface{}) (*btpb.Value, error) {
	switch v := value.(type) {
	case string:
		return &btpb.Value{
			Kind: &btpb.Value_StringValue{StringValue: v},
			Type: &btpb.Type{Kind: &btpb.Type_StringType{}},
		}, nil
	case int32:
		return &btpb.Value{
			Kind: &btpb.Value_IntValue{IntValue: int64(v)},
			Type: &btpb.Type{Kind: &btpb.Type_Int64Type{}},
		}, nil
	case int64:
		return &btpb.Value{
			Kind: &btpb.Value_IntValue{IntValue: v},
			Type: &btpb.Type{Kind: &btpb.Type_Int64Type{}},
		}, nil
	case float64:
		return &btpb.Value{
			Kind: &btpb.Value_FloatValue{FloatValue: v},
			Type: &btpb.Type{Kind: &btpb.Type_Float64Type{}},
		}, nil
	case float32:
		return &btpb.Value{
			Kind: &btpb.Value_FloatValue{FloatValue: float64(v)},
			Type: &btpb.Type{Kind: &btpb.Type_Float32Type{}},
		}, nil
	default:
		val := reflect.ValueOf(value)

		// Return early if value is not a slice
		if val.Kind() != reflect.Slice {
			return nil, fmt.Errorf("unsupported type: %T", value)
		}

		// Return early if slice is empty
		if val.Len() == 0 {
			return &btpb.Value{
				Kind: &btpb.Value_ArrayValue{ArrayValue: &btpb.ArrayValue{Values: []*btpb.Value{}}},
				Type: &btpb.Type{Kind: &btpb.Type_ArrayType{ArrayType: &btpb.Type_Array{ElementType: nil}}},
			}, nil
		}

		// Process array values
		arrayValues := make([]*btpb.Value, val.Len())
		var elementType *btpb.Type

		for i := 0; i < val.Len(); i++ {
			elem := val.Index(i).Interface()
			btpbValue, err := constructRequestValues(elem)
			if err != nil {
				return nil, fmt.Errorf("unsupported element type in array: %v", err)
			}

			// Ensure homogeneous array
			if elementType == nil {
				elementType = btpbValue.Type
			} else if !reflect.DeepEqual(elementType, btpbValue.Type) {
				return nil, fmt.Errorf("heterogeneous array detected: elements must be of the same type")
			}

			arrayValues[i] = btpbValue
		}

		return &btpb.Value{
			Kind: &btpb.Value_ArrayValue{ArrayValue: &btpb.ArrayValue{Values: arrayValues}},
			Type: &btpb.Type{Kind: &btpb.Type_ArrayType{ArrayType: &btpb.Type_Array{ElementType: elementType}}},
		}, nil
	}
}