about summary refs log tree commit diff
path: root/users/Profpatsch
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2021-01-26T13·05+0100
committerProfpatsch <mail@profpatsch.de>2021-01-27T13·41+0000
commitb725e9b7e4682d7edea29d7f5ab4e9b82043f776 (patch)
tree3475f5ef461f1126ee99081f65088fca472b3abb /users/Profpatsch
parenta044a870849d03b3a71df17e589112e0c228a06e (diff)
feat(users/Profpatsch/netencode): add dwim function to nix gen r/2148
Basically what you expect, strings to text, ints to 64-bit integers,
attrs and lists to nested records and lists.

Change-Id: I9d3d841f32ab32a152cd61522f02ebde4a9b11d3
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2444
Reviewed-by: Profpatsch <mail@profpatsch.de>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/Profpatsch')
-rw-r--r--users/Profpatsch/netencode/netencode.nix18
1 files changed, 17 insertions, 1 deletions
diff --git a/users/Profpatsch/netencode/netencode.nix b/users/Profpatsch/netencode/netencode.nix
index 6bb2fa002796..5f98f27f6779 100644
--- a/users/Profpatsch/netencode/netencode.nix
+++ b/users/Profpatsch/netencode/netencode.nix
@@ -1,3 +1,4 @@
+{ lib }:
 let
 
   netstring = tag: suffix: s:
@@ -26,10 +27,24 @@ let
   concatStrings = builtins.concatStringsSep "";
 
   record = lokv: netstring "{" "}"
-    (concatStrings (map (kv: tag kv.key kv.val) lokv));
+    (concatStrings (map ({key, val}: tag key val) lokv));
 
   list = l: netstring "[" "]" (concatStrings l);
 
+  dwim = val:
+    let match = {
+      "bool" = n1;
+      "int" = i6;
+      "string" = text;
+      "set" = attrs: record (lib.mapAttrsToList
+        (k: v: {
+          key = k;
+          val = dwim v;
+        }) attrs);
+      "list" = l: list (map dwim l);
+    };
+    in match.${builtins.typeOf val} val;
+
 in {
   inherit
     unit
@@ -45,5 +60,6 @@ in {
     tag
     record
     list
+    dwim
     ;
 }