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 <mail@profpatsch.de> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									a044a87084
								
							
						
					
					
						commit
						b725e9b7e4
					
				
					 1 changed files with 17 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -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
 | 
			
		||||
    ;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue