Skip to content

Commit

Permalink
refactor(ajsttil): update TypeMetadata without xor metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
notaphplover committed Sep 21, 2023
1 parent f3b9467 commit e05f39a
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 77 deletions.
109 changes: 34 additions & 75 deletions packages/transformers/ajsttil/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,36 @@
import { JsonValue } from '@cuaklabs/json-schema-types';
import {
AndTypeMetadata,
AnyTypeMetadata,
ArrayTypeMetadata,
BooleanTypeMetadata,
FloatTypeMetadata,
IntegerTypeMetadata,
LiteralTypeMetadata,
NoneTypeMetadata,
ObjectTypeMetadata,
OrTypeMetadata,
PropertyTypeMetadata,
StringIndexSignatureType,
StringTypeMetadata,
TypeMedatata,
TypeMetadataKind,
} from './metadata/models/TypeMetadata';

export enum TypeMetadataKind {
and,
anyType,
arrayType,
booleanType,
floatType,
integerType,
literalType,
noneType,
objectType,
or,
propertyType,
stringIndexSignatureType,
stringType,
xor,
}
export type {
AndTypeMetadata,
AnyTypeMetadata,
ArrayTypeMetadata,
BooleanTypeMetadata,
FloatTypeMetadata,
IntegerTypeMetadata,
LiteralTypeMetadata,
NoneTypeMetadata,
ObjectTypeMetadata,
OrTypeMetadata,
PropertyTypeMetadata,
StringIndexSignatureType,
StringTypeMetadata,
TypeMedatata,
};

export interface BaseTypeMetadata<TKind extends TypeMetadataKind> {
id?: string;
kind: TKind;
}

export interface OneChildTypeMetadata<TKind extends TypeMetadataKind>
extends BaseTypeMetadata<TKind> {
child: TypeMedatata;
}

export interface ManyChildrenTypeMetadata<
TKind extends TypeMetadataKind,
TChildren extends TypeMedatata[] = TypeMedatata[],
> extends BaseTypeMetadata<TKind> {
children: TChildren;
}

export type AndTypeMetadata = ManyChildrenTypeMetadata<TypeMetadataKind.and>;
export type AnyTypeMetadata = BaseTypeMetadata<TypeMetadataKind.anyType>;
export type ArrayTypeMetadata =
OneChildTypeMetadata<TypeMetadataKind.arrayType>;
export type BooleanTypeMetadata =
BaseTypeMetadata<TypeMetadataKind.booleanType>;
export type FloatTypeMetadata = BaseTypeMetadata<TypeMetadataKind.floatType>;
export type IntegerTypeMetadata =
BaseTypeMetadata<TypeMetadataKind.integerType>;
export interface LiteralTypeMetadata
extends BaseTypeMetadata<TypeMetadataKind.literalType> {
literal: JsonValue;
}
export type NoneTypeMetadata = BaseTypeMetadata<TypeMetadataKind.noneType>;
export type ObjectTypeMetadata = BaseTypeMetadata<TypeMetadataKind.objectType>;
export type OrTypeMetadata = ManyChildrenTypeMetadata<TypeMetadataKind.or>;
export interface PropertyTypeMetadata
extends OneChildTypeMetadata<TypeMetadataKind.propertyType> {
isOptional: boolean;
property: string;
}
export type StringIndexSignatureType =
OneChildTypeMetadata<TypeMetadataKind.stringIndexSignatureType>;
export type StringTypeMetadata = BaseTypeMetadata<TypeMetadataKind.stringType>;
export type XorTypeMetadata = ManyChildrenTypeMetadata<TypeMetadataKind.xor>;

export type TypeMedatata =
| AndTypeMetadata
| AnyTypeMetadata
| ArrayTypeMetadata
| BooleanTypeMetadata
| FloatTypeMetadata
| IntegerTypeMetadata
| LiteralTypeMetadata
| NoneTypeMetadata
| ObjectTypeMetadata
| OrTypeMetadata
| PropertyTypeMetadata
| StringIndexSignatureType
| StringTypeMetadata
| XorTypeMetadata;
export { TypeMetadataKind };
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TypeMedatata } from '../models/TypeMetadata';
import { TypeMetadataGrammar } from '../models/TypeMetadataGrammar';

