Skip to content

Commit

Permalink
Ensure nested configs can be loaded from env file (#4360)
Browse files Browse the repository at this point in the history
  • Loading branch information
IMax153 authored Jan 29, 2025
1 parent 7437058 commit bb05fb8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-pants-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/platform": patch
---

Ensure that nested configuration values can be properly loaded from an env file
8 changes: 6 additions & 2 deletions packages/platform/src/internal/platformConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import * as Layer from "effect/Layer"

/** @internal */
export const fromDotEnv = (
path: string
path: string,
config?: Partial<ConfigProvider.ConfigProvider.FromMapConfig>
): Effect.Effect<ConfigProvider.ConfigProvider, PlatformError, FileSystem.FileSystem> =>
Effect.gen(function*() {
const fs = yield* FileSystem.FileSystem
const content = yield* fs.readFileString(path)
const obj = parseDotEnv(content)
return ConfigProvider.fromJson(obj)
return ConfigProvider.fromMap(
new Map(Object.entries(obj)),
Object.assign({}, { pathDelim: "_", seqDelim: "," }, config)
)
})

/** @internal */
Expand Down
28 changes: 28 additions & 0 deletions packages/platform/test/ConfigProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as FileSystem from "@effect/platform/FileSystem"
import * as PlatformConfigProvider from "@effect/platform/PlatformConfigProvider"
import { assert, describe, it } from "@effect/vitest"
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"

const ConfigProviderLive = Layer.unwrapEffect(
PlatformConfigProvider.fromDotEnv(".env").pipe(
Effect.map(Layer.setConfigProvider)
)
)

describe("PlatformConfigProvider", () => {
it.effect("should properly load configuration values from an env file", () =>
Effect.gen(function*() {
const fileSystem = FileSystem.layerNoop({
readFileString: () => Effect.succeed("NESTED_CONFIG=nested_config")
})

const result = yield* Config.string("CONFIG").pipe(
Config.nested("NESTED"),
Effect.provide(Layer.provide(ConfigProviderLive, fileSystem))
)

assert.strictEqual(result, "nested_config")
}))
})

0 comments on commit bb05fb8

Please sign in to comment.