Skip to content

Commit

Permalink
refactor (#16): Type constrains are now public (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
y9vad9 authored Jun 11, 2023
1 parent 1211eea commit 9f4df56
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import io.timemates.sdk.common.constructor.CreationFailure
@JvmInline
public value class VerificationHash private constructor(public val string: String) {
public companion object : Factory<VerificationHash, String>() {
private const val SIZE: Int = 128
public const val SIZE: Int = 128

override fun create(input: String): Result<VerificationHash> {
return when (input.length) {
SIZE -> Result.success(VerificationHash(input))
else -> Result.failure(CreationFailure.ofSize(SIZE))
else -> Result.failure(CreationFailure.ofSizeExact(SIZE))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import io.timemates.sdk.common.constructor.Factory
@JvmInline
public value class ConfirmationCode private constructor(public val string: String) {
public companion object : Factory<ConfirmationCode, String>() {
private const val SIZE = 6
private val PATTERN = Regex("^[0-9]+$")
public const val SIZE: Int = 6
public val PATTERN: Regex = Regex("^[0-9]+$")

override fun create(input: String): Result<ConfirmationCode> {
return when {
input.isBlank() -> Result.failure(CreationFailure.ofBlank())
input.length != SIZE -> Result.failure(CreationFailure.ofSize(SIZE))
input.length != SIZE -> Result.failure(CreationFailure.ofSizeExact(SIZE))
!input.matches(PATTERN) -> Result.failure(CreationFailure.ofPattern(PATTERN))
else -> Result.success(ConfirmationCode(input))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("CanBeParameter", "MemberVisibilityCanBePrivate")

package io.timemates.sdk.common.constructor

import io.timemates.sdk.common.exceptions.TimeMatesException
Expand All @@ -14,7 +16,7 @@ public sealed class CreationFailure(message: String) : TimeMatesException(messag
/**
* Represents a creation failure due to a size range constraint.
*/
public class SizeRangeFailure(public val size: IntRange) : CreationFailure("Constraint failure: size must be in range of $size")
public class SizeRangeFailure(public val range: IntRange) : CreationFailure("Constraint failure: size must be in range of $range")

/**
* Represents a creation failure due to an exact size constraint.
Expand Down Expand Up @@ -63,7 +65,7 @@ public sealed class CreationFailure(message: String) : TimeMatesException(messag
* @param size The minimal value that caused the constraint failure.
* @return A [MinValueFailure] object with the constraint failure message.
*/
public fun ofMinValue(size: Int): CreationFailure {
public fun ofMin(size: Int): CreationFailure {
return MinValueFailure(size)
}

Expand All @@ -72,7 +74,7 @@ public sealed class CreationFailure(message: String) : TimeMatesException(messag
*
* @return A [BlankValueFailure] object with the constraint failure message.
*/
public fun ofBlankValue(): CreationFailure {
public fun ofBlank(): CreationFailure {
return BlankValueFailure()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import io.timemates.sdk.common.constructor.Factory
@JvmInline
public value class Count internal constructor(public val int: Int) {
public companion object : Factory<Count, Int>() {
public const val MIN_VALUE: Int = 0

override fun create(input: Int): Result<Count> {
return when {
input < 0 -> Result.failure(CreationFailure.ofMin(0))
input < MIN_VALUE -> Result.failure(CreationFailure.ofMin(MIN_VALUE))
else -> Result.success(Count(input))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import io.timemates.sdk.common.constructor.Factory
@JvmInline
public value class InviteCode private constructor(public val string: String) {
public companion object : Factory<InviteCode, String>() {
private const val SIZE = 8
public const val SIZE: Int = 8

override fun create(input: String): Result<InviteCode> {
return when {
input.length == SIZE -> Result.success(InviteCode(input))
else -> Result.failure(CreationFailure.ofSize(SIZE))
else -> Result.failure(CreationFailure.ofSizeExact(SIZE))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public value class TimerDescription private constructor(public val string: Strin
/**
* Size range of the timer's name.
*/
private val SIZE = 3..500
public val SIZE_RANGE: IntRange = 3..500

override fun create(input: String): Result<TimerDescription> {
return when (input.length) {
!in SIZE -> Result.failure(CreationFailure.ofSize(SIZE))
!in SIZE_RANGE -> Result.failure(CreationFailure.ofSizeRange(SIZE_RANGE))
else -> Result.success(TimerDescription(input))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public value class TimerName private constructor(public val string: String) {
/**
* Size range of the timer's name.
*/
private val SIZE = 3..50
public val SIZE_RANGE: IntRange = 3..50

override fun create(input: String): Result<TimerName> {
return when (input.length) {
!in SIZE -> Result.failure(CreationFailure.ofSize(SIZE))
!in SIZE_RANGE -> Result.failure(CreationFailure.ofSizeRange(SIZE_RANGE))
else -> Result.success(TimerName(input))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import io.timemates.sdk.common.constructor.CreationFailure
@JvmInline
public value class EmailAddress private constructor(public val string: String) {
public companion object : Factory<EmailAddress, String>() {
private val SIZE: IntRange = 5..200
public val SIZE_RANGE: IntRange = 5..200

private val emailPattern = Regex(
public val PATTERN: Regex = Regex(
"[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
Expand All @@ -20,10 +20,10 @@ public value class EmailAddress private constructor(public val string: String) {

override fun create(input: String): Result<EmailAddress> {
return when {
input.length !in SIZE ->
Result.failure(CreationFailure.ofSize(SIZE))
!emailPattern.matches(input) ->
Result.failure(CreationFailure.ofPattern(emailPattern))
input.length !in SIZE_RANGE ->
Result.failure(CreationFailure.ofSizeRange(SIZE_RANGE))
!PATTERN.matches(input) ->
Result.failure(CreationFailure.ofPattern(PATTERN))

else -> Result.success(EmailAddress(input))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public value class UserDescription private constructor(public val string: String
/**
* Size range of the user's short bio.
*/
private val SIZE = 3..200
public val SIZE_RANGE: IntRange = 3..200

override fun create(input: String): Result<UserDescription> {
return when (input.length) {
!in 0..200 -> failure(CreationFailure.ofSize(SIZE))
!in 0..200 -> failure(CreationFailure.ofSizeRange(SIZE_RANGE))
else -> success(UserDescription(input))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import io.timemates.sdk.common.constructor.Factory
@JvmInline
public value class UserId private constructor(public val long: Long) {
public companion object : Factory<UserId, Long>() {
public const val MIN_VALUE: Int = 0

override fun create(input: Long): Result<UserId> {
return when {
input < 0 -> Result.failure(CreationFailure.ofMin(0))
input < MIN_VALUE -> Result.failure(CreationFailure.ofMin(MIN_VALUE))
else -> Result.success(UserId(input))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public value class UserName private constructor(public val string: String) {
/**
* Size range of the user's name.
*/
private val SIZE = 3..50
public val SIZE_RANGE: IntRange = 3..50

override fun create(input: String): Result<UserName> {
return when (input.length) {
!in SIZE -> Result.failure(CreationFailure.ofSize(SIZE))
!in SIZE_RANGE -> Result.failure(CreationFailure.ofSizeRange(SIZE_RANGE))
else -> Result.success(UserName(input))
}
}
Expand Down

0 comments on commit 9f4df56

Please sign in to comment.