export function normalize(typeMetadata: TypeMedatata): TypeMetadataGrammar {
const grammar: TypeMetadataGrammar = {
productions: new Map(),
root: typeMetadata,
};

return grammar;
}
74 changes: 74 additions & 0 deletions packages/transformers/ajsttil/src/metadata/models/TypeMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { JsonValue } from '@cuaklabs/json-schema-types';

export enum TypeMetadataKind {
and,
anyType,
arrayType,
booleanType,
floatType,
integerType,
literalType,
noneType,
objectType,
or,
propertyType,
stringIndexSignatureType,
stringType,
}

export interface BaseTypeMetadata<TKind extends TypeMetadataKind> {
id?: string;
kind: TKind;
}

export interface OneChildTypeMetadata<TKind extends TypeMetadataKind>
extends BaseTypeMetadata<TKind> {
child: TypeMedatata;
}

export interface ManyChildrenTypeMetadata<
TKind extends TypeMetadataKind,
TChildren extends TypeMedatata[] = TypeMedatata[],
> extends BaseTypeMetadata<TKind> {
children: TChildren;
}

export type AndTypeMetadata = ManyChildrenTypeMetadata<TypeMetadataKind.and>;
export type AnyTypeMetadata = BaseTypeMetadata<TypeMetadataKind.anyType>;
export type ArrayTypeMetadata =
OneChildTypeMetadata<TypeMetadataKind.arrayType>;
export type BooleanTypeMetadata =
BaseTypeMetadata<TypeMetadataKind.booleanType>;
export type FloatTypeMetadata = BaseTypeMetadata<TypeMetadataKind.floatType>;
export type IntegerTypeMetadata =
BaseTypeMetadata<TypeMetadataKind.integerType>;
export interface LiteralTypeMetadata
extends BaseTypeMetadata<TypeMetadataKind.literalType> {
literal: JsonValue;
}
export type NoneTypeMetadata = BaseTypeMetadata<TypeMetadataKind.noneType>;
export type ObjectTypeMetadata = BaseTypeMetadata<TypeMetadataKind.objectType>;
export type OrTypeMetadata = ManyChildrenTypeMetadata<TypeMetadataKind.or>;
export interface PropertyTypeMetadata
extends OneChildTypeMetadata<TypeMetadataKind.propertyType> {
isOptional: boolean;
property: string;
}
export type StringIndexSignatureType =
OneChildTypeMetadata<TypeMetadataKind.stringIndexSignatureType>;
export type StringTypeMetadata = BaseTypeMetadata<TypeMetadataKind.stringType>;

export type TypeMedatata =
| AndTypeMetadata
| AnyTypeMetadata
| ArrayTypeMetadata
| BooleanTypeMetadata
| FloatTypeMetadata
| IntegerTypeMetadata
| LiteralTypeMetadata
| NoneTypeMetadata
| ObjectTypeMetadata
| OrTypeMetadata
| PropertyTypeMetadata
| StringIndexSignatureType
| StringTypeMetadata;
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ describe(transformJsonSchema.name, () => {
property: 'bar',
},
],
kind: TypeMetadataKind.xor,
kind: TypeMetadataKind.or,
},
],
[
Expand Down Expand Up @@ -436,6 +436,10 @@ describe(transformJsonSchema.name, () => {
{
kind: TypeMetadataKind.floatType,
},
{
kind: TypeMetadataKind.literalType,
literal: null,
},
{
kind: TypeMetadataKind.objectType,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ function handleJsonSchemaChildren(
{
kind: TypeMetadataKind.floatType,
},
{
kind: TypeMetadataKind.literalType,
literal: null,
},
{
kind: TypeMetadataKind.objectType,
},
Expand Down Expand Up @@ -198,7 +202,7 @@ function handleJsonSchemaSubschemas(
children: schema.oneOf.map((schema: JsonSchema) =>
transformJsonSchema(schema, context),
),
kind: TypeMetadataKind.xor,
kind: TypeMetadataKind.or,
});
}
}
Expand Down

0 comments on commit e05f39a

Please sign in to comment.