From 3faabd0655fa868548aed758b9ed7e4d23eb48cf Mon Sep 17 00:00:00 2001 From: tomasciccola <117094913+tomasciccola@users.noreply.github.com> Date: Thu, 1 Aug 2024 12:29:56 -0300 Subject: [PATCH] add iconRef to preset schema, fix type errors (#203) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomás Ciccola --- schema/preset/v1.json | 15 +++++++++++++++ src/lib/decode-conversions.ts | 11 +++++++++++ src/lib/encode-conversions.ts | 10 ++++++++++ 3 files changed, 36 insertions(+) diff --git a/schema/preset/v1.json b/schema/preset/v1.json index efcbb97..29c7e06 100644 --- a/schema/preset/v1.json +++ b/schema/preset/v1.json @@ -94,6 +94,21 @@ "required": ["docId", "versionId"] } }, + "iconRef": { + "type": "object", + "description": "References to the icon that this preset 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"] + }, "terms": { "description": "Synonyms or related terms (used for search)", "type": "array", diff --git a/src/lib/decode-conversions.ts b/src/lib/decode-conversions.ts index ad5296b..eed2685 100644 --- a/src/lib/decode-conversions.ts +++ b/src/lib/decode-conversions.ts @@ -137,6 +137,16 @@ export const convertPreset: ConvertFunction<'preset'> = ( geomType !== 'UNRECOGNIZED' ) + let iconRef + if (rest.iconRef) { + // iconRef is not required property on the schema, but if it does exist, then a versionId must be present + if (!rest.iconRef.versionId) + throw new Error('found iconRef on preset but is missing versionId') + iconRef = { + docId: rest.iconRef.docId.toString('hex'), + versionId: getVersionId(rest.iconRef.versionId), + } + } return { ...jsonSchemaCommon, ...rest, @@ -151,6 +161,7 @@ export const convertPreset: ConvertFunction<'preset'> = ( versionId: getVersionId(versionId), } }), + iconRef, } } diff --git a/src/lib/encode-conversions.ts b/src/lib/encode-conversions.ts index de4d413..ebf39c7 100644 --- a/src/lib/encode-conversions.ts +++ b/src/lib/encode-conversions.ts @@ -75,6 +75,15 @@ export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => { if (!mapeoDoc.color.match(colorRegex)) { throw new Error(`invalid color string ${mapeoDoc.color}`) } + + let iconRef + if (mapeoDoc.iconRef) { + iconRef = { + docId: Buffer.from(mapeoDoc.iconRef.docId, 'hex'), + versionId: parseVersionId(mapeoDoc.iconRef.versionId), + } + } + return { common: convertCommon(mapeoDoc), ...mapeoDoc, @@ -85,6 +94,7 @@ export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => { docId: Buffer.from(docId, 'hex'), versionId: parseVersionId(versionId), })), + iconRef, } }