Skip to content

Commit

Permalink
Merge pull request #34 from ergon/feature/dope-226-add-array-functions
Browse files Browse the repository at this point in the history
DOPE-226: added array functions, CM extension functions and tests
  • Loading branch information
martinagallati-ergon authored Jul 11, 2024
2 parents 7f54bf9 + 206611d commit 0a01401
Show file tree
Hide file tree
Showing 57 changed files with 2,731 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayAppendExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) : ArrayFunctionExpression<T>("ARRAY_APPEND", array, value, *additionalValues)

fun <T : ValidType> arrayAppend(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = ArrayAppendExpression(array, value, *additionalValues)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayAverageExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_AVG", array)

fun <T : ValidType> arrayAverage(array: TypeExpression<ArrayType<T>>) = ArrayAverageExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayConcatExpression<T : ValidType>(
firstArray: TypeExpression<ArrayType<T>>,
secondArray: TypeExpression<ArrayType<T>>,
vararg additionalArrays: TypeExpression<ArrayType<T>>,
) : ArrayFunctionExpression<T>("ARRAY_CONCAT", firstArray, secondArray, *additionalArrays)

fun <T : ValidType> arrayConcat(
firstArray: TypeExpression<ArrayType<T>>,
secondArray: TypeExpression<ArrayType<T>>,
vararg additionalArrays: TypeExpression<ArrayType<T>>,
) = ArrayConcatExpression(firstArray, secondArray, *additionalArrays)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.BooleanType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.StringType
import ch.ergon.dope.validtype.ValidType

class ArrayContainsExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>, value: TypeExpression<T>) :
ArrayFunctionNumberExpression<T>("ARRAY_CONTAINS", array, value)

fun <T : ValidType> arrayContains(array: TypeExpression<ArrayType<T>>, value: TypeExpression<T>) =
ArrayContainsExpression(array, value)

fun arrayContains(array: TypeExpression<ArrayType<StringType>>, value: String) =
arrayContains(array, value.toDopeType())

fun arrayContains(array: TypeExpression<ArrayType<NumberType>>, value: Number) =
arrayContains(array, value.toDopeType())

fun arrayContains(array: TypeExpression<ArrayType<BooleanType>>, value: Boolean) =
arrayContains(array, value.toDopeType())
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayCountExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_COUNT", array)

fun <T : ValidType> arrayCount(array: TypeExpression<ArrayType<T>>) = ArrayCountExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayDistinctExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionExpression<T>("ARRAY_DISTINCT", array)

fun <T : ValidType> arrayDistinct(array: TypeExpression<ArrayType<T>>) = ArrayDistinctExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayExceptExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>, except: TypeExpression<ArrayType<T>>) :
ArrayFunctionExpression<T>("ARRAY_EXCEPT", array, except)

fun <T : ValidType> arrayExcept(array: TypeExpression<ArrayType<T>>, except: TypeExpression<ArrayType<T>>) =
ArrayExceptExpression(array, except)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayFlattenExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>, depth: TypeExpression<NumberType>) :
ArrayFunctionExpression<T>("ARRAY_FLATTEN", array, depth)

fun <T : ValidType> arrayFlatten(array: TypeExpression<ArrayType<T>>, depth: TypeExpression<NumberType>) =
ArrayFlattenExpression(array, depth)

fun <T : ValidType> arrayFlatten(array: TypeExpression<ArrayType<T>>, depth: Number) =
arrayFlatten(array, depth.toDopeType())
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.operator.FunctionOperator
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

sealed class ArrayFunctionExpression<T : ValidType>(
private val symbol: String,
private val array: TypeExpression<ArrayType<T>>,
private vararg val args: TypeExpression<out ValidType>,
private val extra: TypeExpression<out ValidType>? = null,
) : TypeExpression<ArrayType<T>>, FunctionOperator {
override fun toDopeQuery(): DopeQuery {
val arrayDopeQuery = array.toDopeQuery()
val argsDopeQuery = args.map { it.toDopeQuery() }
val extraDopeQuery = extra?.toDopeQuery()
return DopeQuery(
queryString = toFunctionQueryString(symbol, arrayDopeQuery, *argsDopeQuery.toTypedArray(), extra = extraDopeQuery),
parameters = arrayDopeQuery.parameters + argsDopeQuery.fold(
emptyMap(),
) { argsParameters, field -> argsParameters + field.parameters } + extraDopeQuery?.parameters.orEmpty(),
)
}
}

