Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import community.flock.wirespec.generated.kotlin.endpoint.DeleteTodoById
import community.flock.wirespec.generated.kotlin.endpoint.GetTodoById
import community.flock.wirespec.generated.kotlin.endpoint.GetTodos
import community.flock.wirespec.generated.kotlin.endpoint.PostTodo
import community.flock.wirespec.generated.kotlin.model.validate

private interface TodoApi :
GetTodos.Handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface JavaRefinedTypeDefinitionEmitter: RefinedTypeDefinitionEmitter, JavaTy
|public record ${emit(refined.identifier)} (${refined.reference.emit()} value) implements Wirespec.Refined<${refined.reference.emit()}> {
|${Spacer}@Override
|${Spacer}public String toString() { return value.toString(); }
|${Spacer}public static boolean validate(${emit(refined.identifier)} record) {
|${Spacer}public boolean validate() {
|${Spacer}${Spacer}${refined.emitValidator()}
|${Spacer}}
|${Spacer}@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ interface JavaTypeDefinitionEmitter: TypeDefinitionEmitter, IdentifierEmitter {
}

override fun Reference.Primitive.Type.Constraint.emit() = when(this){
is Reference.Primitive.Type.Constraint.RegExp -> """return java.util.regex.Pattern.compile("${expression.replace("\\", "\\\\")}").matcher(record.value).find();"""
is Reference.Primitive.Type.Constraint.RegExp -> """return java.util.regex.Pattern.compile("${expression.replace("\\", "\\\\")}").matcher(value).find();"""
is Reference.Primitive.Type.Constraint.Bound -> {
val minCheck = min?.let { "$it < record.value" }
val maxCheck = max?.let { "record.value < $it" }
val minCheck = min?.let { "$it < value" }
val maxCheck = max?.let { "value < $it" }
val checks = listOfNotNull(minCheck, maxCheck).joinToString(" && ")
"""return ${if (checks.isEmpty()) "true" else checks};"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ class JavaEmitterTest {
|public record UUID (String value) implements Wirespec.Refined<String> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(UUID record) {
| return java.util.regex.Pattern.compile("^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}${'$'}").matcher(record.value).find();
| public boolean validate() {
| return java.util.regex.Pattern.compile("^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}${'$'}").matcher(value).find();
| }
| @Override
| public String value() { return value; }
Expand Down Expand Up @@ -485,8 +485,8 @@ class JavaEmitterTest {
|public record TodoId (String value) implements Wirespec.Refined<String> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TodoId record) {
| return java.util.regex.Pattern.compile("^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$").matcher(record.value).find();
| public boolean validate() {
| return java.util.regex.Pattern.compile("^[0-9a-fA-F]{8}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{4}\\b-[0-9a-fA-F]{12}$").matcher(value).find();
| }
| @Override
| public String value() { return value; }
Expand All @@ -499,7 +499,7 @@ class JavaEmitterTest {
|public record TodoNoRegex (String value) implements Wirespec.Refined<String> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TodoNoRegex record) {
| public boolean validate() {
| return true;
| }
| @Override
Expand All @@ -513,7 +513,7 @@ class JavaEmitterTest {
|public record TestInt (Long value) implements Wirespec.Refined<Long> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestInt record) {
| public boolean validate() {
| return true;
| }
| @Override
Expand All @@ -527,7 +527,7 @@ class JavaEmitterTest {
|public record TestInt0 (Long value) implements Wirespec.Refined<Long> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestInt0 record) {
| public boolean validate() {
| return true;
| }
| @Override
Expand All @@ -541,8 +541,8 @@ class JavaEmitterTest {
|public record TestInt1 (Long value) implements Wirespec.Refined<Long> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestInt1 record) {
| return 0 < record.value;
| public boolean validate() {
| return 0 < value;
| }
| @Override
| public Long value() { return value; }
Expand All @@ -555,8 +555,8 @@ class JavaEmitterTest {
|public record TestInt2 (Long value) implements Wirespec.Refined<Long> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestInt2 record) {
| return 1 < record.value && record.value < 3;
| public boolean validate() {
| return 1 < value && value < 3;
| }
| @Override
| public Long value() { return value; }
Expand All @@ -569,7 +569,7 @@ class JavaEmitterTest {
|public record TestNum (Double value) implements Wirespec.Refined<Double> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestNum record) {
| public boolean validate() {
| return true;
| }
| @Override
Expand All @@ -583,7 +583,7 @@ class JavaEmitterTest {
|public record TestNum0 (Double value) implements Wirespec.Refined<Double> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestNum0 record) {
| public boolean validate() {
| return true;
| }
| @Override
Expand All @@ -597,8 +597,8 @@ class JavaEmitterTest {
|public record TestNum1 (Double value) implements Wirespec.Refined<Double> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestNum1 record) {
| return record.value < 0.5;
| public boolean validate() {
| return value < 0.5;
| }
| @Override
| public Double value() { return value; }
Expand All @@ -611,8 +611,8 @@ class JavaEmitterTest {
|public record TestNum2 (Double value) implements Wirespec.Refined<Double> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TestNum2 record) {
| return -0.2 < record.value && record.value < 0.5;
| public boolean validate() {
| return -0.2 < value && value < 0.5;
| }
| @Override
| public Double value() { return value; }
Expand All @@ -633,8 +633,8 @@ class JavaEmitterTest {
|public record TodoId (String value) implements Wirespec.Refined<String> {
| @Override
| public String toString() { return value.toString(); }
| public static boolean validate(TodoId record) {
| return java.util.regex.Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}${'$'}").matcher(record.value).find();
| public boolean validate() {
| return java.util.regex.Pattern.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}${'$'}").matcher(value).find();
| }
| @Override
| public String value() { return value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ interface KotlinRefinedTypeDefinitionEmitter: RefinedTypeDefinitionEmitter, Kotl
override fun emit(refined: Refined) = """
|data class ${refined.identifier.sanitize()}(override val value: ${refined.reference.emit()}): Wirespec.Refined<${refined.reference.emit()}> {
|${Spacer}override fun toString() = value.toString()
|${Spacer}override fun validate() = ${refined.emitValidator()}
|}
|
|fun ${refined.identifier.value}.validate() = ${refined.emitValidator()}
|
""".trimMargin()

override fun Refined.emitValidator():String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ class KotlinEmitterTest {
|
|data class UUID(override val value: String): Wirespec.Refined<String> {
| override fun toString() = value.toString()
| override fun validate() = Regex(${"\"\"\""}^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}${'$'}${"\"\"\""}).matches(value)
|}
|
|fun UUID.validate() = Regex(${"\"\"\""}^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}${'$'}${"\"\"\""}).matches(value)
|
""".trimMargin(),
)

Expand Down Expand Up @@ -481,109 +480,99 @@ class KotlinEmitterTest {
|
|data class TodoId(override val value: String): Wirespec.Refined<String> {
| override fun toString() = value.toString()
| override fun validate() = Regex(""${'"'}^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$""${'"'}).matches(value)
|}
|
|fun TodoId.validate() = Regex(""${'"'}^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$""${'"'}).matches(value)
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TodoNoRegex(override val value: String): Wirespec.Refined<String> {
| override fun toString() = value.toString()
| override fun validate() = true
|}
|
|fun TodoNoRegex.validate() = true
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestInt(override val value: Long): Wirespec.Refined<Long> {
| override fun toString() = value.toString()
| override fun validate() = true
|}
|
|fun TestInt.validate() = true
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestInt0(override val value: Long): Wirespec.Refined<Long> {
| override fun toString() = value.toString()
| override fun validate() = true
|}
|
|fun TestInt0.validate() = true
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestInt1(override val value: Long): Wirespec.Refined<Long> {
| override fun toString() = value.toString()
| override fun validate() = 0 < value
|}
|
|fun TestInt1.validate() = 0 < value
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestInt2(override val value: Long): Wirespec.Refined<Long> {
| override fun toString() = value.toString()
| override fun validate() = 1 < value && value < 3
|}
|
|fun TestInt2.validate() = 1 < value && value < 3
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestNum(override val value: Double): Wirespec.Refined<Double> {
| override fun toString() = value.toString()
| override fun validate() = true
|}
|
|fun TestNum.validate() = true
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestNum0(override val value: Double): Wirespec.Refined<Double> {
| override fun toString() = value.toString()
| override fun validate() = true
|}
|
|fun TestNum0.validate() = true
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestNum1(override val value: Double): Wirespec.Refined<Double> {
| override fun toString() = value.toString()
| override fun validate() = value < 0.5
|}
|
|fun TestNum1.validate() = value < 0.5
|
|package community.flock.wirespec.generated.model
|
|import community.flock.wirespec.kotlin.Wirespec
|import kotlin.reflect.typeOf
|
|data class TestNum2(override val value: Double): Wirespec.Refined<Double> {
| override fun toString() = value.toString()
| override fun validate() = -0.2 < value && value < 0.5
|}
|
|fun TestNum2.validate() = -0.2 < value && value < 0.5
|
""".trimMargin()

CompileRefinedTest.compiler { KotlinEmitter() } shouldBeRight kotlin
Expand Down Expand Up @@ -618,10 +607,9 @@ class KotlinEmitterTest {
|
|data class TodoId(override val value: String): Wirespec.Refined<String> {
| override fun toString() = value.toString()
| override fun validate() = Regex(""${'"'}^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}${'$'}""${'"'}).matches(value)
|}
|
|fun TodoId.validate() = Regex(""${'"'}^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}${'$'}""${'"'}).matches(value)
|
|package community.flock.wirespec.generated.endpoint
|
|import community.flock.wirespec.kotlin.Wirespec
Expand Down
Loading
Loading