-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Unify permission overwrite API (Fix #302) * Don't use optional in create builders * Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys <[email protected]> * Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys <[email protected]> * Improve documentation of PermissionOverritesBuilder Co-authored-by: Bart Arys <[email protected]> * Make interface use a function instead of a property * Make PermissionOverwritesBuilder sealed Co-authored-by: Bart Arys <[email protected]> Co-authored-by: Bart Arys <[email protected]>
- Loading branch information
1 parent
330d0d5
commit 07e64fb
Showing
10 changed files
with
132 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
rest/src/main/kotlin/builder/channel/PermissionOverwritesBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dev.kord.rest.builder.channel | ||
|
||
import dev.kord.common.entity.Overwrite | ||
import dev.kord.common.entity.OverwriteType | ||
import dev.kord.common.entity.Snowflake | ||
import kotlin.contracts.ExperimentalContracts | ||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
/** | ||
* Builder that can modify [permission overwrites][Overwrite]. | ||
*/ | ||
sealed interface PermissionOverwritesBuilder { | ||
/** | ||
* Adds [overwrite] to this builder. | ||
*/ | ||
fun addOverwrite(overwrite: Overwrite) | ||
} | ||
|
||
/** | ||
* [PermissionOverwritesBuilder] which creates an entity with overwrites. | ||
*/ | ||
interface PermissionOverwritesCreateBuilder : PermissionOverwritesBuilder { | ||
var permissionOverwrites: MutableSet<Overwrite> | ||
|
||
override fun addOverwrite(overwrite: Overwrite) { | ||
permissionOverwrites.add(overwrite) | ||
} | ||
} | ||
|
||
/** | ||
* [PermissionOverwritesBuilder] which modifies an existing entity with overwrites. | ||
*/ | ||
interface PermissionOverwritesModifyBuilder : PermissionOverwritesBuilder { | ||
var permissionOverwrites: MutableSet<Overwrite>? | ||
|
||
override fun addOverwrite(overwrite: Overwrite) { | ||
val overwrites = permissionOverwrites ?: mutableSetOf() | ||
overwrites.add(overwrite) | ||
|
||
permissionOverwrites = overwrites | ||
} | ||
} | ||
|
||
/** | ||
* Adds an [Overwrite] for the [memberId]. | ||
*/ | ||
@OptIn(ExperimentalContracts::class) | ||
inline fun PermissionOverwritesBuilder.addMemberOverwrite( | ||
memberId: Snowflake, | ||
builder: PermissionOverwriteBuilder.() -> Unit | ||
) { | ||
contract { | ||
callsInPlace(builder, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val overwrite = PermissionOverwriteBuilder(OverwriteType.Member, memberId).apply(builder).toOverwrite() | ||
|
||
return addOverwrite(overwrite) | ||
} | ||
|
||
/** | ||
* Adds an [Overwrite] for the [roleId]. | ||
*/ | ||
@OptIn(ExperimentalContracts::class) | ||
inline fun PermissionOverwritesBuilder.addRoleOverwrite( | ||
roleId: Snowflake, | ||
builder: PermissionOverwriteBuilder.() -> Unit | ||
) { | ||
contract { | ||
callsInPlace(builder, InvocationKind.EXACTLY_ONCE) | ||
} | ||
val overwrite = PermissionOverwriteBuilder(OverwriteType.Member, roleId).apply(builder).toOverwrite() | ||
|
||
return addOverwrite(overwrite) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.