sealed class ArrayFunctionNumberExpression<T : ValidType>(
private val symbol: String,
private val array: TypeExpression<ArrayType<T>>,
private val value: TypeExpression<out ValidType>? = null,
) : TypeExpression<ValidType>, FunctionOperator {
override fun toDopeQuery(): DopeQuery {
val arrayDopeQuery = array.toDopeQuery()
val valueDopeQuery = value?.toDopeQuery()
return DopeQuery(
queryString = toFunctionQueryString(symbol, arrayDopeQuery, extra = valueDopeQuery),
parameters = arrayDopeQuery.parameters + valueDopeQuery?.parameters.orEmpty(),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayIfNullExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_IFNULL", array)

fun <T : ValidType> arrayIfNull(array: TypeExpression<ArrayType<T>>) = ArrayIfNullExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayInsertExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
position: TypeExpression<NumberType>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) : ArrayFunctionExpression<T>("ARRAY_INSERT", array, position, value, *additionalValues)

fun <T : ValidType> arrayInsert(
array: TypeExpression<ArrayType<T>>,
position: TypeExpression<NumberType>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = ArrayInsertExpression(array, position, value, *additionalValues)

fun <T : ValidType> arrayInsert(
array: TypeExpression<ArrayType<T>>,
position: Number,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = arrayInsert(array, position.toDopeType(), value, *additionalValues)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayIntersectExpression<T : ValidType>(
firstArray: TypeExpression<ArrayType<T>>,
secondArray: TypeExpression<ArrayType<T>>,
vararg additionalArrays: TypeExpression<ArrayType<T>>,
) : ArrayFunctionExpression<T>("ARRAY_INTERSECT", firstArray, secondArray, *additionalArrays)

fun <T : ValidType> arrayIntersect(
firstArray: TypeExpression<ArrayType<T>>,
secondArray: TypeExpression<ArrayType<T>>,
vararg additionalArrays: TypeExpression<ArrayType<T>>,
) = ArrayIntersectExpression(firstArray, secondArray, *additionalArrays)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayLengthExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_LENGTH", array)

fun <T : ValidType> arrayLength(array: TypeExpression<ArrayType<T>>) = ArrayLengthExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayMaxExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_MAX", array)

fun <T : ValidType> arrayMax(array: TypeExpression<ArrayType<T>>) = ArrayMaxExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayMinExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>) :
ArrayFunctionNumberExpression<T>("ARRAY_MIN", array)

fun <T : ValidType> arrayMin(array: TypeExpression<ArrayType<T>>) = ArrayMinExpression(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayMoveExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
from: TypeExpression<NumberType>,
to: TypeExpression<NumberType>,
) : ArrayFunctionExpression<T>("ARRAY_MOVE", array, from, to)

fun <T : ValidType> arrayMove(array: TypeExpression<ArrayType<T>>, from: TypeExpression<NumberType>, to: TypeExpression<NumberType>) =
ArrayMoveExpression(array, from, to)

fun <T : ValidType> arrayMove(array: TypeExpression<ArrayType<T>>, from: TypeExpression<NumberType>, to: Number) =
arrayMove(array, from, to.toDopeType())

fun <T : ValidType> arrayMove(array: TypeExpression<ArrayType<T>>, from: Number, to: TypeExpression<NumberType>) =
arrayMove(array, from.toDopeType(), to)

fun <T : ValidType> arrayMove(array: TypeExpression<ArrayType<T>>, from: Number, to: Number) =
arrayMove(array, from.toDopeType(), to.toDopeType())
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.expression.unaliased.type.toDopeType
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayPositionExpression<T : ValidType>(array: TypeExpression<ArrayType<T>>, value: TypeExpression<NumberType>) :
ArrayFunctionNumberExpression<T>("ARRAY_POSITION", array, value)

fun <T : ValidType> arrayPosition(array: TypeExpression<ArrayType<T>>, value: TypeExpression<NumberType>) =
ArrayPositionExpression(array, value)

fun <T : ValidType> arrayPosition(array: TypeExpression<ArrayType<T>>, value: Number) =
arrayPosition(array, value.toDopeType())
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.DopeQuery
import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.operator.FunctionOperator
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayPrependExpression<T : ValidType>(
private val array: TypeExpression<ArrayType<T>>,
private val value: TypeExpression<T>,
private vararg val additionalValues: TypeExpression<T>,
) : TypeExpression<ArrayType<T>>, FunctionOperator {
override fun toDopeQuery(): DopeQuery {
val arrayDopeQuery = array.toDopeQuery()
val valueDopeQuery = value.toDopeQuery()
val additionalValuesDopeQuery = additionalValues.map { it.toDopeQuery() }
return DopeQuery(
queryString = toFunctionQueryString(
"ARRAY_PREPEND",
valueDopeQuery,
*additionalValuesDopeQuery.toTypedArray(),
arrayDopeQuery,
),
parameters = arrayDopeQuery.parameters + valueDopeQuery.parameters + additionalValuesDopeQuery.fold(
emptyMap(),
) { argsParameters, field -> argsParameters + field.parameters },
)
}
}

fun <T : ValidType> arrayPrepend(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = ArrayPrependExpression(array, value, *additionalValues)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayPutExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) : ArrayFunctionExpression<T>("ARRAY_PUT", array, value, *additionalValues)

fun <T : ValidType> arrayPut(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = ArrayPutExpression(array, value, *additionalValues)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.ValidType

class ArrayRemoveExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) : ArrayFunctionExpression<T>("ARRAY_REMOVE", array, value, *additionalValues)

fun <T : ValidType> arrayRemove(
array: TypeExpression<ArrayType<T>>,
value: TypeExpression<T>,
vararg additionalValues: TypeExpression<T>,
) = ArrayRemoveExpression(array, value, *additionalValues)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package ch.ergon.dope.resolvable.expression.unaliased.type.arrayfunction

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.validtype.ArrayType
import ch.ergon.dope.validtype.NumberType
import ch.ergon.dope.validtype.ValidType

class ArrayReplaceExpression<T : ValidType>(
array: TypeExpression<ArrayType<T>>,
toReplace: TypeExpression<T>,
replaceWith: TypeExpression<T>,
max: TypeExpression<NumberType>? = null,
) : ArrayFunctionExpression<T>("ARRAY_REPLACE", array, toReplace, replaceWith, extra = max)

fun <T : ValidType> arrayReplace(
array: TypeExpression<ArrayType<T>>,
toReplace: TypeExpression<T>,
replaceWith: TypeExpression<T>,
max: TypeExpression<NumberType>? = null,
) = ArrayReplaceExpression(array, toReplace, replaceWith, max)
Loading

0 comments on commit 0a01401

Please sign in to comment.