Skip to content

Commit

Permalink
feat: add presetRef to observation (#207)
Browse files Browse the repository at this point in the history
* add `presetRef` to observation

* make `presetRef` optional, add test with minimal observation without `presetRef`

* remove unnecessary null chec

Co-authored-by: Gregor MacLennan <[email protected]>

---------

Co-authored-by: Tomás Ciccola <[email protected]>
Co-authored-by: Gregor MacLennan <[email protected]>
  • Loading branch information
3 people authored Aug 5, 2024
1 parent 46353a9 commit 87785d0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
9 changes: 9 additions & 0 deletions proto/observation/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "google/protobuf/struct.proto";
import "tags/v1.proto";
import "common/v1.proto";
import "options.proto";
import "versionId/v1.proto";

message Observation_1 {
// **DO NOT CHANGE dataTypeId** generated with `openssl rand -hex 6`
Expand Down Expand Up @@ -66,4 +67,12 @@ message Observation_1 {
optional PositionProvider positionProvider = 5;
}
optional Metadata metadata = 9;

optional PresetRef presetRef = 10;

message PresetRef {
bytes docId = 1;
VersionId_1 versionId = 2;
}

}
15 changes: 15 additions & 0 deletions schema/observation/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@
}
},
"additionalProperties": false
},
"presetRef": {
"type": "object",
"description": "References to the preset that this observation is related to.",
"properties": {
"docId": {
"description": "hex-encoded id of the element that this observation references",
"type": "string"
},
"versionId": {
"description": "core discovery id (hex-encoded 32-byte buffer) and core index number, separated by '/'",
"type": "string"
}
},
"required": ["docId", "versionId"]
}
},
"required": ["schemaName", "tags", "attachments", "metadata"],
Expand Down
12 changes: 12 additions & 0 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,25 @@ export const convertObservation: ConvertFunction<'observation'> = (
) => {
const { common, schemaVersion, ...rest } = message
const jsonSchemaCommon = convertCommon(common, versionObj)
let presetRef

if (rest.presetRef) {
if (!rest.presetRef.versionId)
throw new Error('found presetRef on observation but is missing versionId')

presetRef = {
docId: rest.presetRef.docId.toString('hex'),
versionId: getVersionId(rest.presetRef.versionId),
}
}

const obs: Observation = {
...jsonSchemaCommon,
...rest,
attachments: message.attachments.map(convertAttachment),
tags: convertTags(message.tags),
metadata: message.metadata || {},
presetRef,
}
return obs
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,21 @@ export const convertObservation: ConvertFunction<'observation'> = (
const metadata: Observation_1_Metadata = mapeoDoc.metadata && {
...Observation_1_Metadata.fromPartial(mapeoDoc.metadata),
}
let presetRef
if (mapeoDoc.presetRef) {
presetRef = {
docId: Buffer.from(mapeoDoc.presetRef.docId, 'hex'),
versionId: parseVersionId(mapeoDoc.presetRef.versionId),
}
}

return {
common: convertCommon(mapeoDoc),
...mapeoDoc,
attachments,
tags: convertTags(mapeoDoc.tags),
metadata,
presetRef,
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/good-docs-completed.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export const goodDocsCompleted = [
networkAvailable: false,
},
},
presetRef: {
docId: cachedValues.refs.docId,
versionId: cachedValues.refs.versionId,
},
deleted: false,
},
expected: {},
Expand Down Expand Up @@ -149,6 +153,10 @@ export const goodDocsCompleted = [
versionId: cachedValues.refs.versionId,
},
],
iconRef: {
docId: cachedValues.refs.docId,
versionId: cachedValues.refs.versionId,
},
color: '#ff00ff',
terms: ['imastring'],
deleted: false,
Expand Down

0 comments on commit 87785d0

Please sign in to comment.