Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/v79/caisson
An MVC library for the Spark-Kotlin project
https://github.com/v79/caisson
kotlin mvc-framework reflection thymeleaf
Last synced: 6 days ago
JSON representation
An MVC library for the Spark-Kotlin project
- Host: GitHub
- URL: https://github.com/v79/caisson
- Owner: v79
- Created: 2018-03-28T21:12:23.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-07T13:53:29.000Z (over 1 year ago)
- Last Synced: 2024-12-25T05:52:40.419Z (10 days ago)
- Topics: kotlin, mvc-framework, reflection, thymeleaf
- Language: Kotlin
- Homepage:
- Size: 91.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Caisson
An MVC library for the Spark-Kotlin project# PROJECT CLOSED
**Caisson** is an attempt introduce some basic MVC functionality to the Spark-Kotlin project. It is inspired by SpringMVC, but with a much smaller and simpler scope. Step one is to introduce *model binding* and *converters* to Spark-Kotlin, using annotations and reflection to construct instances of Kotlin classes from a Spark-Kotlin request parameter map.
Users of the library will be able to bind any model class from a spark-kotlin request, mapping the request param names to the constructor parameters for the model. For instance, to bind the `Person` model from the spark-kotlin request, simply call the `bind<>()` function on the `request`, supplying the model class in the generics diamonds.
```kotlin
post("/addPerson") {
val person = request.bind()
println("person is ${person?.name}, born on ${person?.date}")
}
```This assumes the following `Person` model and a custom converter class (annotated with `@CConverter`) to parse the request parameter string. Note that only fields defined in the class's primary constructor can be bound; you are welcome to add additional fields or calculated values in the class's `init { }` block. A `data class` is an obvious choice for a model, but ordinary classes work too.
```kotlin
data class Person(val name:String, @CConverter(converterClass = SimpleDateConverter::class) val date: Date)class SimpleDateConverter : Converter {
override fun convert(from: String): Date? {
val sdf: SimpleDateFormat = SimpleDateFormat("dd/MM/yyyy")
try {
return sdf.parse(from)
} catch (e: ParseException) {
return Date()
}
}
}
```## File uploads
To implement file uploading, your model class must contain a field of type `CaissonMultipartContent` (or a List of these). **Caisson** will store each of the uploaded files' bytestreams in the `CaissonMultipartContent` object:
```kotlin
data class MyFiles(val upload: List)
```And use these classes with Spark-Kotlin's normal request object. You must specify the names of the HTML input components used.
```HTML
```
```kotlin
val myFiles = request.bind(arrayListOf("upload"))
```Alternatively, if each input component has a different name, supply a List of their names as the third parameter.
```HTML
```
```kotlin
val myFiles = request.bind(arrayListOf("drivingLicense","passport"))
```This project is in the very earliest stages, and I have a lot to learn about Kotlin, HTTP requests, Reflection and more besides. So don't even think of using it :). There is a corresponding demo project hosted on github called [https://github.com/v79/spark-caisson-integration](spark-caisson-integration) which implements all the features of Caisson and may prove useful while I'm still working on the documentation.