Skip to content

Commit

Permalink
add iconRef to preset schema, fix type errors (#203)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomás Ciccola <tciccola@digital-democracy.com>
  • Loading branch information
tomasciccola and Tomás Ciccola committed Aug 1, 2024
1 parent 0579618 commit 3faabd0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
15 changes: 15 additions & 0 deletions schema/preset/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/lib/decode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -151,6 +161,7 @@ export const convertPreset: ConvertFunction<'preset'> = (
versionId: getVersionId(versionId),
}
}),
iconRef,
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/encode-conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -85,6 +94,7 @@ export const convertPreset: ConvertFunction<'preset'> = (mapeoDoc) => {
docId: Buffer.from(docId, 'hex'),
versionId: parseVersionId(versionId),
})),
iconRef,
}
}

Expand Down

0 comments on commit 3faabd0

Please sign in to comment.