Skip to content

Commit

Permalink
feat: device -> deviceInfo & new schema
Browse files Browse the repository at this point in the history
Fixes #126

For more info see digidem/comapeo-core#182
  • Loading branch information
gmaclennan committed Aug 18, 2023
1 parent a9cd20e commit 8ecbb2a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 60 deletions.
12 changes: 3 additions & 9 deletions proto/device/v1.proto → proto/deviceInfo/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@ import "google/protobuf/timestamp.proto";
import "common/v1.proto";
import "options.proto";

message Device_1 {
message DeviceInfo_1 {
// **DO NOT CHANGE dataTypeId** generated with `openssl rand -hex 6`
option (dataTypeId) = "e96e6c68fcc0";
option (schemaName) = "device";
option (schemaName) = "deviceInfo";

Common_1 common = 1;

string action = 5;
bytes authorId = 6;
bytes projectId = 7;
string signature = 8;
int32 authorIndex = 9;
int32 deviceIndex = 10;
string capabilityType = 11;
string name = 5;
}
35 changes: 0 additions & 35 deletions schema/device/v1.json

This file was deleted.

18 changes: 18 additions & 0 deletions schema/deviceInfo/v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://mapeo.world/schemas/device/v1.json",
"title": "Device Info",
"type": "object",
"properties": {
"schemaName": {
"type": "string",
"const": "deviceInfo"
},
"name": {
"type": "string",
"description": "Name of the device"
}
},
"required": ["schemaName", "name"],
"additionalProperties": false
}
12 changes: 6 additions & 6 deletions src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
convertObservation,
convertPreset,
convertRole,
convertDevice,
convertCoreOwnership
convertDeviceInfo,
convertCoreOwnership,
} from './lib/decode-conversions.js'
// @ts-ignore
import * as cenc from 'compact-encoding'
Expand Down Expand Up @@ -60,11 +60,11 @@ export function decode(buf: Buffer, versionObj: VersionIdObject): MapeoDoc {
case 'preset':
return convertPreset(message, versionObj)
case 'role':
return convertRole(message,versionObj)
case 'device':
return convertDevice(message,versionObj)
return convertRole(message, versionObj)
case 'deviceInfo':
return convertDeviceInfo(message, versionObj)
case 'coreOwnership':
return convertCoreOwnership(message,versionObj)
return convertCoreOwnership(message, versionObj)
default:
const _exhaustiveCheck: never = message
return message
Expand Down
6 changes: 3 additions & 3 deletions src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
convertPreset,
convertProject,
convertRole,
convertDevice,
convertDeviceInfo,
convertCoreOwnership,
} from './lib/encode-converstions.js'

Expand Down Expand Up @@ -54,8 +54,8 @@ export function encode(mapeoDoc: OmitUnion<MapeoDoc, 'versionId'>): Buffer {
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
case 'device': {
const message = convertDevice(mapeoDoc)
case 'deviceInfo': {
const message = convertDeviceInfo(mapeoDoc)
protobuf = Encode[mapeoDoc.schemaName](message).finish()
break
}
Expand Down
4 changes: 1 addition & 3 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export const convertRole: ConvertFunction<'role'> = (message, versionObj) => {
}
}

export const convertDevice: ConvertFunction<'device'> = (
export const convertDeviceInfo: ConvertFunction<'deviceInfo'> = (
message,
versionObj
) => {
Expand All @@ -144,8 +144,6 @@ export const convertDevice: ConvertFunction<'device'> = (
return {
...jsonSchemaCommon,
...rest,
authorId: message.authorId.toString('hex'),
projectId: message.projectId.toString('hex'),
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/lib/encode-converstions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,10 @@ export const convertRole: ConvertFunction<'role'> = (mapeoDoc) => {
}
}

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

Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import {
import { dataTypeIds } from './config.js'

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

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

0 comments on commit 8ecbb2a

Please sign in to comment.