Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Implement suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed Jan 18, 2023
1 parent 475eb49 commit 96dc04e
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"@vitejs/plugin-react": "^3.0.1",
"@vitest/coverage-c8": "^0.27.0",
"babel-plugin-annotate-pure-calls": "^0.4.0",
"change-case": "^4.1.2",
"concurrently": "^7.6.0",
"cpx": "^1.5.0",
"docs-ts": "0.6.10",
Expand Down
117 changes: 117 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Config/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ export declare namespace ConfigProvider {
export interface FromEnvConfig {
readonly pathDelim: string
readonly seqDelim: string
readonly conversion: (k: string) => string
readonly reverseConversion: (k: string) => string
}
}

Expand Down
16 changes: 13 additions & 3 deletions src/internal/configProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,19 @@ export const fromFlat = (flat: ConfigProvider.ConfigProvider.Flat): ConfigProvid
export const fromEnv = (
config: Partial<ConfigProvider.ConfigProvider.FromEnvConfig> = {}
): ConfigProvider.ConfigProvider => {
const { pathDelim, seqDelim } = Object.assign({}, { pathDelim: "_", seqDelim: "," }, config)
const makePathString = (path: Chunk.Chunk<string>): string => pipe(path, Chunk.join(pathDelim))
const unmakePathString = (pathString: string): ReadonlyArray<string> => pathString.split(pathDelim)
const { conversion, pathDelim, reverseConversion, seqDelim } = Object.assign(
{},
{
pathDelim: "_",
seqDelim: ",",
conversion: (k) => k,
reverseConversion: (k) => k
} as ConfigProvider.ConfigProvider.FromEnvConfig,
config
)
const makePathString = (path: Chunk.Chunk<string>): string => pipe(path, Chunk.map(conversion), Chunk.join(pathDelim))
const unmakePathString = (pathString: string): ReadonlyArray<string> =>
pathString.split(pathDelim).map(reverseConversion)

const getEnv = () =>
typeof process !== "undefined" && "env" in process && typeof process.env === "object" ? process.env : {}
Expand Down
13 changes: 10 additions & 3 deletions test/ConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import * as HashMap from "@fp-ts/data/HashMap"
import * as HashSet from "@fp-ts/data/HashSet"
import { assert, describe } from "vitest"

// TODO: Why?
// @ts-expect-error
import { camelCase, constantCase } from "change-case"

interface HostPort {
readonly host: string
readonly port: number
Expand Down Expand Up @@ -324,13 +328,16 @@ describe.concurrent("ConfigProvider", () => {
}))
})

const makeEnvProvider = () =>
ConfigProvider.fromEnv({ pathDelim: "__", conversion: constantCase, reverseConversion: camelCase, seqDelim: "," })

describe.concurrent("EnvProvider", () => {
it.effect("capitalisation", () => {
return Effect.gen(function*($) {
// current in comments
process.env["HOST" /* "host" */] = "localhost"
process.env["PORT" /* "port" */] = "8080"
const provider = ConfigProvider.fromEnv()
const provider = makeEnvProvider()
const result = yield* $(provider.load(hostPortConfig))
assert.deepStrictEqual(result, {
host: "localhost",
Expand All @@ -345,7 +352,7 @@ describe.concurrent("EnvProvider", () => {
process.env["HOST_PORT__HOST" /* "hostPort_host" */] = "localhost"
process.env["HOST_PORT__PORT" /* "hostPort_port" */] = "8080"
process.env["TIMEOUT" /* "timeout" */] = "1000"
const provider = ConfigProvider.fromEnv()
const provider = makeEnvProvider()
const result = yield* $(provider.load(serviceConfigConfig))
assert.deepStrictEqual(result, {
hostPort: {
Expand All @@ -357,7 +364,7 @@ describe.concurrent("EnvProvider", () => {
})
})

it.effect("current - should fail", () => {
it.effect("default", () => {
return Effect.gen(function*($) {
process.env["hostPort_host"] = "localhost"
process.env["hostPort_port"] = "8080"
Expand Down

0 comments on commit 96dc04e

Please sign in to comment.