Skip to content

Commit

Permalink
Fixes in command for transpiler (#469)
Browse files Browse the repository at this point in the history
- Added metadata.context in symbolTable,
- Changed send to publish  in commandBus
  • Loading branch information
Traigor authored May 31, 2023
1 parent 48e228f commit 1877d0d
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ DomainEventHandler SendEmailAfterDepositsIncrementedDomainEventHandler() {
handle(event: DepositsIncrementedDomainEvent): (OK(void), Errors(
ApplicationErrors.AccountNotFound
)){
this.commandBus.send(command);
this.commandBus.publish(command);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DomainEventHandler SendEmailAfterDepositsIncrementedDomainEventHandler(customerService: CustomerServicePort) {
handle(event: DepositsIncrementedDomainEvent): (OK(void), Errors()){
this.commandBus.send(command);
this.commandBus.publish(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const validDomainEventHandlersTestCases = [
new ExpressionBuilderDirector().buildMethodCallExpression(
new ExpressionBuilderDirector().buildThisMemberExpressionOutOfVariables(
'commandBus',
'send',
'publish',
),
new ArgumentListBuilderDirector().buildArgumentList(['command']),
),
Expand Down Expand Up @@ -62,7 +62,7 @@ export const validDomainEventHandlersTestCases = [
new ExpressionBuilderDirector().buildMethodCallExpression(
new ExpressionBuilderDirector().buildThisMemberExpressionOutOfVariables(
'commandBus',
'send',
'publish',
),
new ArgumentListBuilderDirector().buildArgumentList(['command']),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ IntegrationEventHandler MoneyDepositedIntegrationEventHandler() {

handle(event: [banking][banking]MoneyDepositedIntegrationEvent): (OK(void), Errors()) {

this.commandBus.send(command);
this.commandBus.publish(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ IntegrationEventHandler MoneyDepositedIntegrationEventHandler(commandBus: IComma

handle(event: [banking][banking]MoneyDepositedIntegrationEvent): (OK(void), Errors()){

this.commandBus.send(command);
this.commandBus.publish(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const validIntegrationEventHandlersTestCases = [
new ExpressionBuilderDirector().buildMethodCallExpression(
new ExpressionBuilderDirector().buildThisMemberExpressionOutOfVariables(
'commandBus',
'send',
'publish',
),
new ArgumentListBuilderDirector().buildArgumentList(['command']),
),
Expand Down Expand Up @@ -66,7 +66,7 @@ export const validIntegrationEventHandlersTestCases = [
new ExpressionBuilderDirector().buildMethodCallExpression(
new ExpressionBuilderDirector().buildThisMemberExpressionOutOfVariables(
'commandBus',
'send',
'publish',
),
new ArgumentListBuilderDirector().buildArgumentList(['command']),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DomainEventHandler TodoCreatedDomainEventHandler() {
handle(event: TodoCreatedDomainEvent): (OK(void), Errors()) {
this.commandBus.send(event);
this.commandBus.publish(event);
}
}

Expand Down Expand Up @@ -48,7 +48,7 @@ IntegrationEventHandler MoneyDepositedIntegrationEventHandler() {
subject: 'Money deposited',
body: 'Money deposited to your account'
});
this.commandBus.send(command);
this.commandBus.publish(command);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ DomainEvent DepositsIncrementedDomainEvent<AccountEntity> {
DomainEventHandler SendEmailAfterMoneyDepositedDomainEventHandler(customerService: CustomerServicePort) {
handle(event: DepositsIncrementedDomainEvent): (OK(void), Errors()){
const command = SendEmailCommand.create({ customerId: event.customerId, amount: event.amount});
this.commandBus.send(command);
this.commandBus.publish(command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ IntegrationEventHandler MoneyDepositedIntegrationEventHandler() {
const command = IncrementDepositsCommand.create({
accountId: accountId,
})
this.commandBus.send(command);
this.commandBus.publish(command);
}
}

Expand Down
34 changes: 32 additions & 2 deletions transpiler/__tests__/end-to-end/mocks/symbol-table/symbol-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type SymbolTableTestCase = {
inputSetup: string;
expectedSymbolTable: PrimitiveSymbolTable;
};
const METADATA_TYPE = '{ context: { jwt: string; userId: string } }';
const CONTEXT_TYPE = '{ jwt: string; userId: string }';
const SCOPE_NAMES = SymbolTableManager.SCOPE_NAMES;

export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
Expand Down Expand Up @@ -67,6 +69,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
.insert('command', new ParameterSymbolEntry('WithdrawMoneyCommand'))
.insert('command.accountId', new MemberDotSymbolEntry('string'))
.insert('command.amount', new MemberDotSymbolEntry('int32'))
.insert('command.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('command.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('command.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('command.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('accountId', 'UUIDv4', true)
.insert(
'this.accountRepo.getById()',
Expand Down Expand Up @@ -142,6 +148,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
.insert('command', new ParameterSymbolEntry('WithdrawMoneyCommand'))
.insert('command.accountId', new MemberDotSymbolEntry('string'))
.insert('command.amount', new MemberDotSymbolEntry('int32'))
.insert('command.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('command.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('command.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('command.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('accountId', 'UUIDv4', true)
.insert(
'this.accountRepo.getById()',
Expand Down Expand Up @@ -221,6 +231,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
.insert('command', new ParameterSymbolEntry('WithdrawMoneyCommand'))
.insert('command.accountId', new MemberDotSymbolEntry('string'))
.insert('command.amount', new MemberDotSymbolEntry('int32'))
.insert('command.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('command.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('command.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('command.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('accountId', 'UUIDv4', true)
.insert(
'this.accountRepo.getById()',
Expand Down Expand Up @@ -292,6 +306,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
new SymbolTableBuilder()
.insert('query', new ParameterSymbolEntry('GetAccountQuery'))
.insert('query.accountId', new MemberDotSymbolEntry('string'))
.insert('query.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('query.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('query.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('query.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('requestId', 'string', true)
.insert(
'this.accountRepo.getById()',
Expand Down Expand Up @@ -421,8 +439,12 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
.insert('event', new ParameterSymbolEntry('DepositsIncrementedDomainEvent'))
.insert('event.customerId', new MemberDotSymbolEntry('string'))
.insert('event.amount', new MemberDotSymbolEntry('float'))
.insert('event.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('event.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('event.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insert('event.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('command', 'SendEmailCommand', true)
.insert('this.commandBus.send()', new MethodCallSymbolEntry('void')),
.insert('this.commandBus.publish()', new MethodCallSymbolEntry('void')),
),
)
.insertChildScope('DepositsIncrementedDomainEvent', new SymbolTableBuilder())
Expand Down Expand Up @@ -457,7 +479,7 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
.insert('event.accountId', new MemberDotSymbolEntry('string'))
.insertVariableSymbolEntry('accountId', 'string', true)
.insertVariableSymbolEntry('command', 'IncrementDepositsCommand', true)
.insert('this.commandBus.send()', new MethodCallSymbolEntry('void')),
.insert('this.commandBus.publish()', new MethodCallSymbolEntry('void')),
),
)
.insertChildScope('IntegrationMoneyDepositedSchemaV1', new SymbolTableBuilder())
Expand Down Expand Up @@ -737,6 +759,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
new SymbolTableBuilder()
.insert('command', new ParameterSymbolEntry('WithdrawMoneyCommand'))
.insert('command.accountId', new MemberDotSymbolEntry('string'))
.insert('command.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('command.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('command.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('command.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insert('accountEntity', new VariableSymbolEntry('AccountEntity', true))
.insert(
'AccountEntity.create()',
Expand Down Expand Up @@ -786,6 +812,10 @@ export const SYMBOL_TABLE_TEST_CASES: SymbolTableTestCase[] = [
new SymbolTableBuilder()
.insert('command', new ParameterSymbolEntry('AddTodoCommand'))
.insert('command.title', new MemberDotSymbolEntry('string'))
.insert('command.metadata', new MemberDotSymbolEntry(METADATA_TYPE))
.insert('command.metadata.context', new MemberDotSymbolEntry(CONTEXT_TYPE))
.insert('command.metadata.context.jwt', new MemberDotSymbolEntry('string'))
.insert('command.metadata.context.userId', new MemberDotSymbolEntry('string'))
.insert('titlevo', new VariableSymbolEntry('TitleVO', true))
.insert(
'TitleVO.create()',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export class SendEmailAfterMoneyDepositedHandler implements Application.IHandleD
})
public async handle(event: MoneyDepositedToAccountDomainEvent): Promise<Either<void, never>> {
const email = '[email protected]';
await this.commandBus.send(email);
await this.commandBus.publish(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export class SendEmailAfterMoneyDepositedHandler implements Application.IHandleD
const marketingNotificationService = new MarketingNotificationDomainService(this.repo);
const emailToBeSentInfoResponse =
await marketingNotificationService.getNotificationTemplateToBeSent(user);
await this.commandBus.send(emailToBeSentInfoResponse.value);
await this.commandBus.publish(emailToBeSentInfoResponse.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const VALID_DOMAIN_EVENT_HANDLER_TEST_CASES: Array<TDomainEventHandlerTes
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression('email'),
),
],
Expand Down Expand Up @@ -66,7 +66,7 @@ export const VALID_DOMAIN_EVENT_HANDLER_TEST_CASES: Array<TDomainEventHandlerTes
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression('email'),
),
],
Expand Down Expand Up @@ -120,7 +120,7 @@ export const VALID_DOMAIN_EVENT_HANDLER_TEST_CASES: Array<TDomainEventHandlerTes
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression(
'emailToBeSentInfoResponse',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export class SendEmailAfterMoneyDepositedHandler implements Application.IHandleD
event: MoneyDepositedToAccountDomainEvent,
): Promise<Either<void, DomainErrors.SendEmailError>> {
const email = '[email protected]';
await this.commandBus.send(email);
await this.commandBus.publish(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export class MoneyDepositedIntegrationHandler implements Application.IHandleInte
})
public async handle(event: MoneyDepositedIntegrationEvent): Promise<Either<void, never>> {
const email = '[email protected]';
await this.commandBus.send(email);
await this.commandBus.publish(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export class MoneyDepositedIntegrationHandler implements Application.IHandleInte
event: UserRegisteredIntegrationEvent,
): Promise<Either<void, ApplicationErrors.InvalidMoneyError>> {
const email = '[email protected]';
await this.commandBus.send(email);
await this.commandBus.publish(email);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const VALID_INTEGRATION_EVENT_HANDLER_TEST_CASES: Array<TIntegrationEvent
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression('email'),
),
],
Expand Down Expand Up @@ -73,7 +73,7 @@ export const VALID_INTEGRATION_EVENT_HANDLER_TEST_CASES: Array<TIntegrationEvent
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression('email'),
),
],
Expand Down Expand Up @@ -106,7 +106,7 @@ export const VALID_INTEGRATION_EVENT_HANDLER_TEST_CASES: Array<TIntegrationEvent
),
new ExpressionBuilderDirector().buildThisDependencyMethodCall(
'commandBus',
'send',
'publish',
new ArgumentListDirector().buildArgumentListWithIdentifierExpression('email'),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ export class MoneyDepositedIntegrationHandler implements Application.IHandleInte
})
public async handle(event: MoneyDepositedIntegrationEvent): Promise<Either<void, never>> {
const email = '[email protected]';
await this.commandBus.send(email);
await this.commandBus.publish(email);
}
}
2 changes: 1 addition & 1 deletion transpiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitloops/bl-transpiler",
"version": "0.6.7",
"version": "0.6.9",
"description": "The one and only Bitloops Language transpiler",
"engines": {
"node": ">= 13"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EventHandlerBusDependenciesNode extends IntermediateASTNode {

static getCommandBusMethodType(): Record<string, BitloopsPrimaryTypeNode> {
return {
send: new BitloopsPrimaryTypeNodeBuilderDirector().buildPrimitivePrimaryType('void'),
publish: new BitloopsPrimaryTypeNodeBuilderDirector().buildPrimitivePrimaryType('void'),
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class EventHandleNode extends IntermediateASTNode {
const initialSymbolTable = symbolTableManager.getSymbolTable();
symbolTableManager.createSymbolTableChildScope(SymbolTableManager.SCOPE_NAMES.HANDLE, this);
const parameter = this.getParameter();
symbolTableManager.addMetadataContext(parameter.getIdentifier());
parameter.addToSymbolTable(symbolTableManager);

const statementList = this.getStatementList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ExecuteNode extends IntermediateASTNode {
symbolTableManager.createSymbolTableChildScope(SymbolTableManager.SCOPE_NAMES.EXECUTE, this);
const executeParameter = this.getParameter();
if (executeParameter) {
symbolTableManager.addMetadataContext(executeParameter.getIdentifier());
executeParameter.addToSymbolTable(symbolTableManager);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { IntermediateASTTree } from '../../ast/core/intermediate-ast/IntermediateASTTree.js';
import { IntermediateASTNode } from '../../ast/core/intermediate-ast/nodes/IntermediateASTNode.js';
import { TBoundedContexts } from '../../ast/core/types.js';
import { ClassTypeParameterSymbolEntry, ClassTypeThisSymbolEntry } from './SymbolEntry.js';
import {
ClassTypeParameterSymbolEntry,
ClassTypeThisSymbolEntry,
MemberDotSymbolEntry,
} from './SymbolEntry.js';
import { SymbolTable } from './SymbolTable.js';

export class SymbolTableManager {
Expand Down Expand Up @@ -64,6 +68,19 @@ export class SymbolTableManager {
this.symbolTable.insert(queryBusKey, new ClassTypeParameterSymbolEntry('QueryBusPort'));
}

public addMetadataContext(identifier: string): void {
const identifierMetadataKey = this.joinWithDot([identifier, 'metadata']);
this.symbolTable.insert(
identifierMetadataKey,
new MemberDotSymbolEntry(this.getMetadataType()),
);
const identifierContextKey = this.joinWithDot([identifierMetadataKey, 'context']);
this.symbolTable.insert(identifierContextKey, new MemberDotSymbolEntry(this.getContextType()));
const jwtKey = this.joinWithDot([identifierContextKey, 'jwt']);
this.symbolTable.insert(jwtKey, new MemberDotSymbolEntry('string'));
const userIdKey = this.joinWithDot([identifierContextKey, 'userId']);
this.symbolTable.insert(userIdKey, new MemberDotSymbolEntry('string'));
}
private joinThisWithIdentifier(identifier: string): string {
return SymbolTableManager.THIS + '.' + identifier;
}
Expand All @@ -75,4 +92,12 @@ export class SymbolTableManager {
public joinWithDot(memberDotMembers: string[]): string {
return memberDotMembers.join('.');
}

private getMetadataType(): string {
return '{ context: { jwt: string; userId: string } }';
} //type of object is string for now

private getContextType(): string {
return '{ jwt: string; userId: string }';
} //type of object is string for now
}

0 comments on commit 1877d0d

Please sign in to comment.