Skip to content

Commit 72dfc4b

Browse files
committed
feat: allow apps to run any parser (DAP-4697)
1 parent a1d721c commit 72dfc4b

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

libs/engine/src/app/contexts/mutable-web-context/use-mutation-parsers.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,9 @@ export const useMutationParsers = (engine: Engine, apps: AppMetadata[]) => {
1212
try {
1313
setIsLoading(true)
1414

15-
// ToDo: move to service
16-
const uniqueNamespaces = Array.from(
17-
new Set(
18-
apps
19-
.map((app) => app.targets?.map((target) => target.namespace))
20-
.flat()
21-
.filter((ns) => !!ns)
22-
)
23-
)
15+
const parserConfigs = await engine.parserConfigService.getParserConfigsForApps(apps)
2416

25-
const parserConfigs = (
26-
await Promise.all(
27-
uniqueNamespaces.map((ns) => engine.parserConfigService.getParserConfig(ns))
28-
)
29-
).filter((pc) => pc !== null)
30-
31-
setParserConfigs(parserConfigs as ParserConfig[])
17+
setParserConfigs(parserConfigs)
3218
} catch (err) {
3319
if (err instanceof Error) {
3420
setError(err.message)

libs/engine/src/app/services/application/application.entity.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import { ParserConfigId } from '../parser-config/parser-config.entity'
12
import { Target } from '../target/target.entity'
23

34
export type AppId = string
45

6+
export const AnyParserValue = 'any'
7+
58
export type AppMetadataTarget = Target & {
69
static?: boolean
710
componentId: string
@@ -14,6 +17,7 @@ export type AppMetadata = {
1417
authorId: string
1518
appLocalId: string
1619
targets: AppMetadataTarget[]
20+
parsers?: typeof AnyParserValue | ParserConfigId[]
1721
controller?: string // BOS Widget ID
1822
metadata: {
1923
name?: string

libs/engine/src/app/services/application/application.repository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class ApplicationRepository {
8282
const storedAppMetadata = {
8383
[SelfKey]: JSON.stringify({
8484
targets: appMetadata.targets,
85+
parsers: appMetadata.parsers,
8586
}),
8687
metadata: appMetadata.metadata,
8788
}

libs/engine/src/app/services/application/application.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { IContextNode } from '@mweb/core'
22
import { MutationId } from '../mutation/mutation.entity'
33
import { TargetService } from '../target/target.service'
4-
import { AppId, AppMetadata, AppWithSettings } from './application.entity'
4+
import { AnyParserValue, AppId, AppMetadata, AppWithSettings } from './application.entity'
55
import { ApplicationRepository } from './application.repository'
66

77
export class ApplicationService {
88
constructor(private applicationRepository: ApplicationRepository) {}
99

1010
public getApplications(): Promise<AppMetadata[]> {
11+
// ToDo: out of gas
1112
return this.applicationRepository.getApplications()
1213
}
1314

libs/engine/src/app/services/parser-config/parser-config.repository.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,44 @@ const ParserKey = 'parser'
66
const SettingsKey = 'settings'
77
const SelfKey = ''
88
const KeyDelimiter = '/'
9+
const WildcardKey = '*'
10+
const RecursiveWildcardKey = '**'
911

1012
export class ParserConfigRepository {
1113
constructor(private socialDb: SocialDbService) {}
1214

15+
async getAllParserConfigs(): Promise<ParserConfig[]> {
16+
const keys = [
17+
WildcardKey, // any author id
18+
SettingsKey,
19+
ProjectIdKey,
20+
ParserKey,
21+
WildcardKey, // any app local id
22+
]
23+
24+
const queryResult = await this.socialDb.get([
25+
[...keys, RecursiveWildcardKey].join(KeyDelimiter),
26+
])
27+
28+
const parsersByKey = SocialDbService.splitObjectByDepth(queryResult, keys.length)
29+
30+
const parsers = Object.entries(parsersByKey).map(([key, parser]: [string, any]) => {
31+
const [authorId, , , , appLocalId] = key.split(KeyDelimiter)
32+
const globalParserId = [authorId, ParserKey, appLocalId].join(KeyDelimiter)
33+
34+
const config = JSON.parse(parser[SelfKey])
35+
36+
return {
37+
id: globalParserId,
38+
parserType: config.parserType,
39+
contexts: config.contexts,
40+
targets: config.targets,
41+
}
42+
})
43+
44+
return parsers
45+
}
46+
1347
async getParserConfig(globalParserId: string): Promise<ParserConfig | null> {
1448
if (globalParserId === 'mweb') return null
1549
const { accountId, parserLocalId } = this._extractParserIdFromNamespace(globalParserId)
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
1-
import { ParserConfigId } from './parser-config.entity'
1+
import { AnyParserValue, AppMetadata } from '../application/application.entity'
2+
import { ParserConfig, ParserConfigId } from './parser-config.entity'
23
import { ParserConfigRepository } from './parser-config.repository'
34

45
export class ParserConfigService {
56
constructor(private parserConfigRepository: ParserConfigRepository) {}
67

7-
public async getParserConfig(parserId: ParserConfigId) {
8+
public async getParserConfig(parserId: ParserConfigId): Promise<ParserConfig | null> {
89
return this.parserConfigRepository.getParserConfig(parserId)
910
}
11+
12+
public async getAllParserConfigs(): Promise<ParserConfig[]> {
13+
// ToDo: out of gas
14+
return this.parserConfigRepository.getAllParserConfigs()
15+
}
16+
17+
public async getParserConfigsForApps(apps: AppMetadata[]): Promise<ParserConfig[]> {
18+
const namespaces = new Set<ParserConfigId>()
19+
20+
for (const app of apps) {
21+
if (!app.parsers) {
22+
app.targets.forEach((target) => namespaces.add(target.namespace))
23+
} else if (app.parsers === AnyParserValue) {
24+
return this.getAllParserConfigs() // returns all parsers and breaks the loop!
25+
} else {
26+
app.parsers.forEach((parserGlobalId) => namespaces.add(parserGlobalId))
27+
}
28+
}
29+
30+
// ToDo: catch errors
31+
const parserConfigs = await Promise.all(
32+
Array.from(namespaces).map((ns) => this.getParserConfig(ns))
33+
)
34+
35+
return parserConfigs.filter((pc) => pc !== null)
36+
}
1037
}

0 commit comments

Comments
 (0)