nix-helpers: 420d2570aaaf4b13fb40dc6d140d569a197128a4
1: # Add a value somewhere inside a nested set of attributes, based on a path
2: { dummyWithEnv, lib }:
3: with builtins;
4: with lib;
5: with rec {
6: # The actual function
7: go = { path, value, set }:
8: with rec {
9: name = head path;
10: new = if length path == 1 then
11: value
12: else
13: go {
14: inherit value;
15: path = (tail path);
16: set = set."${name}" or { };
17: };
18: };
19: set // {
20: "${name}" = new;
21: };
22:
23: # Unit test
24: testData = rec {
25: inputPath = [ "x" "y" "z" ];
26: inputValue = 1337;
27: inputSet = {
28: a = {
29: b = 1;
30: c = null;
31: };
32: b = [ "foo" "bar" ];
33: x = {
34: a = 1;
35: y = {
36: a = 42;
37: b = null;
38: };
39: };
40: };
41:
42: expected = {
43: a = {
44: b = 1;
45: c = null;
46: };
47: b = [ "foo" "bar" ];
48: x = {
49: a = 1;
50: y = {
51: a = 42;
52: b = null;
53: z = 1337;
54: };
55: };
56: };
57:
58: got = go {
59: path = inputPath;
60: value = inputValue;
61: set = inputSet;
62: };
63: message = "'got' should match 'expected'";
64: result = got == expected;
65: };
66:
67: test = testData.result || abort (toJSON testData);
68: };
69: assert test;
70: go
Generated by git2html.