Skip to content

Commit

Permalink
Condition syntax with boolean, ModelPermissions alternate constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownJoe796 committed Oct 20, 2023
1 parent 594599b commit cc0a4d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ data class ModelPermissions<Model>(
delete = Condition.Always(),
)
}

constructor(
read: Condition<Model>,
readMask: Mask<Model> = Mask(),
manage: Condition<Model>,
updateRestriction: UpdateRestrictions<Model> = UpdateRestrictions(),
) : this(
create = manage,
update = manage,
updateRestrictions = updateRestriction,
read = read,
readMask = readMask,
delete = manage
)
constructor(
all: Condition<Model>,
readMask: Mask<Model> = Mask(),
updateRestriction: UpdateRestrictions<Model> = UpdateRestrictions(),
) : this(
create = all,
update = all,
updateRestrictions = updateRestriction,
read = all,
readMask = readMask,
delete = all
)

/**
* @return a condition defining under what circumstances the given [modification] is permitted in.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ inline fun <reified T : IsCodableAndHashable> path(): DataClassPath<T, T> = Data

inline fun <reified T : IsCodableAndHashable> condition(setup: (DataClassPath<T, T>) -> Condition<T>): Condition<T> =
path<T>().let(setup)
fun <T> condition(boolean: Boolean): Condition<T> = if(boolean) Condition.Always() else Condition.Never()

val <K : IsCodableAndHashable> DataClassPath<K, K>.always: Condition<K> get() = Condition.Always<K>()
val <K : IsCodableAndHashable> DataClassPath<K, K>.never: Condition<K> get() = Condition.Never<K>()

infix fun <K : IsCodableAndHashable, T : IsCodableAndHashable> DataClassPath<K, T>.eq(value: T) = mapCondition(Condition.Equal(value))
infix fun <K : IsCodableAndHashable, T : IsCodableAndHashableNotNull> DataClassPath<K, T>.eqNn(value: T?) = if(value == null) Condition.Never() else mapCondition(Condition.Equal(value))
infix fun <K : IsCodableAndHashable, T : IsCodableAndHashable> DataClassPath<K, T>.neq(value: T) = mapCondition(Condition.NotEqual(value))
@JsName("xDataClassPathNotInSet") infix fun <K : IsCodableAndHashable, T : IsCodableAndHashable> DataClassPath<K, T>.notInside(values: Set<T>) = mapCondition(Condition.NotInside(values.toList()))
infix fun <K : IsCodableAndHashable, T : IsCodableAndHashable> DataClassPath<K, T>.notInside(values: List<T>) = mapCondition(Condition.NotInside(values))
Expand Down

0 comments on commit cc0a4d7

Please sign in to comment.