Skip to content

Commit

Permalink
migrate makeMetadataKeysWithContext
Browse files Browse the repository at this point in the history
  • Loading branch information
badetitou committed Mar 20, 2024
1 parent ff46a28 commit 4d28b1b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 56 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ jobs:
uses: ATiltedTree/setup-rust@v1.0.5
with:
rust-version: stable
- uses: jetli/wasm-pack-action@v0.4.0
with:
version: 'latest'
- run: npm ci
- run: npm test
- run: mkdir coverage
Expand Down
55 changes: 30 additions & 25 deletions jackson-wasm/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,33 @@ pub fn make_metadata_key_with_context(
Ok(result)
}

// #[wasm_bindgen]
// pub fn make_metadata_keys_with_context(
// key: String,
// options: MakeMetadataKeysWithContextOptions,
// ) -> Vec<String> {
// match options.contextGroups() {
// Some(context_groups) => context_groups
// .iter()
// .map(|context_group| {
// make_metadata_key_with_context(
// &key,
// &options.prefix,
// &options.suffix,
// Some(context_group),
// )
// })
// .collect(),
// None => vec![make_metadata_key_with_context(
// &key,
// &options.prefix,
// &options.suffix,
// None,
// )],
// }
// }
#[wasm_bindgen]
pub fn make_metadata_keys_with_context(
key: String,
options: MakeMetadataKeysWithContextOptions,
) -> Vec<String> {
match options.contextGroups() {

Some(context_groups) => context_groups
.iter()
.map(|context_group| {
make_metadata_key_with_context(
&key,
MakeMetadataKeyWithContextOptions::new(
Some(context_group.to_string()),
options.prefix(),
options.suffix(),
),
).ok().unwrap()
})
.collect(),
None => vec![make_metadata_key_with_context(
&key,
MakeMetadataKeyWithContextOptions::new(
None,
options.prefix(),
options.suffix(),
),
).ok().unwrap() ],
}
}
38 changes: 22 additions & 16 deletions src/core/JsonParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import {
isSameConstructor,
isSameConstructorOrExtensionOf,
isSameConstructorOrExtensionOfNoObject,
makeMetadataKeysWithContext,
mapClassPropertyToVirtualProperty,
mapVirtualPropertiesToClassProperties,
mapVirtualPropertyToClassProperty, sortMappersByOrder
} from '../util';
import {
// eslint-disable-next-line camelcase
make_metadata_keys_with_context} from 'jackson-wasm';
import {
ClassType,
ClassTypeWithDecoratorDefinitions,
Expand Down Expand Up @@ -315,7 +317,7 @@ export class JsonParser<T> {
} else if (hasBigInt && isSameConstructorOrExtensionOfNoObject(currentMainCreator, BigInt)) {
value = BigInt(+value);
} else if (isSameConstructorOrExtensionOfNoObject(currentMainCreator, String)) {
// @ts-ignore
// @ts-expect-error - value is a number
value += '';
}
} else if (value.constructor === Boolean) {
Expand All @@ -324,7 +326,7 @@ export class JsonParser<T> {
} else if (hasBigInt && isSameConstructorOrExtensionOfNoObject(currentMainCreator, BigInt)) {
value = BigInt(value ? 1 : 0);
} else if (isSameConstructorOrExtensionOfNoObject(currentMainCreator, String)) {
// @ts-ignore
// @ts-expect-error - value is a number
value += '';
}
}
Expand Down Expand Up @@ -369,15 +371,15 @@ export class JsonParser<T> {
return this.parseMapAndObjLiteral(key, value, context, globalContext);
} else if (hasBigInt && isSameConstructorOrExtensionOfNoObject(currentMainCreator, BigInt)) {
return (value != null && value.constructor === String && value.endsWith('n')) ?
// @ts-ignore
// @ts-expect-error - value is a number
currentMainCreator(value.substring(0, value.length - 1)) :
// @ts-ignore
// @ts-expect-error - value is a number
currentMainCreator(value);
} else if (isSameConstructorOrExtensionOfNoObject(currentMainCreator, RegExp)) {
// @ts-ignore
// @ts-expect-error - value is a number
return new currentMainCreator(value);
} else if (isSameConstructorOrExtensionOfNoObject(currentMainCreator, Date)) {
// @ts-ignore
// @ts-expect-error - value is a number
return new currentMainCreator(value);
} else if (typeof value === 'object' && !isIterableNoMapNoString(value)) {

Expand Down Expand Up @@ -536,7 +538,7 @@ export class JsonParser<T> {
const jsonDecoratorOptions: JsonDecoratorOptions = this.cachedGetMetadata(metadataKey, currentMainCreator, key, context);
if (jsonDecoratorOptions) {
const metadataKeysWithContext =
makeMetadataKeysWithContext(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
make_metadata_keys_with_context(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeAppliedForDeepestClass[metadataKeyWithContext] = jsonDecoratorOptions;
}
Expand Down Expand Up @@ -566,7 +568,7 @@ export class JsonParser<T> {
this.cachedGetMetadata('JsonClassTypeParam:' + argumentIndex, currentMainCreator, methodName, context);

const metadataKeysWithContext =
makeMetadataKeysWithContext(classicMetadataKey,
make_metadata_keys_with_context(classicMetadataKey,
{contextGroups: jsonDecoratorOptions.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeAppliedForDeepestClass[metadataKeyWithContext] = jsonDecoratorOptions;
Expand All @@ -576,7 +578,7 @@ export class JsonParser<T> {
deepestClass = null;
} else {
const jsonClassMetadataKeysWithContext =
makeMetadataKeysWithContext('JsonClassType', {contextGroups: jsonClassParam.contextGroups});
make_metadata_keys_with_context('JsonClassType', {contextGroups: jsonClassParam.contextGroups});
for (const metadataKeyWithContext of jsonClassMetadataKeysWithContext) {
decoratorsToBeAppliedForDeepestClass[metadataKeyWithContext] = jsonClassParam;
}
Expand All @@ -585,7 +587,7 @@ export class JsonParser<T> {
decoratorsNameFoundForDeepestClass.push(classicMetadataKey);
} else {
const metadataKeysWithContext =
makeMetadataKeysWithContext(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
make_metadata_keys_with_context(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeAppliedForDeepestClass[metadataKeyWithContext] = jsonDecoratorOptions;
}
Expand All @@ -605,7 +607,7 @@ export class JsonParser<T> {
this.cachedGetMetadata('JsonClassTypeParam:' + argumentIndex, currentMainCreator, methodName, context);

const metadataKeysWithContext =
makeMetadataKeysWithContext('JsonDeserialize',
make_metadata_keys_with_context('JsonDeserialize',
{contextGroups: jsonDecoratorOptionsForFirstClass.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeAppliedForFirstClass[metadataKeyWithContext] = jsonDecoratorOptionsForFirstClass;
Expand All @@ -615,7 +617,7 @@ export class JsonParser<T> {
firstClass = null;
} else {
const jsonClassMetadataKeysWithContext =
makeMetadataKeysWithContext('JsonClassType', {contextGroups: jsonClassParam.contextGroups});
make_metadata_keys_with_context('JsonClassType', {contextGroups: jsonClassParam.contextGroups});
for (const metadataKeyWithContext of jsonClassMetadataKeysWithContext) {
decoratorsToBeAppliedForFirstClass[metadataKeyWithContext] = jsonClassParam;
}
Expand All @@ -624,7 +626,7 @@ export class JsonParser<T> {
decoratorsNameFoundForFirstClass.push('JsonDeserialize');
} else {
const metadataKeysWithContext =
makeMetadataKeysWithContext(metadataKeyForFirstClass, {contextGroups: jsonDecoratorOptionsForFirstClass.contextGroups});
make_metadata_keys_with_context(metadataKeyForFirstClass, {contextGroups: jsonDecoratorOptionsForFirstClass.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsNameFoundForFirstClass[metadataKeyWithContext] = jsonDecoratorOptionsForFirstClass;
}
Expand All @@ -645,6 +647,7 @@ export class JsonParser<T> {
/**
* This method implements a cache that can be used instead of calling directly the getMetadata of util.ts
*/
// eslint-disable-next-line no-shadow
private cachedGetMetadata<T extends JsonDecoratorOptions>(metadataKey: string,
target: ClassType<any>,
propertyKey: string = null,
Expand Down Expand Up @@ -808,10 +811,12 @@ export class JsonParser<T> {
} else {
const args = props.map(([, value]) => value);
instance = ('_method' in jsonCreator && jsonCreator._method) ?
// eslint-disable-next-line @typescript-eslint/ban-types
(method as Function)(...args) : new (method as ObjectConstructor)(...args);
}
} else {
instance = ('_method' in jsonCreator && jsonCreator._method) ?
// eslint-disable-next-line @typescript-eslint/ban-types
(method as Function)(obj) : new (method as ObjectConstructor)(obj);
}

Expand Down Expand Up @@ -908,6 +913,7 @@ export class JsonParser<T> {

if (jsonCreatorMode === JsonCreatorMode.PROPERTIES_OBJECT) {
instance = ('_method' in jsonCreator && jsonCreator._method) ?
// eslint-disable-next-line @typescript-eslint/ban-types
(method as Function)(instance) : new (method as ObjectConstructor)(instance);
}

Expand Down Expand Up @@ -1326,7 +1332,7 @@ export class JsonParser<T> {
const decorators = result.decorators;
for (const decorator of decorators) {
const metadataKeysWithContext =
makeMetadataKeysWithContext(decorator.name, {contextGroups: decorator.options.contextGroups});
make_metadata_keys_with_context(decorator.name, {contextGroups: decorator.options.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeApplied[metadataKeyWithContext] = {
enabled: true,
Expand Down Expand Up @@ -1837,7 +1843,7 @@ export class JsonParser<T> {
(newIterable as Array<any>).push(this.deepTransform(key, value, undefined, newContext, globalContext));
}
if (!isSameConstructor(currentCreator, Array)) {
// @ts-ignore
// @ts-expect-error BVER
newIterable = new currentCreator(...newIterable);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/core/JsonStringifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ import {
isSameConstructorOrExtensionOfNoObject,
isValueEmpty,
isVariablePrimitiveType,
makeMetadataKeysWithContext,
mapVirtualPropertiesToClassProperties,
sortMappersByOrder
} from '../util';
import {
// eslint-disable-next-line camelcase
make_metadata_keys_with_context} from 'jackson-wasm';
// import * as moment from 'moment';
// import {v1 as uuidv1, v3 as uuidv3, v4 as uuidv4, v5 as uuidv5} from 'uuid';
import {JacksonError} from './JacksonError';
Expand Down Expand Up @@ -624,7 +626,7 @@ export class JsonStringifier<T> {
const jsonDecoratorOptions: JsonDecoratorOptions = getMetadata(metadataKey, currentMainCreator, key, context);
if (jsonDecoratorOptions) {
const metadataKeysWithContext =
makeMetadataKeysWithContext(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
make_metadata_keys_with_context(metadataKey, {contextGroups: jsonDecoratorOptions.contextGroups});
for (const metadataKeyWithContext of metadataKeysWithContext) {
decoratorsToBeApplied[metadataKeyWithContext] = jsonDecoratorOptions;
}
Expand Down
28 changes: 15 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import {
// eslint-disable-next-line camelcase
find_metadata_by_metadata_key_with_context,
// eslint-disable-next-line camelcase
make_metadata_key_with_context } from 'jackson-wasm';
make_metadata_key_with_context,
// eslint-disable-next-line camelcase
make_metadata_keys_with_context} from 'jackson-wasm';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { JacksonError } from './core/JacksonError';

Expand All @@ -48,17 +50,17 @@ export interface MakeMetadataKeysWithContextOptions {
/**
* @internal
*/
export const makeMetadataKeysWithContext = (
key: string,
options: MakeMetadataKeysWithContextOptions
): string[] =>
options.contextGroups != null && options.contextGroups.length > 0
? options.contextGroups.map((contextGroup) =>
make_metadata_key_with_context(key, new MakeMetadataKeyWithContextOptions(contextGroup, options.prefix, options.suffix))
)
: [
make_metadata_key_with_context(key, new MakeMetadataKeyWithContextOptions(null, options.prefix, options.suffix)),
];
// export const makeMetadataKeysWithContext = (
// key: string,
// options: MakeMetadataKeysWithContextOptions
// ): string[] =>
// options.contextGroups != null && options.contextGroups.length > 0
// ? options.contextGroups.map((contextGroup) =>
// make_metadata_key_with_context(key, new MakeMetadataKeyWithContextOptions(contextGroup, options.prefix, options.suffix))
// )
// : [
// make_metadata_key_with_context(key, new MakeMetadataKeyWithContextOptions(null, options.prefix, options.suffix)),
// ];

/**
* @internal
Expand All @@ -84,7 +86,7 @@ export const defineMetadata = (
...options,
};

makeMetadataKeysWithContext(
make_metadata_keys_with_context(
metadataKey,
makeMetadataKeysWithContextOptions
).forEach((metadataKeyWithContext) => {
Expand Down

0 comments on commit 4d28b1b

Please sign in to comment.