-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// A simple wrapper for set usage in the KV in the constitution | ||
class KVSet { | ||
#map; | ||
|
||
constructor(map) { | ||
this.#map = map; | ||
} | ||
|
||
has(key) { | ||
return this.#map.has(key); | ||
} | ||
|
||
add(key) { | ||
this.#map.set(key, new ArrayBuffer(8)); | ||
} | ||
|
||
delete(key) { | ||
this.#map.delete(key); | ||
} | ||
|
||
clear() { | ||
this.#map.clear(); | ||
} | ||
|
||
asSetOfStrings() { | ||
let set = new Set(); | ||
this.#map.forEach((_, key) => set.add(ccf.bufToStr(key))); | ||
return set; | ||
} | ||
} | ||
|
||
actions.set( | ||
"set_role_definition", | ||
new Action( | ||
function (args) { | ||
checkType(args.role, "string", "role"); | ||
checkType(args.actions, "array", "actions"); | ||
for (const [i, action] of args.actions.entries()) { | ||
checkType(action, "string", `actions[${i}]`); | ||
} | ||
}, | ||
function (args) { | ||
let roleDefinition = new KVSet( | ||
ccf.kv[`public:ccf.gov.roles.${args.role}`], | ||
); | ||
let oldValues = roleDefinition.asSetOfStrings(); | ||
let newValues = new Set(args.actions); | ||
for (const action of oldValues) { | ||
if (!newValues.has(action)) { | ||
roleDefinition.delete(ccf.strToBuf(action)); | ||
} | ||
} | ||
for (const action of newValues) { | ||
if (!oldValues.has(action)) { | ||
roleDefinition.add(ccf.strToBuf(action)); | ||
} | ||
} | ||
}, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters