Skip to content

Commit

Permalink
improve melding behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
sparecycles committed Oct 15, 2024
1 parent 6cb701c commit bc9aa52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"tsx/esm",
"--stack-trace-limit=100",
"src/entry/main/cli/index.ts",
"test.http"
"test.http",
"--include"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
Expand Down
32 changes: 29 additions & 3 deletions packages/core/src/core/schema/definition/datum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ function mergeRepresentation<T extends Scalar>(
!pattern.vars.some((param) => isMelding(param))
) {
if (
!patterns?.some(
(existing) =>
isPatternTrivial(existing) || existing.source === source,
!patterns?.some((existing) =>
arePatternsMeldable(context, existing, pattern),
)
) {
return;
Expand All @@ -127,6 +126,32 @@ function mergeRepresentation<T extends Scalar>(
};
}

// this is a bit rough, still:
// the idea is that {{a}} and {{b}} are different patterns which don't by-default conflate
// (we would conflate them if explicitly imply meldability by `{{~b}}` on the incoming pattern).
//
// But a value and a parameter are conflated if they can be.
// we might want to use to context to check/apply this better.
function arePatternsMeldable(
_context: SchemaMergingContext<unknown>,
existing: Pattern,
pattern: PatternRegex,
) {
if (isPatternTrivial(existing)) {
return true;
}

if (existing.source === pattern.source) {
return true;
}

if (isPatternSimple(existing) && isPatternSimple(pattern)) {
return existing.vars[0].param == pattern.vars[0].param;
}

return false;
}

function defineScalar<T extends Scalar>(self: DatumRepresentation): Schema<T> {
return defineSchema<T>({
scope(context) {
Expand Down Expand Up @@ -234,6 +259,7 @@ function defineScalar<T extends Scalar>(self: DatumRepresentation): Schema<T> {
const { environment } = context;
const info = extractDatumInfo(context);
const mergedSelf = mergeRepresentation(context, self, info);

if (!mergedSelf) {
return;
}
Expand Down

0 comments on commit bc9aa52

Please sign in to comment.