Skip to content

Commit

Permalink
feat: Add updated Role data type (#127)
Browse files Browse the repository at this point in the history
* feat: Add updated `Role` data type

Fixes #124
For details see digidem/comapeo-core#189

* Update & add tests; fix things

* clarify description of `fromIndex`

Still not particularly clear, but hopefully a bit more so
  • Loading branch information
gmaclennan authored Aug 23, 2023
1 parent 7749deb commit a6d5ecc
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 56 deletions.
14 changes: 2 additions & 12 deletions proto/role/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,6 @@ message Role_1 {

Common_1 common = 1;

string role = 5;
bytes projectId = 6;
enum Action {
role_set = 0;
role_unset = 1;
};
Action action = 7;
string signature = 8;
int32 authorIndex = 9;
int32 deviceIndex = 10;
bytes authorId = 11;
string capabilityType = 12;
bytes roleId = 5;
int32 fromIndex = 6;
}
29 changes: 6 additions & 23 deletions schema/role/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,15 @@
"type": "string",
"const": "role"
},
"role": {
"type": "string"
},
"projectId": {
"type": "string"
},
"action": {
"roleId": {
"type": "string",
"enum": ["role_set", "role_unset", "UNRECOGNIZED"]
},
"signature": {
"type": "string"
},
"authorIndex": {
"type": "integer"
},
"deviceIndex": {
"type": "integer"
},
"authorId": {
"type": "string"
"description": "Unique identifier for role assigned to device with auth core ID equal to `docId` of this record"
},
"capabilityType": {
"type": "string"
"fromIndex": {
"type": "number",
"description": "This is the index of the auth core that this role applies to (identified by the `docId`) to apply this role from. E.g. documents in the auth core assigned this role from this index will be evaluated according to this role."
}
},
"required": ["schemaName", "role", "projectId", "action", "signature", "authorIndex", "deviceIndex", "authorId", "capabilityType"],
"required": ["schemaName", "roleId", "fromIndex"],
"additionalProperties": false
}
7 changes: 4 additions & 3 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ export const convertPreset: ConvertFunction<'preset'> = (
}

export const convertRole: ConvertFunction<'role'> = (message, versionObj) => {
if (message.roleId.length === 0) {
throw new Error('Invalid roleId')
}
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
role: rest.role,
projectId: message.projectId.toString('hex'),
authorId: message.authorId.toString('hex'),
roleId: message.roleId.toString('hex'),
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/lib/encode-converstions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,14 @@ export const convertObservation: ConvertFunction<'observation'> = (
}

export const convertRole: ConvertFunction<'role'> = (mapeoDoc) => {
const roleId = Buffer.from(mapeoDoc.roleId, 'hex')
if (roleId.length === 0) {
throw new Error('Invalid roleId')
}
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
projectId: Buffer.from(mapeoDoc.projectId, 'hex'),
authorId: Buffer.from(mapeoDoc.authorId, 'hex'),
roleId: Buffer.from(mapeoDoc.roleId, 'hex'),
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/bad-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,18 @@ export const badDocs = [
metadata: {},
},
},
{
text: 'role doc with empty roleId',
/** @type {import('../../dist/index.js').Role} */
doc: {
docId: cachedValues.docId,
versionId: cachedValues.versionId,
schemaName: 'role',
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
roleId: '',
fromIndex: 4,
},
},
]
10 changes: 2 additions & 8 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,8 @@ export const goodDocsCompleted = [
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
role: 'author',
projectId: cachedValues.projectId,
action: 'role_set',
signature: 'signature',
authorIndex: 8,
deviceIndex: 7,
authorId: cachedValues.authorId,
capabilityType: 'capability',
roleId: '6fd029a78243',
fromIndex: 5,
},
expected: {},
},
Expand Down
10 changes: 2 additions & 8 deletions test/fixtures/good-docs-minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,8 @@ export const goodDocsMinimal = [
createdAt: cachedValues.createdAt,
updatedAt: cachedValues.updatedAt,
links: [],
role: 'author',
projectId: cachedValues.projectId,
signature: 'signature',
authorId: cachedValues.authorId,
capabilityType: 'capability',
authorIndex: 0,
deviceIndex: 0,
action: 'UNRECOGNIZED',
roleId: '3b0104e370f9',
fromIndex: 5,
},
expected: {},
},
Expand Down

0 comments on commit a6d5ecc

Please sign in to comment.