Skip to content

Commit

Permalink
Merge pull request #56 from samtkit/codepoints-length
Browse files Browse the repository at this point in the history
fix(codegen): use unicode codepoints as length
  • Loading branch information
mjossdev authored Jun 15, 2023
2 parents 328b08e + f174ad6 commit 218212d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ private fun validateLiteralConstraintsSuffix(typeReference: TypeReference): Stri
}
}
typeReference.sizeConstraint?.let { constraint ->
val property = if (typeReference.type is StringType) "length" else "size"
val accessor = if (typeReference.type is StringType) "codePointCount(0, it.length)" else "size"
constraint.lowerBound?.let {
add("it.${property} >= ${constraint.lowerBound}")
add("it.${accessor} >= ${constraint.lowerBound}")
}
constraint.upperBound?.let {
add("it.${property} <= ${constraint.upperBound}")
add("it.${accessor} <= ${constraint.upperBound}")
}
}
typeReference.patternConstraint?.let { constraint ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class GreeterEndpointImpl(private val baseUrl: String) : tools.samt.client.gener
appendPathSegments("all", encodeSlash = true)

// Encode query parameters
this.parameters.append("names", (JsonArray(names.map { it?.let { it -> JsonPrimitive(it.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull })).toString())
this.parameters.append("names", (JsonArray(names.map { it?.let { it -> JsonPrimitive(it.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull })).toString())
}
contentType(ContentType.Application.Json)
this.method = HttpMethod.Get
Expand Down Expand Up @@ -101,7 +101,7 @@ class GreeterEndpointImpl(private val baseUrl: String) : tools.samt.client.gener
val bodyAsText = `client response`.bodyAsText()
val jsonElement = Json.parseToJsonElement(bodyAsText)

jsonElement.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 100) }
jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 100) }
}

override fun allTheTypes(long: Long, float: Float, double: Double, decimal: java.math.BigDecimal, boolean: Boolean, date: java.time.LocalDate, dateTime: java.time.LocalDateTime, duration: java.time.Duration): Unit = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun `encode Greeting`(record: tools.samt.client.generated.greeter.Greeting): Jso
// Encode field message
val `field message` = run {
val value = record.message
JsonPrimitive(value.also { require(it.length <= 128) })
JsonPrimitive(value.also { require(it.codePointCount(0, it.length) <= 128) })
}
// Create JSON for tools.samt.greeter.Greeting
return buildJsonObject {
Expand All @@ -27,7 +27,7 @@ fun `decode Greeting`(json: JsonElement): tools.samt.client.generated.greeter.Gr
// Decode field message
val `field message` = run {
val jsonElement = json.jsonObject["message"]!!
jsonElement.jsonPrimitive.content.also { require(it.length <= 128) }
jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) <= 128) }
}
// Create record tools.samt.greeter.Greeting
return tools.samt.client.generated.greeter.Greeting(
Expand Down Expand Up @@ -103,10 +103,10 @@ fun `decode GreetingType`(json: JsonElement): tools.samt.client.generated.greete

/** Encode alias tools.samt.greeter.ID to JSON */
fun `encode ID`(value: tools.samt.client.generated.greeter.ID): JsonElement =
value?.let { value -> JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull
value?.let { value -> JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull
/** Decode alias tools.samt.greeter.ID from JSON */
fun `decode ID`(json: JsonElement): String? {
if (json is JsonNull) return null
return json.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) }
return json.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }
}

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fun Routing.routeGreeterEndpoint(
val `parameter name` = run {
// Read from path
val jsonElement = call.parameters["name"]!!.toJson()
jsonElement.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) }
jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }
}

// Decode parameter type
Expand Down Expand Up @@ -75,7 +75,7 @@ fun Routing.routeGreeterEndpoint(
val `parameter names` = run {
// Read from queryParameter
val jsonElement = call.request.queryParameters["names"]!!.toJson()
jsonElement.jsonArray.map { it.takeUnless { it is JsonNull }?.let { it.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) } } }
jsonElement.jsonArray.map { it.takeUnless { it is JsonNull }?.let { it.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) } } }
}

// Call user provided implementation
Expand Down Expand Up @@ -105,7 +105,7 @@ fun Routing.routeGreeterEndpoint(
val value = greeter.greeting(`parameter who`)

// Encode response
val response = JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 100) })
val response = JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 100) })

// Return response with 200 OK
call.respond(HttpStatusCode.OK, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fun `encode Greeting`(record: tools.samt.server.generated.greeter.Greeting): Jso
// Encode field message
val `field message` = run {
val value = record.message
JsonPrimitive(value.also { require(it.length <= 128) })
JsonPrimitive(value.also { require(it.codePointCount(0, it.length) <= 128) })
}
// Create JSON for tools.samt.greeter.Greeting
return buildJsonObject {
Expand All @@ -27,7 +27,7 @@ fun `decode Greeting`(json: JsonElement): tools.samt.server.generated.greeter.Gr
// Decode field message
val `field message` = run {
val jsonElement = json.jsonObject["message"]!!
jsonElement.jsonPrimitive.content.also { require(it.length <= 128) }
jsonElement.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) <= 128) }
}
// Create record tools.samt.greeter.Greeting
return tools.samt.server.generated.greeter.Greeting(
Expand Down Expand Up @@ -103,10 +103,10 @@ fun `decode GreetingType`(json: JsonElement): tools.samt.server.generated.greete

/** Encode alias tools.samt.greeter.ID to JSON */
fun `encode ID`(value: tools.samt.server.generated.greeter.ID): JsonElement =
value?.let { value -> JsonPrimitive(value.also { require(it.length >= 1 && it.length <= 50) }) } ?: JsonNull
value?.let { value -> JsonPrimitive(value.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }) } ?: JsonNull
/** Decode alias tools.samt.greeter.ID from JSON */
fun `decode ID`(json: JsonElement): String? {
if (json is JsonNull) return null
return json.jsonPrimitive.content.also { require(it.length >= 1 && it.length <= 50) }
return json.jsonPrimitive.content.also { require(it.codePointCount(0, it.length) >= 1 && it.codePointCount(0, it.length) <= 50) }
}

0 comments on commit 218212d

Please sign in to comment.