Skip to content

Commit

Permalink
Kotlin guidelines (#252)
Browse files Browse the repository at this point in the history
* Use official kotlin style

[publish skip]

Co-authored-by: Michael Rittmeister <[email protected]>
  • Loading branch information
HopeBaron and DRSchlaubi authored Apr 15, 2021
1 parent 96422d4 commit ce7f0a1
Show file tree
Hide file tree
Showing 265 changed files with 3,279 additions and 2,719 deletions.
13 changes: 8 additions & 5 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,31 @@ object Versions {
@Suppress("ObjectPropertyName")
object Dependencies {
const val jdk8 = "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
const val `kotlinx-serialization` = "org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.kotlinxSerialization}"
const val `kotlinx-serialization-json` = "org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinxSerialization}"
const val `kotlinx-serialization` =
"org.jetbrains.kotlinx:kotlinx-serialization-core:${Versions.kotlinxSerialization}"
const val `kotlinx-serialization-json` =
"org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.kotlinxSerialization}"
const val `kotlinx-coroutines` = "org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.kotlinxCoroutines}"
const val `kotlinx-atomicfu` = "org.jetbrains.kotlinx:atomicfu-jvm:${Versions.atomicFu}"

const val `kotlin-logging` = "io.github.microutils:kotlin-logging:${Versions.kotlinLogging}"

const val `kotlin-test` = "org.jetbrains.kotlin:kotlin-test:${Versions.kotlinTest}"
const val junit5 = "org.jetbrains.kotlin:kotlin-test-junit5:${Versions.kotlinTest}"
const val `kotlinx-coroutines-test` = "org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinxCoroutinesTest}"
const val `kotlinx-coroutines-test` =
"org.jetbrains.kotlinx:kotlinx-coroutines-test:${Versions.kotlinxCoroutinesTest}"
const val `kotlin-reflect` = "org.jetbrains.kotlin:kotlin-reflect:${Versions.kotlinReflect}"
const val `junit-jupiter-api` = "org.junit.jupiter:junit-jupiter-api:${Versions.junitJupiterApi}"
const val `junit-jupiter-engine` = "org.junit.jupiter:junit-jupiter-engine:${Versions.junitJupiterEngine}"
const val sl4j = "org.slf4j:slf4j-simple:${Versions.sl4j}"

const val `ktor-client-json` = "io.ktor:ktor-client-json:${Versions.ktor}"
const val `ktor-client-json` = "io.ktor:ktor-client-json:${Versions.ktor}"
const val `ktor-client-json-jvm` = "io.ktor:ktor-client-json-jvm:${Versions.ktor}"
const val `ktor-client-serialization-jvm` = "io.ktor:ktor-client-serialization-jvm:${Versions.ktor}"
const val `ktor-client-cio` = "io.ktor:ktor-client-cio:${Versions.ktor}"
const val `ktor-client-websocket` = "io.ktor:ktor-client-websockets:${Versions.ktor}"
const val `ktor-client-mock` = "io.ktor:ktor-client-mock:${Versions.ktor}"
const val `ktor-client-mock-jvm`= "io.ktor:ktor-client-mock-jvm:${Versions.ktor}"
const val `ktor-client-mock-jvm` = "io.ktor:ktor-client-mock-jvm:${Versions.ktor}"

const val `cache-api` = "dev.kord.cache:cache-api"
const val `cache-map` = "dev.kord.cache:cache-map"
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/Projects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ object Library {
const val name = "kord"
const val group = "dev.kord"
val version: String
get() = if (isJitPack) System.getenv("RELEASE_TAG")
get() = if (isJitPack) System.getenv("RELEASE_TAG")
else {
val tag = System.getenv("GITHUB_TAG_NAME")
val branch = System.getenv("GITHUB_BRANCH_NAME")
when {
when {
!tag.isNullOrBlank() -> tag
!branch.isNullOrBlank() && branch.startsWith("refs/heads/") ->
branch.substringAfter("refs/heads/").replace("/", "-") + "-SNAPSHOT"
Expand Down
11 changes: 10 additions & 1 deletion common/src/main/kotlin/annotation/Annotations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,14 @@ annotation class KordUnsafe
* Marks the annotated declaration as deprecated since [version].
*/
@MustBeDocumented
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.TYPEALIAS)
@Target(
AnnotationTarget.CLASS,
AnnotationTarget.FUNCTION,
AnnotationTarget.PROPERTY,
AnnotationTarget.ANNOTATION_CLASS,
AnnotationTarget.CONSTRUCTOR,
AnnotationTarget.PROPERTY_SETTER,
AnnotationTarget.PROPERTY_GETTER,
AnnotationTarget.TYPEALIAS
)
annotation class DeprecatedSinceKord(val version: String)
148 changes: 79 additions & 69 deletions common/src/main/kotlin/entity/AuditLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,87 +18,87 @@ import dev.kord.common.entity.VerificationLevel as CommonVerificationLevel

@Serializable
data class DiscordAuditLog(
val webhooks: List<DiscordWebhook>,
val users: List<DiscordUser>,
@SerialName("audit_log_entries")
val auditLogEntries: List<DiscordAuditLogEntry>,
val integrations: List<DiscordPartialIntegration>,
val webhooks: List<DiscordWebhook>,
val users: List<DiscordUser>,
@SerialName("audit_log_entries")
val auditLogEntries: List<DiscordAuditLogEntry>,
val integrations: List<DiscordPartialIntegration>,
)

@Serializable
data class DiscordAuditLogEntry(
@SerialName("target_id")
val targetId: Snowflake?,
val changes: Optional<List<AuditLogChange<in @Contextual Any?>>> = Optional.Missing(),
@SerialName("user_id")
val userId: Snowflake,
val id: Snowflake,
@SerialName("action_type")
val actionType: AuditLogEvent,
val options: Optional<AuditLogEntryOptionalInfo> = Optional.Missing(),
val reason: Optional<String> = Optional.Missing(),
@SerialName("target_id")
val targetId: Snowflake?,
val changes: Optional<List<AuditLogChange<in @Contextual Any?>>> = Optional.Missing(),
@SerialName("user_id")
val userId: Snowflake,
val id: Snowflake,
@SerialName("action_type")
val actionType: AuditLogEvent,
val options: Optional<AuditLogEntryOptionalInfo> = Optional.Missing(),
val reason: Optional<String> = Optional.Missing(),
) {

@Suppress("UNCHECKED_CAST")
operator fun <T> get(value: AuditLogChangeKey<T>): AuditLogChange<T>? =
changes.orEmpty().firstOrNull { it.key == value } as? AuditLogChange<T>
changes.orEmpty().firstOrNull { it.key == value } as? AuditLogChange<T>

}

@Serializable
data class AuditLogEntryOptionalInfo(
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("delete_member_days")
val deleteMemberDays: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("members_removed")
val membersRemoved: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("channel_id")
val channelId: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("message_id")
val messageId: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val count: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val id: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val type: Optional<OverwriteType> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("role_name")
val roleName: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("delete_member_days")
val deleteMemberDays: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("members_removed")
val membersRemoved: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("channel_id")
val channelId: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("message_id")
val messageId: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val count: Optional<String> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val id: OptionalSnowflake = OptionalSnowflake.Missing,
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
val type: Optional<OverwriteType> = Optional.Missing(),
/*
Do not trust the docs:
2020-11-12 field is described as present but is in fact optional
*/
@SerialName("role_name")
val roleName: Optional<String> = Optional.Missing(),
)

@Serializable(with = AuditLogChange.Serializer::class)
data class AuditLogChange<T>(
val new: T?,
val old: T?,
val key: AuditLogChangeKey<T>,
val new: T?,
val old: T?,
val key: AuditLogChangeKey<T>,
) {

internal class Serializer<T>(val ser: KSerializer<T>) : KSerializer<AuditLogChange<T>> {
Expand All @@ -117,7 +117,11 @@ data class AuditLogChange<T>(
when (val index = decodeElementIndex(descriptor)) {
0 -> new = decodeSerializableElement(descriptor, index, JsonElement.serializer())
1 -> old = decodeSerializableElement(descriptor, index, JsonElement.serializer())
2 -> key = decodeSerializableElement(descriptor, index, AuditLogChangeKey.Serializer(Unit.serializer()))
2 -> key = decodeSerializableElement(
descriptor,
index,
AuditLogChangeKey.Serializer(Unit.serializer())
)
CompositeDecoder.DECODE_DONE -> break
else -> throw SerializationException("unknown index: $index")
}
Expand All @@ -127,7 +131,11 @@ data class AuditLogChange<T>(
val oldVal = old?.let { Json.decodeFromJsonElement(key.serializer, old) }

@Suppress("UNCHECKED_CAST")
return AuditLogChange(new = newVal, old = oldVal, key = key as AuditLogChangeKey<Any?>) as AuditLogChange<T>
return AuditLogChange(
new = newVal,
old = oldVal,
key = key as AuditLogChangeKey<Any?>
) as AuditLogChange<T>
}
}

Expand All @@ -148,7 +156,7 @@ sealed class AuditLogChangeKey<T>(val name: String, val serializer: KSerializer<

override fun toString(): String = "AuditLogChangeKey(name=$name)"

class Unknown(name: String): AuditLogChangeKey<JsonElement>(name, JsonElement.serializer())
class Unknown(name: String) : AuditLogChangeKey<JsonElement>(name, JsonElement.serializer())

@SerialName("name")
object Name : AuditLogChangeKey<String>("name", serializer())
Expand Down Expand Up @@ -178,10 +186,12 @@ sealed class AuditLogChangeKey<T>(val name: String, val serializer: KSerializer<
object VerificationLevel : AuditLogChangeKey<CommonVerificationLevel>("verification_level", serializer())

@SerialName("explicit_content_filter")
object ExplicitContentFilter : AuditLogChangeKey<CommonExplicitContentFilter>("explicit_content_filter", serializer())
object ExplicitContentFilter :
AuditLogChangeKey<CommonExplicitContentFilter>("explicit_content_filter", serializer())

@SerialName("default_message_notifications")
object DefaultMessageNotificationLevel : AuditLogChangeKey<CommonDefaultMessageNotificationLevel>("default_message_notifications", serializer())
object DefaultMessageNotificationLevel :
AuditLogChangeKey<CommonDefaultMessageNotificationLevel>("default_message_notifications", serializer())

@SerialName("vanity_url_code")
object VanityUrlCode : AuditLogChangeKey<String>("vanity_url_code", serializer())
Expand Down
38 changes: 19 additions & 19 deletions common/src/main/kotlin/entity/Data.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,28 @@ import kotlinx.serialization.Serializable

@Serializable
data class DiscordPinsUpdateData(
@SerialName("guild_id")
val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("last_pin_timestamp")
/*
Do not trust the docs:
2020-11-06 Docs mention this being optional only, but unpinning a channel results
in this field being null.
*/
val lastPinTimestamp: Optional<String?> = Optional.Missing()
@SerialName("guild_id")
val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("last_pin_timestamp")
/*
Do not trust the docs:
2020-11-06 Docs mention this being optional only, but unpinning a channel results
in this field being null.
*/
val lastPinTimestamp: Optional<String?> = Optional.Missing()
)

@Serializable
data class DiscordTyping(
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("guild_id")
val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("user_id")
val userId: Snowflake,
val timestamp: Long,
val member: Optional<DiscordGuildMember> = Optional.Missing()
@SerialName("channel_id")
val channelId: Snowflake,
@SerialName("guild_id")
val guildId: OptionalSnowflake = OptionalSnowflake.Missing,
@SerialName("user_id")
val userId: Snowflake,
val timestamp: Long,
val member: Optional<DiscordGuildMember> = Optional.Missing()
)

Loading

0 comments on commit ce7f0a1

Please sign in to comment.