Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow using a type as auth model #1841

Merged
merged 3 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions packages/language/src/generated/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,15 @@ export function isLiteralExpr(item: unknown): item is LiteralExpr {
return reflection.isInstance(item, LiteralExpr);
}

export type ReferenceTarget = DataModelField | EnumField | FunctionParam;
export type MemberAccessTarget = DataModelField | TypeDefField;

export const MemberAccessTarget = 'MemberAccessTarget';

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

export type ReferenceTarget = DataModelField | EnumField | FunctionParam | TypeDefField;

export const ReferenceTarget = 'ReferenceTarget';

Expand Down Expand Up @@ -285,7 +293,7 @@ export function isDataModel(item: unknown): item is DataModel {
}

export interface DataModelAttribute extends AstNode {
readonly $container: DataModel | Enum;
readonly $container: DataModel | Enum | TypeDef;
readonly $type: 'DataModelAttribute';
args: Array<AttributeArg>
decl: Reference<Attribute>
Expand All @@ -298,7 +306,7 @@ export function isDataModelAttribute(item: unknown): item is DataModelAttribute
}

export interface DataModelField extends AstNode {
readonly $container: DataModel | Enum | FunctionDecl;
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
readonly $type: 'DataModelField';
attributes: Array<DataModelFieldAttribute>
comments: Array<string>
Expand Down Expand Up @@ -370,7 +378,7 @@ export function isEnum(item: unknown): item is Enum {
}

export interface EnumField extends AstNode {
readonly $container: DataModel | Enum | FunctionDecl;
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
readonly $type: 'EnumField';
attributes: Array<DataModelFieldAttribute>
comments: Array<string>
Expand Down Expand Up @@ -413,7 +421,7 @@ export function isFunctionDecl(item: unknown): item is FunctionDecl {
}

export interface FunctionParam extends AstNode {
readonly $container: DataModel | Enum | FunctionDecl;
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
readonly $type: 'FunctionParam';
name: RegularID
optional: boolean
Expand Down Expand Up @@ -482,7 +490,7 @@ export function isInvocationExpr(item: unknown): item is InvocationExpr {
export interface MemberAccessExpr extends AstNode {
readonly $container: Argument | ArrayExpr | AttributeArg | BinaryExpr | ConfigArrayExpr | ConfigField | ConfigInvocationArg | FieldInitializer | FunctionDecl | MemberAccessExpr | PluginField | ReferenceArg | UnaryExpr | UnsupportedFieldType;
readonly $type: 'MemberAccessExpr';
member: Reference<DataModelField>
member: Reference<MemberAccessTarget>
operand: Expression
}

Expand Down Expand Up @@ -631,6 +639,7 @@ export function isThisExpr(item: unknown): item is ThisExpr {
export interface TypeDef extends AstNode {
readonly $container: Model;
readonly $type: 'TypeDef';
attributes: Array<DataModelAttribute>
comments: Array<string>
fields: Array<TypeDefField>
name: RegularID
Expand All @@ -643,7 +652,7 @@ export function isTypeDef(item: unknown): item is TypeDef {
}

export interface TypeDefField extends AstNode {
readonly $container: TypeDef;
readonly $container: DataModel | Enum | FunctionDecl | TypeDef;
readonly $type: 'TypeDefField';
attributes: Array<DataModelFieldAttribute>
comments: Array<string>
Expand Down Expand Up @@ -730,6 +739,7 @@ export type ZModelAstType = {
InvocationExpr: InvocationExpr
LiteralExpr: LiteralExpr
MemberAccessExpr: MemberAccessExpr
MemberAccessTarget: MemberAccessTarget
Model: Model
ModelImport: ModelImport
NullExpr: NullExpr
Expand All @@ -754,7 +764,7 @@ export type ZModelAstType = {
export class ZModelAstReflection extends AbstractAstReflection {

getAllTypes(): string[] {
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'BooleanLiteral', 'ConfigArrayExpr', 'ConfigExpr', 'ConfigField', 'ConfigInvocationArg', 'ConfigInvocationExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'InternalAttribute', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'Model', 'ModelImport', 'NullExpr', 'NumberLiteral', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'StringLiteral', 'ThisExpr', 'TypeDeclaration', 'TypeDef', 'TypeDefField', 'TypeDefFieldType', 'TypeDefFieldTypes', 'UnaryExpr', 'UnsupportedFieldType'];
return ['AbstractDeclaration', 'Argument', 'ArrayExpr', 'Attribute', 'AttributeArg', 'AttributeParam', 'AttributeParamType', 'BinaryExpr', 'BooleanLiteral', 'ConfigArrayExpr', 'ConfigExpr', 'ConfigField', 'ConfigInvocationArg', 'ConfigInvocationExpr', 'DataModel', 'DataModelAttribute', 'DataModelField', 'DataModelFieldAttribute', 'DataModelFieldType', 'DataSource', 'Enum', 'EnumField', 'Expression', 'FieldInitializer', 'FunctionDecl', 'FunctionParam', 'FunctionParamType', 'GeneratorDecl', 'InternalAttribute', 'InvocationExpr', 'LiteralExpr', 'MemberAccessExpr', 'MemberAccessTarget', 'Model', 'ModelImport', 'NullExpr', 'NumberLiteral', 'ObjectExpr', 'Plugin', 'PluginField', 'ReferenceArg', 'ReferenceExpr', 'ReferenceTarget', 'StringLiteral', 'ThisExpr', 'TypeDeclaration', 'TypeDef', 'TypeDefField', 'TypeDefFieldType', 'TypeDefFieldTypes', 'UnaryExpr', 'UnsupportedFieldType'];
}

protected override computeIsSubtype(subtype: string, supertype: string): boolean {
Expand Down Expand Up @@ -788,14 +798,17 @@ export class ZModelAstReflection extends AbstractAstReflection {
return this.isSubtype(AbstractDeclaration, supertype) || this.isSubtype(TypeDeclaration, supertype);
}
case DataModelField:
case EnumField:
case FunctionParam: {
return this.isSubtype(ReferenceTarget, supertype);
case TypeDefField: {
return this.isSubtype(MemberAccessTarget, supertype) || this.isSubtype(ReferenceTarget, supertype);
}
case Enum:
case TypeDef: {
return this.isSubtype(AbstractDeclaration, supertype) || this.isSubtype(TypeDeclaration, supertype) || this.isSubtype(TypeDefFieldTypes, supertype);
}
case EnumField:
case FunctionParam: {
return this.isSubtype(ReferenceTarget, supertype);
}
case InvocationExpr:
case LiteralExpr: {
return this.isSubtype(ConfigExpr, supertype) || this.isSubtype(Expression, supertype);
Expand Down Expand Up @@ -826,7 +839,7 @@ export class ZModelAstReflection extends AbstractAstReflection {
return FunctionDecl;
}
case 'MemberAccessExpr:member': {
return DataModelField;
return MemberAccessTarget;
}
case 'ReferenceExpr:target': {
return ReferenceTarget;
Expand Down Expand Up @@ -1055,6 +1068,7 @@ export class ZModelAstReflection extends AbstractAstReflection {
return {
name: 'TypeDef',
mandatory: [
{ name: 'attributes', type: 'array' },
{ name: 'comments', type: 'array' },
{ name: 'fields', type: 'array' }
]
Expand Down
72 changes: 58 additions & 14 deletions packages/language/src/generated/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/rules@38"
"$ref": "#/types@1"
},
"deprecatedSyntax": false
}
Expand Down Expand Up @@ -2165,7 +2165,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/types@2"
"$ref": "#/types@3"
},
"terminal": {
"$type": "RuleCall",
Expand Down Expand Up @@ -2257,16 +2257,33 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"value": "{"
},
{
"$type": "Assignment",
"feature": "fields",
"operator": "+=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@41"
"$type": "Alternatives",
"elements": [
{
"$type": "Assignment",
"feature": "fields",
"operator": "+=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@41"
},
"arguments": []
}
},
"arguments": []
},
{
"$type": "Assignment",
"feature": "attributes",
"operator": "+=",
"terminal": {
"$type": "RuleCall",
"rule": {
"$ref": "#/rules@55"
},
"arguments": []
}
}
],
"cardinality": "*"
},
{
Expand Down Expand Up @@ -2375,7 +2392,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/types@1"
"$ref": "#/types@2"
},
"terminal": {
"$type": "RuleCall",
Expand Down Expand Up @@ -2827,7 +2844,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/types@2"
"$ref": "#/types@3"
},
"terminal": {
"$type": "RuleCall",
Expand Down Expand Up @@ -3255,7 +3272,7 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"terminal": {
"$type": "CrossReference",
"type": {
"$ref": "#/types@2"
"$ref": "#/types@3"
},
"terminal": {
"$type": "RuleCall",
Expand Down Expand Up @@ -3829,6 +3846,12 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
"$ref": "#/rules@38"
}
},
{
"$type": "SimpleType",
"typeRef": {
"$ref": "#/rules@41"
}
},
{
"$type": "SimpleType",
"typeRef": {
Expand All @@ -3838,6 +3861,27 @@ export const ZModelGrammar = (): Grammar => loadedZModelGrammar ?? (loadedZModel
]
}
},
{
"$type": "Type",
"name": "MemberAccessTarget",
"type": {
"$type": "UnionType",
"types": [
{
"$type": "SimpleType",
"typeRef": {
"$ref": "#/rules@38"
}
},
{
"$type": "SimpleType",
"typeRef": {
"$ref": "#/rules@41"
}
}
]
}
},
{
"$type": "Type",
"name": "TypeDefFieldTypes",
Expand Down
15 changes: 10 additions & 5 deletions packages/language/src/zmodel.langium
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ ConfigArrayExpr:
ConfigExpr:
LiteralExpr | InvocationExpr | ConfigArrayExpr;

type ReferenceTarget = FunctionParam | DataModelField | EnumField;
type ReferenceTarget = FunctionParam | DataModelField | TypeDefField | EnumField;
ThisExpr:
value='this';

Expand Down Expand Up @@ -94,18 +94,20 @@ FieldInitializer:
InvocationExpr:
function=[FunctionDecl] '(' ArgumentList? ')';

// binary operator precedence follow Javascript's rules:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table
type MemberAccessTarget = DataModelField | TypeDefField;

MemberAccessExpr infers Expression:
PrimaryExpr (
{infer MemberAccessExpr.operand=current}
('.' member=[DataModelField])
('.' member=[MemberAccessTarget])
)*;

UnaryExpr:
operator=('!') operand=MemberAccessExpr;

// binary operator precedence follow Javascript's rules:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#table

CollectionPredicateExpr infers Expression:
MemberAccessExpr (
{infer BinaryExpr.left=current}
Expand Down Expand Up @@ -179,10 +181,12 @@ DataModelField:
DataModelFieldType:
(type=BuiltinType | unsupported=UnsupportedFieldType | reference=[TypeDeclaration:RegularID]) (array?='[' ']')? (optional?='?')?;

// TODO: unify TypeDef and abstract DataModel
TypeDef:
(comments+=TRIPLE_SLASH_COMMENT)*
'type' name=RegularID '{' (
fields+=TypeDefField
fields+=TypeDefField |
attributes+=DataModelAttribute
)*
'}';

Expand Down Expand Up @@ -245,6 +249,7 @@ type TypeDeclaration = DataModel | TypeDef | Enum;
DataModelFieldAttribute:
decl=[Attribute:FIELD_ATTRIBUTE_NAME] ('(' AttributeArgList? ')')?;

// TODO: need rename since it's for both DataModel and TypeDef
DataModelAttribute:
TRIPLE_SLASH_COMMENT* decl=[Attribute:MODEL_ATTRIBUTE_NAME] ('(' AttributeArgList? ')')?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,17 @@ generator client {
provider = "prisma-client-js"
}

/// @@allow('create', true)
/// @@allow('all', auth() == this)
model User {
id String @id() @default(cuid())
/// @email
/// @length(6, 32)
email String @unique()
/// @password
/// @omit
password String
posts Post[]
}

/// @@allow('read', auth() != null && published)
/// @@allow('all', author == auth())
model Post {
id String @id() @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt()
/// @length(1, 256)
title String
content String
published Boolean @default(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,17 @@ generator client {
provider = "prisma-client-js"
}

/// @@allow('create', true)
/// @@allow('all', auth() == this)
model User {
id String @id() @default(cuid())
/// @email
/// @length(6, 32)
email String @unique()
/// @password
/// @omit
password String
posts Post[]
}

/// @@allow('read', auth() != null && published)
/// @@allow('all', author == auth())
model Post {
id String @id() @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt()
/// @length(1, 256)
title String
content String
published Boolean @default(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ generator client {
provider = "prisma-client-js"
}

/// @@allow('all', true)
model User {
id Int @id() @default(autoincrement())
email String @unique()
posts Post[]
}

/// @@allow('all', true)
model Post {
id Int @id() @default(autoincrement())
name String
Expand Down
Loading
Loading