Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/swift-foxes-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baseplate-dev/project-builder-lib': patch
---

Refactor reference extraction to use functional approach with `refContext` and `provides` instead of `withRefBuilder`
13 changes: 2 additions & 11 deletions examples/blog-with-auth/baseplate/project-definition.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"type": "backend"
}
],
"cliVersion": "0.3.1",
"cliVersion": "0.4.1",
"features": [
{ "id": "feature:I_EnFXnHjbGQ", "name": "accounts" },
{ "id": "feature:c28pJNS_89Oz", "name": "blogs" },
Expand Down Expand Up @@ -694,16 +694,7 @@
],
"plugins": [
{
"config": {
"initialUserRoles": ["admin"],
"modelRefs": {
"user": "User",
"userAccount": "UserAccount",
"userRole": "UserRole",
"userSession": "UserSession"
},
"userAdminRoles": ["admin"]
},
"config": { "initialUserRoles": ["admin"], "userAdminRoles": ["admin"] },
"id": "plugin:baseplate-dev_plugin-auth_local-auth",
"name": "local-auth",
"packageName": "@baseplate-dev/plugin-auth",
Expand Down
114 changes: 114 additions & 0 deletions packages/project-builder-lib/src/references/collect-refs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import type {
DefinitionEntityAnnotation,
DefinitionRefAnnotations,
DefinitionReferenceAnnotation,
DefinitionSlotAnnotation,
} from './markers.js';
import type { ReferencePath } from './types.js';

import {
DefinitionReferenceMarker,
REF_ANNOTATIONS_MARKER_SYMBOL,
} from './markers.js';

/**
* Result of collecting refs before slot resolution.
*/
export interface CollectedRefs {
/**
* All input entities from the definition.
*/
entities: DefinitionEntityAnnotation[];
/**
* All input references from the definition.
*/
references: DefinitionReferenceAnnotation[];
/**
* All slots from the definition.
*/
slots: DefinitionSlotAnnotation[];
}

function collectRefAnnotationsRecursive(
pathPrefix: ReferencePath,
value: unknown,
): CollectedRefs | undefined {
if (value === undefined || value === null) return undefined;
if (value instanceof DefinitionReferenceMarker) {
return {
entities: [],
references: [
{ ...value.reference, path: [...pathPrefix, ...value.reference.path] },
],
slots: [],
};
}
const collected = {
entities: [],
references: [],
slots: [],
} as CollectedRefs;
if (Array.isArray(value)) {
for (const [i, element] of value.entries()) {
const childCollected = collectRefAnnotationsRecursive(
[...pathPrefix, i],
element,
);
if (childCollected) {
collected.entities.push(...childCollected.entities);
collected.references.push(...childCollected.references);
collected.slots.push(...childCollected.slots);
}
}
return collected;
}
if (typeof value === 'object') {
if (REF_ANNOTATIONS_MARKER_SYMBOL in value) {
const annotations = value[
REF_ANNOTATIONS_MARKER_SYMBOL
] as DefinitionRefAnnotations;
collected.entities.push(
...annotations.entities.map((entity) => ({
...entity,
path: [...pathPrefix, ...entity.path],
})),
);
collected.references.push(
...annotations.references.map((reference) => ({
...reference,
path: [...pathPrefix, ...reference.path],
})),
);
collected.slots.push(
...annotations.slots.map((slot) => ({
...slot,
path: [...pathPrefix, ...slot.path],
})),
);
}
for (const [key, childValue] of Object.entries(value)) {
if (typeof key !== 'string') continue;
const childCollected = collectRefAnnotationsRecursive(
[...pathPrefix, key],
childValue,
);
if (childCollected) {
collected.entities.push(...childCollected.entities);
collected.references.push(...childCollected.references);
collected.slots.push(...childCollected.slots);
}
}
return collected;
}
return undefined;
}

export function collectRefs(value: unknown): CollectedRefs {
return (
collectRefAnnotationsRecursive([], value) ?? {
entities: [],
references: [],
slots: [],
}
);
}
Loading