Closed LVMVRQUXL closed 7 months ago
Also related to issue #602.
It may be a good idea to extract serializers to a new kotlinx-serialization
subproject while making this type serializable.
Consumers will be able to import the new subproject and use it like the following:
val format = Json { serializersModule = KotoolsTypesSerializersModule.all }
val emailAddress = format.decodeFromString<EmailAddress>("\"contact@kotools.org\"")
println(emailAddress) // contact@kotools.org
A better design for creating instances of the EmailAddress
type is to provide constructors that should throw exceptions in case of invalid arguments, and orNull
factory functions that should return null
in case of invalid arguments.
Also, arguments should be of type String
instead of Any
for avoiding confusions.
fun EmailAddress(value: String): EmailAddress
fun EmailAddress(value: String, pattern: String): EmailAddress
fun EmailAddress.Companion.orNull(value: String): EmailAddress?
fun EmailAddress.Companion.orNull(value: String, pattern: String): EmailAddress?
Here are some examples for calling these functions from Kotlin code:
EmailAddress("contact@kotools.org")
EmailAddress(" @kotools.org") // throws an exception
EmailAddress(value = "contact@kotools.org", pattern = "^[a-z]+@[a-z]+\\.[a-z]+\$")
EmailAddress(value = " @kotools.org", pattern = "^[a-z]+@[a-z]+\\.[a-z]+\$") // throws an exception
EmailAddress(value = "contact@kotools.org", pattern = "^[a-z]+\$") // throws an exception
EmailAddress.orNull("contact@kotools.org")
EmailAddress.orNull(" @kotools.org") // returns null
EmailAddress.orNull(value = "contact@kotools.org", pattern = "^[a-z]+@[a-z]+\\.[a-z]+\$")
EmailAddress.orNull(value = " @kotools.org", pattern = "^[a-z]+@[a-z]+\\.[a-z]+\$") // returns null
EmailAddress.orNull(value = "contact@kotools.org", pattern = "^[a-z]+\$") // returns null
Finally, the EmailAddress(String)
function should be the primary constructor.
It is not possible to define another constructor without directly calling the primary one, including the check of the value against the default pattern, which is not the intended behavior when calling the factory function of EmailAddress
with a custom pattern. So we will go back to the original factory functions.
📝 Description
We want to introduce an
EmailAddress
experimental type, in theorg.kotools.types
package of thetypes
Gradle subproject, with an improved API for deprecating the same type from thekotools.types.web
package.Like the type from the
kotools.types.web
package, this new one should be serializable asString
. Finally, we want to deprecate the type from thekotools.types.web
with a warning level, and its factory functions with an error level and replacement suggestions, for removal in v4.7.✅ Checklist
org.kotools.types
package and dump the Application Binary Interface (ABI).PATTERN
constant, test this constant, add Kotlin and Java code samples for this constant, and dump the ABI.fromStringOrNull(Any)
function with tests, code samples for Kotlin and Java, reference it in the type's documentation and dump the ABI.fromString(Any)
function with tests, code samples for Kotlin and Java, reference it in the type's documentation and dump the ABI.fromStringOrNull(Any, Any)
function with tests, code samples for Kotlin and Java, and dump the ABI.fromString(Any, Any)
function with tests, code samples for Kotlin and Java, and dump the ABI.toString
function with tests, code samples for Kotlin and Java, and dump the ABI.equals
andhashCode
) with tests, code samples for Kotlin and Java, and dump the ABI.String
with tests, Kotlin code sample and dump the ABI.EmailAddress
type from thekotools.types.web
package with a warning level, and its factory functions with an error level and replacement suggestions, for removal in v4.7. This is a source incompatible change.EmailAddress
type from thekotools.types.web
package in v4.7.KotoolsTypesSerializers.emailAddress
property for serializing this type asString
, with documentation, tests, samples, then dump the corresponding ABI.@Serializable
annotation from this type, its serialization tests and documentation, then dump the ABI.KotoolsTypesSerializers.emailAddress
module in theKotoolsTypesSerializers.all
one with tests.