Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion functions for authStore types #96

Merged
merged 8 commits into from
Aug 3, 2023
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions proto/coreOwnership/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ message CoreOwnership_1 {
Common_1 common = 1;

string action = 5;
string coreId = 6;
string projectId = 7;
bytes coreId = 6;
bytes projectId = 7;
string storeType = 8;
string signature = 9;
int32 authorIndex = 10;
Expand Down
4 changes: 2 additions & 2 deletions proto/device/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ message Device_1 {
Common_1 common = 1;

string action = 5;
string authorId = 6;
string projectId = 7;
bytes authorId = 6;
bytes projectId = 7;
string signature = 8;
int32 authorIndex = 9;
int32 deviceIndex = 10;
Expand Down
8 changes: 6 additions & 2 deletions proto/role/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ message Role_1 {
Common_1 common = 1;

string role = 5;
string projectId = 6;
string action = 7;
bytes projectId = 6;
enum Action {
role_set = 0;
role_unset = 1;
};
Action action = 7;
string signature = 8;
int32 authorIndex = 9;
int32 deviceIndex = 10;
Expand Down
5 changes: 3 additions & 2 deletions schema/coreOwnership/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"signature": { "type": "string" },
"authorIndex": { "type": "integer" },
"deviceIndex": { "type": "integer" },
"authorId": {"type": "string"}
"authorId": {"type": "string"},
"capabilityType": {"type": "string"}
},
"required": ["schemaName"]
"required": ["schemaName", "action", "coreId", "projectId", "storeType", "signature", "authorIndex", "deviceIndex", "authorId", "capabilityType"]
}
2 changes: 1 addition & 1 deletion schema/device/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"type": "string"
}
},
"required": ["schemaName"],
"required": ["schemaName", "action", "authorId", "projectId", "signature", "authorIndex", "deviceIndex", "capabilityType"],
"additionalProperties": false
}
4 changes: 2 additions & 2 deletions schema/role/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"action": {
"type": "string",
"enum": ["role:set"]
"enum": ["role_set", "role_unset", "UNRECOGNIZED"]
},
"signature": {
"type": "string"
Expand All @@ -34,6 +34,6 @@
"type": "string"
}
},
"required": ["schemaName"],
"required": ["schemaName", "role", "projectId", "action", "signature", "authorIndex", "deviceIndex", "authorId", "capabilityType"],
"additionalProperties": false
}
9 changes: 9 additions & 0 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
convertField,
convertObservation,
convertPreset,
convertRole,
convertDevice,
convertCoreOwnership
} from './lib/decode-conversions.js'
// @ts-ignore
import * as cenc from 'compact-encoding'
Expand Down Expand Up @@ -56,6 +59,12 @@ export function decode(buf: Buffer, versionObj: VersionIdObject): MapeoDoc {
return convertField(message, versionObj)
case 'preset':
return convertPreset(message, versionObj)
case 'role':
return convertRole(message,versionObj)
case 'device':
return convertDevice(message,versionObj)
case 'coreOwnership':
return convertCoreOwnership(message,versionObj)
default:
const _exhaustiveCheck: never = message
return message
Expand Down
18 changes: 18 additions & 0 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
convertObservation,
convertPreset,
convertProject,
convertRole,
convertDevice,
convertCoreOwnership,
} from './lib/encode-converstions.js'

/**
Expand Down Expand Up @@ -46,6 +49,21 @@ export function encode(mapeoDoc: OmitUnion<MapeoDoc, 'versionId'>): Buffer {
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
case 'role': {
const message = convertRole(mapeoDoc)
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
case 'device': {
const message = convertDevice(mapeoDoc)
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
case 'coreOwnership': {
const message = convertCoreOwnership(mapeoDoc)
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
default:
const _exhaustiveCheck: never = mapeoDoc
protobuf = _exhaustiveCheck
Expand Down
45 changes: 45 additions & 0 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,51 @@ export const convertPreset: ConvertFunction<'preset'> = (
}
}

export const convertRole: ConvertFunction<'role'> = (
message,
versionObj
) => {
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')
}
}

export const convertDevice: ConvertFunction<'device'> = (
message,
versionObj
) => {
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
authorId: message.authorId.toString('hex'),
projectId: message.projectId.toString('hex')
}
}

export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = (
message,
versionObj
) => {
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
return {
...jsonSchemaCommon,
...rest,
coreId: message.coreId.toString('hex'),
projectId: message.projectId.toString('hex'),
authorId: message.authorId.toString('hex')
}

}

function convertTags(tags: { [key: string]: TagValue_1 } | undefined): {
[key: string]: Exclude<JsonTagValue, undefined>
} {
Expand Down
35 changes: 35 additions & 0 deletions src/lib/encode-converstions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@ export const convertObservation: ConvertFunction<'observation'> = (
}
}

export const convertRole: ConvertFunction<'role'> = (
mapeoDoc
) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
projectId: Buffer.from(mapeoDoc.projectId,'hex'),
authorId: Buffer.from(mapeoDoc.authorId,'hex')

}
}

export const convertDevice: ConvertFunction<'device'> = (
mapeoDoc
) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
authorId: Buffer.from(mapeoDoc.authorId, 'hex'),
projectId: Buffer.from(mapeoDoc.projectId, 'hex')
}
}

export const convertCoreOwnership: ConvertFunction<'coreOwnership'> = (
mapeoDoc
) => {
return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
coreId: Buffer.from(mapeoDoc.coreId, 'hex'),
projectId: Buffer.from(mapeoDoc.projectId, 'hex'),
authorId: Buffer.from(mapeoDoc.authorId,'hex')
}
}

function convertCommon(
common: Omit<MapeoCommon, 'versionId'>
): ProtoTypesWithSchemaInfo['common'] {
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { dataTypeIds } from './config.js'

/** Temporary: once we have completed this module everything should be supported */
type SupportedSchemaNames = 'project' | 'observation' | 'field' | 'preset'
type SupportedSchemaNames = 'project' | 'observation' | 'field' | 'preset' | 'role' | 'device' | 'coreOwnership'

export type SchemaName = Extract<keyof typeof dataTypeIds, SupportedSchemaNames>
export type SchemaNameAll = keyof typeof dataTypeIds
Expand Down