-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: type-safe encode function [3/6] (#74)
* WIP: type-safe encode function * WIP: encode function skeleton * feat: start working on lib/encode-conversions.ts, on observation * Add tag conversion for decode Co-authored-by: tomasciccola <tomasciccola@users.noreply.github.com> * feat: convertField and convertProject on encode-converstions.ts I realize Project has a missing acording to the revition we did called `defaultPresets`. I'll try to add it and see how it goes... * feat: add (commented for now) convertPreset to encode-convertions * fix up encode convert functions * Add preset encode/decode --------- Co-authored-by: Tomás Ciccola <tciccola@digital-democracy.com> Co-authored-by: tomasciccola <tomasciccola@users.noreply.github.com>
- Loading branch information
1 parent
096a669
commit 11291ce
Showing
15 changed files
with
477 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @ts-check | ||
/** | ||
* @param {ReturnType<import('./parse-config.js').parseConfig>} config | ||
*/ | ||
export function generateEncodeDecode({ currentSchemaVersions, protoTypeDefs }) { | ||
const typeImports = protoTypeDefs.map( | ||
({ schemaName, schemaVersion, typeName }) => { | ||
return `import { ${typeName} } from './${schemaName}/v${schemaVersion}'` | ||
} | ||
) | ||
|
||
const currentProtoTypeDefs = protoTypeDefs.filter( | ||
({ schemaName, schemaVersion }) => { | ||
return currentSchemaVersions[schemaName] === schemaVersion | ||
} | ||
) | ||
|
||
const encodeLines = ['export const Encode = {'] | ||
for (const { schemaName, typeName } of currentProtoTypeDefs) { | ||
encodeLines.push(` ${schemaName}: ${typeName}.encode,`) | ||
} | ||
encodeLines.push('}') | ||
|
||
const decodeLines = ['export const Decode = {'] | ||
for (const { typeName } of protoTypeDefs) { | ||
decodeLines.push(` ${typeName}: ${typeName}.decode,`) | ||
} | ||
decodeLines.push('}') | ||
|
||
return [...typeImports, '', ...encodeLines, '', ...decodeLines, ''].join('\n') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** Length in bytes of data type ID encoding */ | ||
export const DATA_TYPE_ID_BYTES = 6 | ||
/** Length in bytes of schema version encoding */ | ||
export const SCHEMA_VERSION_BYTES = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.