Skip to content

Commit

Permalink
added more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aramikm committed Nov 21, 2024
1 parent c5f5901 commit 0c80121
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions js/schemas/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SchemaInfo {
}

const schemaFullName = (info: SchemaInfo): string => `${info.namespace}.${info.name}@v${info.version}`;
const schemaFullNameWithoutVersion = (info: SchemaInfo): string => `${info.namespace}.${info.name}`;

/**
* Mapping that will allow us to get schema full names from their ids
Expand All @@ -24,7 +25,23 @@ export const ID_TO_SCHEMA_FULL_NAME = new Map<number, string>(SCHEMA_INFOS.map((
*/
export const FULL_NAME_TO_ID = new Map<string, number>(SCHEMA_INFOS.map((x) => [schemaFullName(x), x.id]));

/**
* Mapping that will allow us to get active schema ids from their names
* example input dsnp.public-key-key-agreement
*/
export const NAME_TO_ID_ACTIVE = new Map<string, number>(
SCHEMA_INFOS.filter((info) => !info.deprecated).map((x) => [schemaFullNameWithoutVersion(x), x.id])
);

/**
* Mapping that will allow us to get schema infos from their IDs
*/
export const ID_TO_SCHEMA_INFO = new Map<number, SchemaInfo>(SCHEMA_INFOS.map((x) => [x.id, x]));

/**
* A method that can retrieve all versions of a given schema name without any version
* example input dsnp.public-key-key-agreement
*/
export const getAllVersionsFromSchemaName = (schemaName: string): SchemaInfo[] => {
return SCHEMA_INFOS.filter((info) => schemaFullNameWithoutVersion(info) === schemaName);
};
19 changes: 18 additions & 1 deletion js/schemas/test/schemas.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import assert from 'assert';
import { FULL_NAME_TO_ID, ID_TO_SCHEMA_FULL_NAME, ID_TO_SCHEMA_INFO, SchemaInfo } from '../schemas';
import {
FULL_NAME_TO_ID,
getAllVersionsFromSchemaName,
ID_TO_SCHEMA_FULL_NAME,
ID_TO_SCHEMA_INFO,
NAME_TO_ID_ACTIVE,
SchemaInfo,
} from '../schemas';

describe('schemas', function () {
it('should be able to successfully get schemas from Id', function () {
Expand Down Expand Up @@ -35,4 +42,14 @@ describe('schemas', function () {
assert.equal(info.appendOnly, expected.appendOnly);
assert.equal(info.signatureRequired, expected.signatureRequired);
});

it('should be able to successfully get all schema version from the schema name', function () {
const versions = getAllVersionsFromSchemaName('dsnp.tombstone');
assert.deepEqual(versions, [ID_TO_SCHEMA_INFO.get(1), ID_TO_SCHEMA_INFO.get(16)]);
});

it('should be able to successfully get active schema version from the schema name', function () {
const id = NAME_TO_ID_ACTIVE.get('dsnp.tombstone');
assert.equal(id, 16);
});
});

0 comments on commit 0c80121

Please sign in to comment.