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

Add proposal for upper case mapping #202

Closed
wants to merge 4 commits into from

Conversation

patroza
Copy link
Member

@patroza patroza commented Jan 18, 2023

Motivation

Currently there is no conversion done (except nesting . -> _), and the rest of the format for Flat or Map is the same as for environment variables.
Which seems awkward to me.

Now if I define a Config, and want to read from Env, I am kind of forced to use ugly upper-case key names in my Config, making the Config specialised for Environment variables.
Or I must break with convention and live with camelCase key names to fit with Flat or Map providers.

Proposal

Auto convert from/to Environment variable convention.

The convention for environment variables classically is UPPERCASE,
word separation is clasically with _ so: HOST_PORT.
Nesting is currently also done with _, so I would instead propose __ to avoid reasonable colissions.

key - current - suggestion

  • host - host -> HOST
  • hostPort - hostPort -> HOST_PORT
  • hostPort.port - hostPort_port -> HOST_PORT__PORT

TODO

Auto replace any unsupported symbol with __?
hubla@abc% -> HUBLA__ABC__

sample of invalid:

> export ABC.ABC=1
export: not valid in this context: ABC.ABC
> export "ABC.ABC"=1
export: not valid in this context: ABC.ABC
> export ABC\.ABC=1
export: not valid in this context: ABC.ABC

This might however prove challenging because apparently there's also a return conversion :)

Alternative

Provide the ability to provide a transformer to a ConfigProvider, so that one may override it with their own convention.
Extending

  export interface FromEnvConfig {
    readonly pathDelim: string
    readonly seqDelim: string
    readonly conversion: (key: string) => string
    readonly reverseConversion: (key: string) => string
  }

Base automatically changed from feat/config-enhancements to main January 18, 2023 12:35
@patroza patroza force-pushed the feat/config-env-variable-convention branch from 96dc04e to 5117d00 Compare January 18, 2023 13:22
@patroza
Copy link
Member Author

patroza commented Jan 19, 2023

Reported upstream at zio/zio#7727

@patroza patroza force-pushed the feat/config-env-variable-convention branch from 41a9540 to f0352d7 Compare January 20, 2023 13:12
@datner
Copy link
Member

datner commented Jan 24, 2023

Is adding dependencies for this really necessary? This is not something hard to implement without increasing the bundle size. Some regex magic can absolutely do this. Maybe something like /([A-Z]?[a-zA-Z0-9]+)/g you can then iterate over then something like

key.replace('.','_').replace(regexFromBefore, (m, ...args) => {
  const hasNamedGroups = typeof args.at(-1) === "object"
  const offset = hasNamedGroups ? args.at(-3) : args.at(-2)
  const spacer = offset === 0 ? '' : '_'
  return m + spacer
}) 

This can be changed to something more robust. Either way, no deps needed

@patroza
Copy link
Member Author

patroza commented Jan 24, 2023

@datner this isn't adding dependencies.
also the change will be a different one: zio/zio#7731

@patroza patroza closed this Jan 27, 2023
@mikearnaldi mikearnaldi deleted the feat/config-env-variable-convention branch March 3, 2023 13:44
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants