Skip to content

Commit

Permalink
feature: a better "zod" plugin (zenstackhq#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Jun 30, 2023
1 parent 6397925 commit 2280f83
Show file tree
Hide file tree
Showing 86 changed files with 3,638 additions and 3,004 deletions.
78 changes: 40 additions & 38 deletions packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ export function isAbstractDeclaration(item: unknown): item is AbstractDeclaratio
return reflection.isInstance(item, AbstractDeclaration);
}

export type AttributeAttributeName = string;

export function isAttributeAttributeName(item: unknown): item is AttributeAttributeName {
return typeof item === 'string';
}

export type AttributeName = AttributeAttributeName | DataModelAttributeName | DataModelFieldAttributeName;
export type AttributeName = DataModelAttributeName | DataModelFieldAttributeName | InternalAttributeName;

export function isAttributeName(item: unknown): item is AttributeName {
return isDataModelAttributeName(item) || isDataModelFieldAttributeName(item) || isAttributeAttributeName(item);
return isDataModelAttributeName(item) || isDataModelFieldAttributeName(item) || isInternalAttributeName(item);
}

export type BuiltinType = 'BigInt' | 'Boolean' | 'Bytes' | 'DateTime' | 'Decimal' | 'Float' | 'Int' | 'Json' | 'String';
Expand Down Expand Up @@ -58,6 +52,12 @@ export function isExpressionType(item: unknown): item is ExpressionType {
return item === 'String' || item === 'Int' || item === 'Float' || item === 'Boolean' || item === 'DateTime' || item === 'Null' || item === 'Object' || item === 'Any' || item === 'Unsupported';
}

export type InternalAttributeName = string;

export function isInternalAttributeName(item: unknown): item is InternalAttributeName {
return typeof item === 'string';
}

export type QualifiedName = string;

