https://github.com/lhwdev/model
🚧 (toy project) Kotlin compiler plugin & library that structuralizes any Kotlin classes
https://github.com/lhwdev/model
Last synced: 8 months ago
JSON representation
🚧 (toy project) Kotlin compiler plugin & library that structuralizes any Kotlin classes
- Host: GitHub
- URL: https://github.com/lhwdev/model
- Owner: lhwdev
- License: mit
- Created: 2021-03-04T11:06:23.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-07-03T12:54:28.000Z (almost 5 years ago)
- Last Synced: 2025-05-31T19:31:56.601Z (about 1 year ago)
- Language: Kotlin
- Homepage:
- Size: 178 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🚧 Model (toy project)
**Structuralizes any Kotlin classes**, which enables:
- Serialization
- Realtime synchronization with database
- Observing mutation
- Zero-cost reflection(only properties)
- Simple dumping
- ...and unlimited possibilities
## Examples
**All codes below are just PoC, some of them not implemented yet.**
### Serialization & Synchronization
``` kotlin
@Model
data class User(var name: String, val id: Int, val children: MutableModelList)
val serializer = JsonSerializer()
val user = serializer.hydrate(File("data.json")) // can be anything like file, socket, directory, ...
println(user.id) // lazily read(depends on implementation)
user.name = "Hello, world!" // synchornized with original file
user.children += User(name = "Jack", id = 123, children = mutableModelListOf()) // also synchronized
// note that MutableModelList is also a model class
```
### Observing mutation
``` kotlin
@Model
class MyModel {
var data = 123
}
val myModel = MyModel()
observeModels(onChange = { model -> println(model) }) {
myModel.data = 7
}
```
### Zero-cost reflection
``` kotlin
@Model
data class MyModel(val value: Long, var value2: String)
val model = MyModel(123L, "Hello!")
model.writeModel(model.modelInfo.children["value"], 4L)
```
### Dumping
``` kotlin
@Model
data class Node(val data: String, @Dump(primaryStructure = true) val children: ModelList)
val node = getNode()
println(dumpModelStructure(node))
```
output:
``` text
Node data="hello, world!"
|- Node data="Wow!"
|- Node data="simple dumping!"
|- Node data="such wow. very amaze."
|- Node data="ho"
```