2 let mocklib = global.mocklib;
7 for (let i = 0; i < length(s); i++)
8 h = h * 31 + ord(s, i)[0];
13 let id = (config, t, n) => {
15 let id = sprintf('cfg%08x', hash(t + n));
17 if (!exists(config, id))
24 let fixup_config = (config) => {
28 for (let stype in config) {
29 switch (type(config[stype])) {
31 config[stype] = [ config[stype] ];
35 for (let idx, sobj in config[stype]) {
38 if (exists(sobj, '.name') && !exists(rv, sobj['.name'])) {
43 sid = id(rv, stype, idx);
48 '.index': n_section++,
60 for (let n, sid in sort(keys(rv), (a, b) => rv[a]['.index'] - rv[b]['.index']))
61 rv[sid]['.index'] = n;
70 load: function(file) {
71 let basename = replace(file, /^.+\//, ''),
72 path = sprintf("uci/%s.json", basename),
73 mock = mocklib.read_json_file(path);
75 if (!mock || mock != mock) {
76 mocklib.I("No configuration fixture defined for uci package %s.", file);
77 mocklib.I("Provide a mock configuration through the following JSON file:\n%s\n", path);
82 this._configs[basename] = fixup_config(mock);
85 _get_section: function(config, section) {
86 if (!exists(this._configs, config)) {
89 if (!exists(this._configs, config))
93 let cfg = this._configs[config],
94 extended = match(section, "^@([A-Za-z0-9_-]+)\[(-?[0-9]+)\]$");
97 let stype = extended[1],
98 sindex = +extended[2];
101 filter(keys(cfg), sid => cfg[sid]['.type'] == stype),
102 (a, b) => cfg[a]['.index'] - cfg[b]['.index']
106 sindex = sids.length + sindex;
108 return cfg[sids[sindex]];
114 get: function(config, section, option) {
115 let sobj = this._get_section(config, section);
117 if (option && index(option, ".") == 0)
119 else if (sobj && option)
122 return sobj[".type"];
125 get_all: function(config, section) {
126 return section ? this._get_section(config, section) : this._configs[config];
129 foreach: function(config, stype, cb) {
132 if (exists(this._configs, config)) {
133 let cfg = this._configs[config],
134 sids = sort(keys(cfg), (a, b) => cfg[a]['.index'] - cfg[b]['.index']);
136 for (let i, sid in sids) {
137 if (stype == null || cfg[sid]['.type'] == stype) {
138 if (cb({ ...(cfg[sid]) }) === false)