Compiler-plugin that does Scala-like interpolation a.k.a. JEP-430 for Kotlin

(UPDATE I am currently waiting for approval from plugins.gradle.org to be able to publish the plugin)

I just created a Kotlin compiler-plugin that does Scala like string interpolation for Kotlin, in the Java world this is known as String Templates i.e. JEP-430. This plugin allows Kotlin to capture “the $dollar $sign $variables” in Strings before the variables are spliced into the surrounding string. This allows for third-party Kotlin libraries to create functionality like sql-prefixed strings e.g:

val ps: PreparedStatement = sql("SELECT * FROM person WHERE id = $id" AND name = $name) 

So you can capture the “id” and “name” variables and put them into a PreparedStatement correctly without worrying about SQL-injection attacks!

This can be implemented as simply as:

class SqlInterpolator(val connection: Connection): Interpolator<Any, PreparedStatement> { // Parts is ["SELECT * FROM users WHERE id = ", " AND name = ", ""] // Params is [`id`, `name`] i.e. [1234, "Joe"] override fun interpolate(parts: () -> List<String>, params: () -> List<Any>): PreparedStatement { val stmt = connection.prepareStatement(parts().joinToString("?")) for ((arg, i) <- params().zipWithIndex) { stmt.setObject(i + 1, arg) } return stmt } } 

There’s a lot more that you can do when you can capture the values of “$dollar $sign $variables” in a string. For example, Scala libraries like Doobie and Quill use these string-templates to implement a rich set of functionality by returning custom objects from
sql(“…”) strings that can then be spliced into other sql(“…”) objects.

Enjoy!

https://github.com/deusaquilus/terpal

submitted by /u/deusaquilus
[link] [comments]

Continue ReadingCompiler-plugin that does Scala-like interpolation a.k.a. JEP-430 for Kotlin

Can someone explain what’s going on here to a Kotlin noob?

Hello, your local Kotlin noob here trying to understand ktor basics. I understand the following code as a series of trailing lambdas used to configure a customer route with four http methods. I understand that the route, get, post and delete functions are imported from the routing module and also methods of the Route class. There are then a bunch of things I don’t understand.

I think customerRouting is a new function being declared on the Route class, but I don’t get how that works.

I don’t know which scope of route, get, etc. are being called.

I don’t understand how calling a series of http method function within the route trailing lambda results in those http method/paths being bound to the route itself.

I would appreciate any and all explanations or advice, even if it’s just pointing me towards resources that explain Kotlin or ktor concepts that I need to learn in order to fully understand this code.

package com.example.routes import io.ktor.server.routing.* fun Route.customerRouting() { route("/customer") { get { } get("{id?}") { } post { } delete("{id?}") { } } } 

submitted by /u/rover_G
[link] [comments]

Continue ReadingCan someone explain what’s going on here to a Kotlin noob?

Kilua 0.0.1 – composable web framework for Kotlin/Wasm and Kotlin/JS

I have published Kilua 0.0.1 – the first public release of my new compose powered web framework for Kotlin/Wasm and Kotlin/JS.

Kilua allows you to:

  • Use powerful Compose programming model and state management to develop web applications.
  • Compile the same application code for Kotlin/Wasm and Kotlin/JS targets.
  • Create fullstack applications with companion Kilua RPC library.
  • Deploy your application with full SSR (Server Side Rendering) for better SEO performance and user experience.

Kilua is similar to compose-html, but it has considerable advantages:

  • Webassembly support
  • Server Side Rendering
  • a lot of ready to use components
  • fullstack RPC with fullstack UI components
  • i18n support with gettext
  • imperative API layer to directly interact with UI components

If you want to see how to write Kilua applications, you can find fully compatible TodoMVC and RealWorld example applications, alongside other demo apps in the examples directory.

More information can be found at https://github.com/rjaros/kilua.

Any feedback is appreciated 🙂

submitted by /u/Kai_132
[link] [comments]

Continue ReadingKilua 0.0.1 – composable web framework for Kotlin/Wasm and Kotlin/JS

Query on prototyping and interactions in Android using Kotlin.

I have seen lot of demos of interactions and animations using Swift and SwiftUI,

are there any resources for Kotlin to do the same, or is Kotlin used to do similar things, how are these prototypes / micro interactions achieved in the android world?

Any devs I can follow to stay motivated around these animations, interactions and prototype kind of demos ??

submitted by /u/Flat_Dust1754
[link] [comments]

Continue ReadingQuery on prototyping and interactions in Android using Kotlin.

Kotlin/Wasm and keyboard events

Hi I’ve been trying find any documentation about this but couldn’t find anything, I tried using desktop compose approach, but no luck, I couldn’t move a single square with my keyboard arrows, if anyone out there can give me pointers on how to archive it in kotlin/wasm I appreciate it.

@Composable @Preview fun mainScreen() { var xPosition by remember { mutableStateOf(50f) } Column( modifier = Modifier .fillMaxSize() .background(color = Color.Red) .onPreviewKeyEvent { event: KeyEvent -> if (event.type == KeyEventType.KeyDown) { when (event.key) { Key.DirectionLeft -> { // Left arrow key xPosition -= 10f true } Key.DirectionRight -> { // Right arrow key xPosition += 10f true } else -> false } } else false }, verticalArrangement = Arrangement.Center ) { Box( modifier = Modifier .size(100.dp) ) { Canvas(modifier = Modifier.fillMaxSize()) { drawRect( color = Color.Blue, topLeft = androidx.compose.ui.geometry.Offset(xPosition, 0f), size = androidx.compose.ui.geometry.Size(100f, 100f) ) } } } } 

submitted by /u/olrc
[link] [comments]

Continue ReadingKotlin/Wasm and keyboard events

Building KMP Library with resources for iOS

We have a project, where we use compose multiplatform with the multiplatform resources library. The library will be distributed as both a Cocoapods dependency and Swift Package Manager dependency. This inherently means that the project our shared code will be used in doesn’t reside in the same repo. We’re experiencing differences when building through XCode (from the sample project inside the library repo) and from the command line. For example when running podPublishDebugXCFramework the build/compose/ios/<name>/compose-resources directory is missing. The generated podspec within the build folder has the resources directory specified, but since the dir is missing, this leads to MissingResource crashes

# the directory doesn't exist when running podPublishDebugXCFrameworkspec.resources = ['build/compose/ios/KMPPaymentCards/compose-resources'] 

When building through AS or XC (where the gradle syncFrameworks command is hooked as an XC build phase with some mysterious parameters) we have everything we need and the code runs without crashes, showing the expected images. Is there a way to build the library through the command line?

P.s: I know moko-resources have been used a lot, but the library hasn’t been updated recently, there are a lot of open issues on GitHub so I figured it’s safer to stick with the official lib (if possible).

submitted by /u/dephinera_bck
[link] [comments]

Continue ReadingBuilding KMP Library with resources for iOS

End of content

No more pages to load