From b725e9b7e4682d7edea29d7f5ab4e9b82043f776 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 26 Jan 2021 14:05:46 +0100 Subject: feat(users/Profpatsch/netencode): add dwim function to nix gen 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 Tested-by: BuildkiteCI --- users/Profpatsch/netencode/netencode.nix | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'users/Profpatsch') 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 ; } -- cgit 1.4.1