export function isQualifiedName(item: unknown): item is QualifiedName {
Expand Down Expand Up @@ -114,7 +114,7 @@ export function isArrayExpr(item: unknown): item is ArrayExpr {
export interface Attribute extends AstNode {
readonly $container: Model;
readonly $type: 'Attribute';
attributes: Array<AttributeAttribute>
attributes: Array<InternalAttribute>
name: AttributeName
params: Array<AttributeParam>
}
Expand All @@ -126,7 +126,7 @@ export function isAttribute(item: unknown): item is Attribute {
}

export interface AttributeArg extends AstNode {
readonly $container: AttributeAttribute | DataModelAttribute | DataModelFieldAttribute;
readonly $container: DataModelAttribute | DataModelFieldAttribute | InternalAttribute;
readonly $type: 'AttributeArg';
name?: RegularID
value: Expression
Expand All @@ -138,19 +138,6 @@ export function isAttributeArg(item: unknown): item is AttributeArg {
return reflection.isInstance(item, AttributeArg);
}

export interface AttributeAttribute extends AstNode {
readonly $container: Attribute;
readonly $type: 'AttributeAttribute';
args: Array<AttributeArg>
decl: Reference<Attribute>
}

export const AttributeAttribute = 'AttributeAttribute';

export function isAttributeAttribute(item: unknown): item is AttributeAttribute {
return reflection.isInstance(item, AttributeAttribute);
}

export interface AttributeParam extends AstNode {
readonly $container: Attribute;
readonly $type: 'AttributeParam';
Expand Down Expand Up @@ -339,6 +326,7 @@ export function isFieldInitializer(item: unknown): item is FieldInitializer {
export interface FunctionDecl extends AstNode {
readonly $container: Model;
readonly $type: 'FunctionDecl';
attributes: Array<InternalAttribute>
expression?: Expression
name: RegularID
params: Array<FunctionParam>
Expand Down Expand Up @@ -405,6 +393,19 @@ export function isGeneratorField(item: unknown): item is GeneratorField {
return reflection.isInstance(item, GeneratorField);
}

export interface InternalAttribute extends AstNode {
readonly $container: Attribute | FunctionDecl;
readonly $type: 'InternalAttribute';
args: Array<AttributeArg>
decl: Reference<Attribute>
}

export const InternalAttribute = 'InternalAttribute';

export function isInternalAttribute(item: unknown): item is InternalAttribute {
return reflection.isInstance(item, InternalAttribute);
}

export interface InvocationExpr extends AstNode {
readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | DataSourceField | FieldInitializer | FunctionDecl | GeneratorField | MemberAccessExpr | PluginField | UnaryExpr | UnsupportedFieldType;
readonly $type: 'InvocationExpr';
Expand Down Expand Up @@ -586,7 +587,6 @@ export type ZModelAstType = {
ArrayExpr: ArrayExpr
Attribute: Attribute
AttributeArg: AttributeArg
AttributeAttribute: AttributeAttribute
AttributeParam: AttributeParam
AttributeParamType: AttributeParamType
BinaryExpr: BinaryExpr
Expand All @@ -606,6 +606,7 @@ export type ZModelAstType = {
FunctionParamType: FunctionParamType
GeneratorDecl: GeneratorDecl
GeneratorField: GeneratorField
InternalAttribute: InternalAttribute
InvocationExpr: InvocationExpr
LiteralExpr: LiteralExpr
MemberAccessExpr: MemberAccessExpr
Expand All @@ -627,7 +628,7 @@ export type ZModelAstType = {
export class ZModelAstReflection extends AbstractAstReflection {

getAllTypes(): string[] {
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeAttribute', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'DataSourceField', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'GeneratorField', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ModelImport', 'NullExpr', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'ThisExpr', 'TypeDeclaration', 'UnaryExpr', 'UnsupportedFieldType'];
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'DataSourceField', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'GeneratorField', 'InternalAttribute', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ModelImport', 'NullExpr', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'ThisExpr', 'TypeDeclaration', 'UnaryExpr', 'UnsupportedFieldType'];
}

protected override computeIsSubtype(subtype: string, supertype: string): boolean {
Expand Down Expand Up @@ -669,11 +670,6 @@ export class ZModelAstReflection extends AbstractAstReflection {
getReferenceType(refInfo: ReferenceInfo): string {
const referenceId = `${refInfo.container.$type}:${refInfo.property}`;
switch (referenceId) {
case 'AttributeAttribute:decl':
case 'DataModelAttribute:decl':
case 'DataModelFieldAttribute:decl': {
return Attribute;
}
case 'AttributeParamType:reference':
case 'DataModelFieldType:reference':
case 'FunctionParamType:reference': {
Expand All @@ -682,6 +678,11 @@ export class ZModelAstReflection extends AbstractAstReflection {
case 'DataModel:superTypes': {
return DataModel;
}
case 'DataModelAttribute:decl':
case 'DataModelFieldAttribute:decl':
case 'InternalAttribute:decl': {
return Attribute;
}
case 'InvocationExpr:function': {
return FunctionDecl;
}
Expand Down Expand Up @@ -716,14 +717,6 @@ export class ZModelAstReflection extends AbstractAstReflection {
]
};
}
case 'AttributeAttribute': {
return {
name: 'AttributeAttribute',
mandatory: [
{ name: 'args', type: 'array' }
]
};
}
case 'AttributeParam': {
return {
name: 'AttributeParam',
Expand Down Expand Up @@ -818,6 +811,7 @@ export class ZModelAstReflection extends AbstractAstReflection {
return {
name: 'FunctionDecl',
mandatory: [
{ name: 'attributes', type: 'array' },
{ name: 'params', type: 'array' }
]
};
Expand Down Expand Up @@ -846,6 +840,14 @@ export class ZModelAstReflection extends AbstractAstReflection {
]
};
}
case 'InternalAttribute': {
return {
name: 'InternalAttribute',
mandatory: [
{ name: 'args', type: 'array' }
]
};
}
case 'InvocationExpr': {
return {
name: 'InvocationExpr',
Expand Down
99 changes: 56 additions & 43 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1004,42 +1004,6 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "UnaryExpr",
"definition": {
"$type": "Group",
"elements": [
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Keyword",
"value": "!"
}
},
{
"$type": "Assignment",
"feature": "operand",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@9"
},
"arguments": []
}
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "MemberAccessExpr",
Expand Down Expand Up @@ -1102,6 +1066,42 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "UnaryExpr",
"definition": {
"$type": "Group",
"elements": [
{
"$type": "Assignment",
"feature": "operator",
"operator": "=",
"terminal": {
"$type": "Keyword",
"value": "!"
}
},
{
"$type": "Assignment",
"feature": "operand",
"operator": "=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@20"
},
"arguments": []
}
}
]
},
"definesHiddenTokens": false,
"entry": false,
"fragment": false,
"hiddenTokens": [],
"parameters": [],
"wildcard": false
},
{
"$type": "ParserRule",
"name": "CollectionPredicateExpr",
Expand All @@ -1115,7 +1115,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@21"
"$ref": "#/rules@20"
},
"arguments": []
},
Expand Down Expand Up @@ -1522,28 +1522,28 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@19"
"$ref": "#/rules@21"
},
"arguments": []
},
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@11"
"$ref": "#/rules@19"
},
"arguments": []
},
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@14"
"$ref": "#/rules@11"
},
"arguments": []
},
{
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@20"
"$ref": "#/rules@14"
},
"arguments": []
},
Expand Down Expand Up @@ -2242,6 +2242,19 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
{
"$type": "Keyword",
"value": "}"
},
{
"$type": "Assignment",
"feature": "attributes",
"operator": "+=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@50"
},
"arguments": []
},
"cardinality": "*"
}
]
},
Expand Down Expand Up @@ -2492,7 +2505,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
},
{
"$type": "ParserRule",
"name": "AttributeAttributeName",
"name": "InternalAttributeName",
"dataType": "string",
"definition": {
"$type": "Group",
Expand Down Expand Up @@ -2988,7 +3001,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
},
{
"$type": "ParserRule",
"name": "AttributeAttribute",
"name": "InternalAttribute",
"definition": {
"$type": "Group",
"elements": [
Expand Down
Loading

0 comments on commit 2280f83

Please sign in to comment.