Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bamboo/placeholders-sketch
A small study on DSL design for map declarations.
https://github.com/bamboo/placeholders-sketch
Last synced: 18 days ago
JSON representation
A small study on DSL design for map declarations.
- Host: GitHub
- URL: https://github.com/bamboo/placeholders-sketch
- Owner: bamboo
- Created: 2020-02-03T14:42:29.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-03T14:42:46.000Z (almost 5 years ago)
- Last Synced: 2024-10-28T19:57:29.592Z (2 months ago)
- Language: Kotlin
- Size: 55.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Placeholders Sketch
A small study on DSL design for map declarations.
## Values
1. Instant execution compatibility
2. Flexible wiring of keys, values and maps
3. Familiar syntax## Proposal
1. Use a [MapProperty](https://docs.gradle.org/current/userguide/lazy_configuration.html#working_with_maps), see [Extension.placeholders](./buildSrc/src/main/kotlin/my/Extension.kt)
```kotlin
interface Extension {
val placeholders: MapProperty
}
```
2. Add Kotlin operator extensions as needed, see [kotlin-dsl/build.gradle.kts](./kotlin-dsl/build.gradle.kts)
```kotlin
/**
* Enables the `map[key] = value` syntax on [MapProperty] values.
*/
operator fun MapProperty.set(k: K, v: V) =
put(k, v)
/**
* Enables the `map("k1" to "v1", "kn" to "vn")` syntax on [MapProperty] values.
*/
operator fun MapProperty.invoke(vararg entries: Pair) =
entries.forEach { (k, v) -> [email protected](k, v) }
```
Resulting in the following Kotlin DSL configuration syntax:```kotlin
my {
placeholders["foo"] = "bar"
placeholders(
"a" to "b",
"c" to "d",
"key" to true
)
}
```And the following Groovy DSL configuration syntax:
```groovy
my {
placeholders = [a: 'b', c: 'd']
placeholders.e = 'f'
placeholders['g'] = 'h'
// Can be achieved via custom method OR Groovy decoration
// placeholders i: 'j', k: 'l'
}
```