-
Notifications
You must be signed in to change notification settings - Fork 338
-
I'm curious if anyone has written a Redis renderer, since I have configuration not unlike https://docs.konghq.com/hub/kong-inc/rate-limiting/configuration/ and currently Redis keys are maintained manually by operations and can cause operational issues.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments 3 replies
-
This is an interesting problem space! Pkl can only produce textual output, but there are a couple it may be able to help:
- Shell command generation - a simple technique here might be to have Pkl produce a shell script that runs redis-cli commands. A custom renderer of this kind is easy to build in-language. You would run it like
pkl eval <module> | bash. - Language binding library - using the Java/Kotlin library, pkl-swift, or pkl-go write a program where the Pkl module is evaluated into types in the target language. The program would use this result and a Redis client library to write the values.
Beta Was this translation helpful? Give feedback.
All reactions
-
Do you have an example of how to produce said shell script please?
Beta Was this translation helpful? Give feedback.
All reactions
-
Sure, it's fairly straightforward. Using your pkl-redis repo as the basis:
module Control hidden baseCommand: String = "redis-cli" // could be "docker exec redis redis-cli" hidden dbNum: UInt = 0 rl: Controller; cb: Controller; class Controller { toggle: Running = "OFF"; } typealias Running = "ON"|"OFF"|"SHADOW" /// Flatten an object structure down to "key:path"="value" pairs function walkKV(val: Map|Mapping|Dynamic|Typed): Mapping<String, Any> = new { for (k, v in val.toMap()) { when (v is Map|Mapping|Dynamic|Typed) { for (k2, v2 in walkKV(v)) { ["\(k):\(k2)"] = v2 } } else { // not clear how List|Listing should be handled here, pretend they don't exist [k] = v } } } output { text = new Listing { "#!/bin/bash" for (k, v in walkKV(module)) { "\(baseCommand) -n \(dbNum) SET \(k) '\(v)'" } }.join("\n") }
Then to run it
pkl eval config.pkl | bash
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
Did I get the idea of the "Language binding library" over at https://github.com/kaihendry/pkl-redis ?
Beta Was this translation helpful? Give feedback.
All reactions
-
In general, yeah! My original thought was to directly use go-redis or similar for tighter integration instead of printing out the shell commands to run.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1