Skip to content

Commit

Permalink
feat: constants with type-constrains (fixed)
Browse files Browse the repository at this point in the history
  • Loading branch information
y9vad9 committed Mar 19, 2024
1 parent 601cb3c commit 9c428c4
Show file tree
Hide file tree
Showing 28 changed files with 211 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal object StartAuthorizationCommand : RSocketCommand<StartAuthorizationReq
).let { result ->
StartAuthorizationRequest.Result(
verificationHash = VerificationHash.createOrThrow(result.verificationHash),
attempts = Count.createOrThrow(result.attempts),
attempts = Count.factory.createOrThrow(result.attempts),
expiresAt = Instant.fromEpochMilliseconds(result.expiresAt),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal fun RSTimer.sdk(): SdkTimer {
name = TimerName.createOrThrow(name),
description = TimerDescription.createOrThrow(description),
ownerId = UserId.createOrThrow(ownerId),
membersCount = Count.createOrThrow(membersCount),
membersCount = Count.factory.createOrThrow(membersCount),
state = currentState?.sdk() ?: SdkTimerState.Inactive(Instant.DISTANT_PAST),
settings = settings?.sdk() ?: TimerSettings(),
)
Expand Down Expand Up @@ -64,7 +64,7 @@ internal fun RSTimer.Settings.sdk(): SdkTimerSettings {
restTime = restTime.minutes,
bigRestTime = bigRestTime.minutes,
bigRestEnabled = bigRestEnabled,
bigRestPer = Count.createOrThrow(bigRestPer),
bigRestPer = Count.factory.createOrThrow(bigRestPer),
isEveryoneCanPause = isEveryoneCanPause,
isConfirmationRequired = isConfirmationRequired,
)
Expand All @@ -84,6 +84,6 @@ internal fun RSInvite.sdk(): SdkInvite {
return SdkInvite(
inviteCode = InviteCode.createOrThrow(code),
creationTime = Instant.fromEpochMilliseconds(creationTime),
limit = Count.createOrThrow(limit),
limit = Count.factory.createOrThrow(limit),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.lengthExact
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class VerificationHash private constructor(public val string: String) {
public companion object : Factory<VerificationHash, String> by factory(
rules = listOf(
ValidationRule.lengthExact(128),
),
constructor = ::VerificationHash
)
public companion object {
@JvmStatic
public val REQUIRED_LENGTH: Int = 128

@JvmStatic
public val factory: Factory<VerificationHash, String> = factory(
rules = listOf(
ValidationRule.lengthExact(REQUIRED_LENGTH),
),
constructor = ::VerificationHash
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class AuthorizedSessionsApi(
@Suppress("NAME_SHADOWING")
public fun AuthorizedSessionsApi.getSessionsPages(
pageToken: PageToken?,
maxRetries: Count = Count.createOrThrow(5),
maxRetries: Count = Count.factory.createOrThrow(5),
initialDelayOnRetries: Duration = 1.seconds,
): PagesIterator<Authorization> = PagesIteratorImpl(
initialPageToken = pageToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package org.timemates.sdk.authorization.sessions.types.value
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class ApplicationName private constructor(public val string: String) {
public companion object : Factory<ApplicationName, String> by factory(::ApplicationName)
public companion object {
@JvmStatic
public val factory: Factory<ApplicationName, String> = factory(::ApplicationName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package org.timemates.sdk.authorization.sessions.types.value
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class AuthorizationId private constructor(public val int: Int) {
public companion object : Factory<AuthorizationId, Int> by factory(::AuthorizationId)
public companion object {
@JvmStatic
public val factory: Factory<AuthorizationId, Int> = factory(::AuthorizationId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package org.timemates.sdk.authorization.sessions.types.value
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class ClientIpAddress private constructor(public val string: String) {
public companion object : Factory<ClientIpAddress, String> by factory(::ClientIpAddress)
public companion object {
@JvmStatic
public val factory: Factory<ClientIpAddress, String> = factory(::ClientIpAddress)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package org.timemates.sdk.authorization.sessions.types.value
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class ClientVersion private constructor(public val double: Double) {
public companion object : Factory<ClientVersion, Double> by factory(::ClientVersion)
public companion object {
@JvmStatic
public val factory: Factory<ClientVersion, Double> = factory(::ClientVersion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.lengthExact
import org.timemates.sdk.common.constructor.rules.notBlank
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class ConfirmationCode private constructor(public val string: String) {
public companion object : Factory<ConfirmationCode, String> by factory(
rules = listOf(
ValidationRule.notBlank(),
ValidationRule.lengthExact(8),
),
constructor = ::ConfirmationCode,
)
public companion object {
@JvmStatic
public val REQUIRED_LENGTH: Int = 8

public val factory: Factory<ConfirmationCode, String> = factory(
rules = listOf(
ValidationRule.notBlank(),
ValidationRule.lengthExact(REQUIRED_LENGTH),
),
constructor = ::ConfirmationCode,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package org.timemates.sdk.authorization.types.value
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class HashValue private constructor(public val string: String) {
public companion object : Factory<HashValue, String> by factory(
constructor = ::HashValue,
)
public companion object {
@JvmStatic
public val factory: Factory<HashValue, String> = factory(constructor = ::HashValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import org.timemates.sdk.common.constructor.results.getUnsafe
* @param TRaw The type of TRaw used to create the object.
*/
public interface Factory<TBoxed, TRaw> {

/**
* Validation rules that apply to the creation of [TBoxed] by contract of this factory.
*
* @see ValidationRule
*/
public val rules: List<ValidationRule<TRaw>>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.timemates.sdk.common.pagination
import org.timemates.sdk.common.constructor.Factory
import org.timemates.sdk.common.constructor.factory
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

/**
* A value class representing a page token.
Expand All @@ -13,5 +14,8 @@ import kotlin.jvm.JvmInline
*/
@JvmInline
public value class PageToken private constructor(public val string: String) {
public companion object : Factory<PageToken, String> by factory(::PageToken)
public companion object {
@JvmStatic
public val factory: Factory<PageToken, String> = factory(::PageToken)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public fun <T, R> PagesIterator<T>.map(mapper: suspend (T) -> R): PagesIterator<
* on every retry.
*/
internal class PagesIteratorImpl<T : TimeMatesEntity>(
private val chunkSize: Count = Count.createOrThrow(20),
private val chunkSize: Count = Count.factory.createOrThrow(20),
private val initialPageToken: PageToken?,
prevPage: Page<T>? = null,
private val provider: suspend (size: Count, pageToken: PageToken?) -> Result<Page<T>>,
private val delayOnServerErrors: Duration,
private val maxRetries: Count = Count.createOrThrow(5),
private val maxRetries: Count = Count.factory.createOrThrow(5),
private val increaseDelayOnRetries: Boolean = true,
) : PagesIterator<T> {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.minValue
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class Count private constructor(public val int: Int) {
public companion object : Factory<Count, Int> by factory(
rules = listOf(ValidationRule.minValue(Count.MIN_REQUIRED_VALUE)),
constructor = ::Count,
)
}
public companion object {
public const val MIN_VALUE: Int = 0

public val Count.Companion.MIN_REQUIRED_VALUE: Int get() = 0
@JvmStatic
public val factory: Factory<Count, Int> = factory(
rules = listOf(ValidationRule.minValue(MIN_VALUE)),
constructor = ::Count,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class TimersApi(

public fun TimersApi.getUserTimersPages(
pageToken: PageToken? = null,
maxRetries: Count = Count.createOrThrow(5),
maxRetries: Count = Count.factory.createOrThrow(5),
initialDelayOnRetries: Duration = 1.seconds,
): PagesIterator<Timer> = PagesIteratorImpl(
initialPageToken = pageToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class TimerMembersApi(
public fun TimerMembersApi.getMembersPages(
timerId: TimerId,
pageToken: PageToken? = null,
maxRetries: Count = Count.createOrThrow(5),
maxRetries: Count = Count.factory.createOrThrow(5),
initialDelayOnRetries: Duration = 1.seconds,
): PagesIterator<User> = PagesIteratorImpl(
initialPageToken = pageToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class TimerInvitesApi(
public fun TimerInvitesApi.getInvitesPages(
timerId: TimerId,
pageToken: PageToken? = null,
maxRetries: Count = Count.createOrThrow(5),
maxRetries: Count = Count.factory.createOrThrow(5),
initialDelayOnRetries: Duration = 1.seconds,
): PagesIterator<Invite> = PagesIteratorImpl(
initialPageToken = pageToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.lengthExact
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class InviteCode private constructor(public val string: String) {
public companion object : Factory<InviteCode, String> by factory(
rules = listOf(ValidationRule.lengthExact(InviteCode.REQUIRED_LENGTH)),
constructor = ::InviteCode,
)
}
public companion object {
public const val REQUIRED_LENGTH: Int = 8

public val InviteCode.Companion.REQUIRED_LENGTH: Int get() = 8
@JvmStatic
public val factory: Factory<InviteCode, String> = factory(
rules = listOf(ValidationRule.lengthExact(InviteCode.REQUIRED_LENGTH)),
constructor = ::InviteCode,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.notBlank
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class InviteName private constructor(public val string: String) {
public companion object : Factory<InviteName, String> by factory(
rules = listOf(
ValidationRule.notBlank(),
),
constructor = ::InviteName
)
public companion object {
@JvmStatic
public val factory: Factory<InviteName, String> = factory(
rules = listOf(
ValidationRule.notBlank(),
),
constructor = ::InviteName
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public data class TimerSettings(
val restTime: Duration = 5.minutes,
val bigRestTime: Duration = 10.minutes,
val bigRestEnabled: Boolean = true,
val bigRestPer: Count = Count.createOrThrow(4),
val bigRestPer: Count = Count.factory.createOrThrow(4),
val isEveryoneCanPause: Boolean = false,
val isConfirmationRequired: Boolean = false,
) : TimeMatesEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.lengthRange
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class TimerDescription private constructor(public val string: String) {
public companion object : Factory<TimerDescription, String> by factory(
rules = listOf(
ValidationRule.lengthRange(TimerDescription.LENGTH_RANGE),
),
constructor = ::TimerDescription,
)
}
public companion object {
@JvmStatic
public val LENGTH_RANGE: IntRange = 0..500

public val TimerDescription.Companion.LENGTH_RANGE: IntRange get() = 0..500
@JvmStatic
public val factory: Factory<TimerDescription, String> = factory(
rules = listOf(
ValidationRule.lengthRange(LENGTH_RANGE),
),
constructor = ::TimerDescription,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import org.timemates.sdk.common.constructor.factory
import org.timemates.sdk.common.constructor.rules.ValidationRule
import org.timemates.sdk.common.constructor.rules.minValue
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic

@JvmInline
public value class TimerId private constructor(public val long: Long) {
public companion object : Factory<TimerId, Long> by factory(
rules = listOf(
ValidationRule.minValue(0),
),
constructor = ::TimerId,
)
public companion object {
public const val MIN_VALUE: Long = 0L

@JvmStatic
public val factory: Factory<TimerId, Long> = factory(
rules = listOf(
ValidationRule.minValue(MIN_VALUE),
),
constructor = ::TimerId,
)
}
}
Loading

0 comments on commit 9c428c4

Please sign in to comment.