Skip to content

Commit

Permalink
Working array schema transforms.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrich committed Jun 3, 2024
1 parent dbf6ebf commit 38c274c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 46 deletions.
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,13 @@ export {SchemaOutputTransformer} from './schema/output/transformer';
export {SchemaVerifyFlags} from './schema/verify/flags';
export {SchemaVerifyInit} from './schema/verify/init';
export {SchemaVerifyValue} from './schema/verify/value';
export {simpleOutputTransform} from './transform/verified';
export {Statement} from './statement';
export {Tracer} from './tracer';
export {TracerInit} from './tracer/init';
export {Transformed} from './transformed';
export {transformVerified} from './transform/verified';
export {transformVerifiedArray} from './transform/verified/array';
export {transformVerifiedField} from './transform/verified/field';
export {valueTypeLabel} from './value/type/label';
export {Verified} from './verified';
export {VerifiedArray} from './verified/array';
Expand All @@ -133,5 +136,3 @@ export {verifyBigInt} from './verify/big/int';
export {verifyBoolean} from './verify/boolean';
export {verifyStringId} from './verify/string/id';
export {verifyUrl} from './verify/url';
export {transformVerifiedArray} from './transform/verified/array';
export {transformVerifiedMap} from './transform/verified/map';
34 changes: 12 additions & 22 deletions src/transform/verified.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
import {Fate} from '@toreda/fate';
import {Log} from '@toreda/log';
import {schemaError} from '../schema/error';
import {type Verified} from '../verified';
import {type VerifiedMap} from '../verified/map';
import {type VerifiedArray} from '../verified/array';
import {Transformed} from '../transformed';
import {transformVerifiedField} from './verified/field';

/**
* Default transformer when one isn't provided to a schema. Expects a
Expand All @@ -38,8 +38,8 @@ import {type VerifiedArray} from '../verified/array';
*
* @category Schema – Transform Output
*/
export async function transformVerified<DataT, TransformedT>(
input: DataT | VerifiedMap<DataT> | VerifiedArray<DataT>,
export async function transformVerified<DataT = unknown, TransformedT = unknown>(
input: VerifiedMap<DataT>,
base: Log
): Promise<Fate<TransformedT | null>> {
const fate = new Fate<TransformedT | null>();
Expand All @@ -55,26 +55,16 @@ export async function transformVerified<DataT, TransformedT>(
}

try {
const verified: Verified = {};
const transformed: Transformed = {};

for (const [id, field] of input) {
if (Array.isArray(field)) {
verified[id] = [];
for (const item of field) {
}
const result = await transformVerifiedField(id, field, log);
if (result.ok()) {
transformed[id] = result.data;
} else {
if (field instanceof Map) {
const result = await transformVerified<DataT, TransformedT>(field, base);
if (result.ok()) {
verified[id] = result.data;
} else {
log.error(`Error in schema output transform ${id}: ${result.errorCode()}`);
verified[id] = null;
// TODO: Need tracer here for detailed path info.
}
} else {
verified[id] = field;
}
log.error(`Error in schema output transform ${id}: ${result.errorCode()}`);
fate.setErrorCode(result.errorCode());
break;
}
}

Expand All @@ -84,7 +74,7 @@ export async function transformVerified<DataT, TransformedT>(
return fate;
}

fate.data = verified as TransformedT;
fate.data = transformed as TransformedT;

fate.setSuccess(true);
} catch (e: unknown) {
Expand Down
12 changes: 0 additions & 12 deletions src/transform/verified/array.ts

This file was deleted.

70 changes: 64 additions & 6 deletions src/transform/verified/field.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,83 @@
/**
* MIT License
*
* Copyright (c) 2019 - 2024 Toreda, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

import {Fate} from '@toreda/fate';
import {type VerifiedMap} from '../../verified/map';
import {type VerifiedArray} from '../../verified/array';
import {type Transformed} from '../../transformed';
import {Log} from '@toreda/log';

export async function transformVerifiedField<DataT, TransformedT>(
/**
* Default transformer when one isn't provided to a schema. Expects a
* string -> primitive map and produces a simple object of the same mapping.
* @param id
* @param data
* @param base
*
* @category Schema – Transform Output
*/
export async function transformVerifiedField<DataT = unknown, TransformedT = unknown>(
id: string,
data: DataT | VerifiedMap<DataT> | VerifiedArray<DataT>
data: DataT | VerifiedMap<DataT> | VerifiedArray<DataT>,
base: Log
): Promise<Fate<TransformedT | TransformedT[]>> {
const fate = new Fate<any>();
const fate = new Fate<TransformedT | TransformedT[]>();
const log = base.makeLog(`${id}`);

if (Array.isArray(data)) {
fate.data = [];
const results: TransformedT[] = [];
for (const item of data) {
const result = await transformVerifiedField<DataT, TransformedT>('xxxx', item);
const result = await transformVerifiedField<DataT, TransformedT>('xxxx', item, log);
if (result.ok()) {
fate.data.push(result.data);
results.push(result.data as any);
} else {
log.error(`Error in schema output transform ${id}: ${result.errorCode()}`);
fate.setErrorCode(result.errorCode());
break;
}
}
fate.data = results;
} else if (data instanceof Map) {
const transformed = {} as Transformed;
for (const [id, field] of data) {
const result = await transformVerifiedField(id, field, log);
if (result.ok()) {
transformed[id] = result.data;
} else {
transformed[id] = null;
}
}

fate.data = transformed as TransformedT;
} else {
// HACK: Should not rely on any. Defeats the purpose of type checking.
fate.data = data as any;
}

if (!fate.errorCode()) {
fate.setSuccess(true);
}

return fate;
Expand Down
31 changes: 31 additions & 0 deletions src/transformed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* MIT License
*
* Copyright (c) 2019 - 2024 Toreda, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

/**
* @category Schema
*/
export interface Transformed {
[k: string]: unknown;
}
6 changes: 3 additions & 3 deletions tests/schema/recursive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ describe('Schema - Recursive Parsing', () => {
expect(result.data?.subSchemas).toHaveLength(2);

const resultSubA = result.data?.subSchemas![0];
expect(resultSubA).toEqual({});
const resultSubB = result.data?.subSchemas![1];

expect(resultSubB).toBeDefined();

expect(resultSubA?.int2b).toBe(intSample1);
expect(resultSubA?.str2b).toBe(strSample1);

expect(resultSubB?.int2b).toBe(intSample1);
expect(resultSubB?.str2b).toBe(strSample1);
expect(resultSubB?.int2b).toBe(intSample2);
expect(resultSubB?.str2b).toBe(strSample2);
});
});
});

0 comments on commit 38c274c

Please sign in to comment.