1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
let mocklib = global.mocklib;
let byte = (str, off) => {
let v = ord(str, off);
return length(v) ? v[0] : v;
};
let hash = (s) => {
let h = 7;
for (let i = 0; i < length(s); i++)
h = h * 31 + byte(s, i);
return h;
};
let id = (config, t, n) => {
while (true) {
let id = sprintf('cfg%08x', hash(t + n));
if (!exists(config, id))
return id;
n++;
}
};
let fixup_config = (config) => {
let rv = {};
let n_section = 0;
for (let stype in config) {
switch (type(config[stype])) {
case 'object':
config[stype] = [ config[stype] ];
/* fall through */
case 'array':
for (let idx, sobj in config[stype]) {
let sid, anon;
if (exists(sobj, '.name') && !exists(rv, sobj['.name'])) {
sid = sobj['.name'];
anon = false;
}
else {
sid = id(rv, stype, idx);
anon = true;
}
rv[sid] = {
'.index': n_section++,
...sobj,
'.name': sid,
'.type': stype,
'.anonymous': anon
};
}
break;
}
}
for (let n, sid in sort(keys(rv), (a, b) => rv[a]['.index'] - rv[b]['.index']))
rv[sid]['.index'] = n;
return rv;
};
return {
cursor: () => ({
_configs: {},
load: function(file) {
let basename = replace(file, /^.+\//, ''),
path = sprintf("uci/%s.json", basename),
mock = mocklib.read_json_file(path);
if (!mock || mock != mock) {
mocklib.I("No configuration fixture defined for uci package %s.", file);
mocklib.I("Provide a mock configuration through the following JSON file:\n%s\n", path);
return null;
}
this._configs[basename] = fixup_config(mock);
},
_get_section: function(config, section) {
if (!exists(this._configs, config)) {
this.load(config);
if (!exists(this._configs, config))
return null;
}
let cfg = this._configs[config],
extended = match(section, "^@([A-Za-z0-9_-]+)\[(-?[0-9]+)\]$");
if (extended) {
let stype = extended[1],
sindex = +extended[2];
let sids = sort(
filter(keys(cfg), sid => cfg[sid]['.type'] == stype),
(a, b) => cfg[a]['.index'] - cfg[b]['.index']
);
if (sindex < 0)
sindex = sids.length + sindex;
return cfg[sids[sindex]];
}
return cfg[section];
},
get: function(config, section, option) {
let sobj = this._get_section(config, section);
if (option && index(option, ".") == 0)
return null;
else if (sobj && option)
return sobj[option];
else if (sobj)
return sobj[".type"];
},
get_all: function(config, section) {
return section ? this._get_section(config, section) : this._configs[config];
},
foreach: function(config, stype, cb) {
let rv = false;
if (exists(this._configs, config)) {
let cfg = this._configs[config],
sids = sort(keys(cfg), (a, b) => cfg[a]['.index'] - cfg[b]['.index']);
for (let i, sid in sids) {
if (stype == null || cfg[sid]['.type'] == stype) {
if (cb({ ...(cfg[sid]) }) === false)
break;
rv = true;
}
}
}
return rv;
}
})
};
|