Skip to content
Open
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
18 changes: 16 additions & 2 deletions packages/alchemy/src/Platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export interface PlatformProps {
isExternal?: boolean;
}

/** Synthetic Config nodes such as an absent `Config.option` have nothing to bind. */
const isBindableRuntimeConfigNode = (
path: ReadonlyArray<unknown>,
value: unknown,
): boolean => path.length > 0 && value !== undefined;

/**
* Provide the platform class's layer (`cls.make(props, impl)`) with a
* lifetime that matches the phase.
Expand Down Expand Up @@ -569,14 +575,22 @@ export const Platform = <
path.map((p) => p.toString()).join("_"),
);
const node = yield* configProvider.load(path);
if (phase === "plan" && node) {
if (
phase === "plan" &&
node &&
isBindableRuntimeConfigNode(path, node.value)
) {
// bind it to the RuntimeContext if running in plan phase
const output = Output.literal(
Redacted.make(node.value),
);
yield* ctx?.set(key, output) ?? Effect.void;
return node;
} else if (phase === "runtime" && ctx) {
} else if (
phase === "runtime" &&
ctx &&
key.length > 0
) {
// retrieve from the RuntimeContext if running in runtime phase
const value =
yield* ctx.get<Redacted.Redacted<string>>(key);
Expand Down
83 changes: 83 additions & 0 deletions packages/alchemy/test/PlatformConfigBindings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Platform } from "@/Platform.ts";
import * as Provider from "@/Provider.ts";
import type { Resource } from "@/Resource.ts";
import { inMemoryState } from "@/State/index.ts";
import * as Test from "@/Test/Alchemy.ts";
import { expect } from "alchemy-test";
import * as Config from "effect/Config";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

interface ConfigHost extends Resource<
"Test.ConfigHost",
{ env?: Record<string, unknown> },
{ ready: true },
{ env?: Record<string, unknown> }
> {}

const boundKeys: string[] = [];

const ConfigHost: any = Platform<ConfigHost>("Test.ConfigHost", {
createRuntimeContext: (id) => ({
Type: "Test.ConfigHost",
id,
env: {},
set: (key) =>
Effect.sync(() => {
boundKeys.push(key);
return key;
}),
get: () => Effect.succeed(undefined),
}),
});

const providers = Provider.succeed(ConfigHost, {
list: () => Effect.succeed([]),
diff: Effect.fn(function* () {
return undefined;
}),
reconcile: Effect.fn(function* () {
return { ready: true as const };
}),
delete: Effect.fn(function* () {}),
});

const { test } = Test.make({ providers, state: inMemoryState() });

test.provider(
"an absent optional field skips synthetic binding and preserves concrete config bindings",
(stack) =>
Effect.gen(function* () {
boundKeys.length = 0;

yield* stack.deploy(
ConfigHost(
"ConfigHost",
{},
Effect.gen(function* () {
const optional = yield* Config.schema(
Schema.Struct({ maybe: Schema.optional(Schema.String) }),
);
expect(optional).toEqual({});

const concrete = yield* Config.string(
"ALCHEMY_TEST_PRESENT_CONFIG",
);
expect(concrete).toBe("present");
return {};
}),
),
);

expect(boundKeys).toEqual(["ALCHEMY_TEST_PRESENT_CONFIG"]);
}).pipe(
Effect.provide(
ConfigProvider.layer(
ConfigProvider.fromUnknown({
ALCHEMY_TEST_PRESENT_CONFIG: "present",
}),
),
),
),
);