Skip to content

fix(read): parse env in StringMap for map[string]string values - #232

Merged
inhere merged 1 commit into
gookit:masterfrom
youdie006:stringmap-parse-env
Oct 27, 2026
Merged

inhere merged 1 commit into
gookit:masterfrom
youdie006:stringmap-parse-env

Conversation

@youdie006

Copy link
Copy Markdown
Contributor

With ParseEnv, StringMap parses env for map values decoded from files (the map[string]any and map[any]any branches), but returns a map[string]string stored by Set or LoadData as is. #230 fixed the same gap for []string in Strings.

c := config.New("demo", config.ParseEnv) // APP_HOST=db.internal
c.Set("map", map[string]string{"host": "${APP_HOST}"})
c.String("map.host")  // "db.internal"
c.StringMap("map")    // before: map[host:${APP_HOST}]   after: map[host:db.internal]

This clones and parses the map the same way the []string branch of Strings does, so the stored value is not changed; with ParseEnv off the stored map is returned as before. The new test fails without the change. go test -race ./... and go vet pass locally (Go 1.26, with NAME unset: TestIssues_114 reads ${NAME | Bob} and my shell sets NAME).

Written with AI assistance (Claude); I have reviewed the change.

StringMap parsed env for map values decoded from files, but returned a
map[string]string stored by Set or LoadData as is, so with ParseEnv
${VAR} was left unexpanded. Clone and parse it like Strings does for
[]string since gookit#230.
@inhere
inhere merged commit 4fdbc1c into gookit:master Oct 27, 2026
12 checks passed
inhere added a commit that referenced this pull request Oct 27, 2026
… paths

- document the contract on StringMap like Strings does since c0ce4bb: with
  ParseEnv a parsed copy is returned, otherwise the stored map[string]string is
  returned directly, so the caller must not change it.
- cover the two paths the #232 test left out: the read cache returns the parsed
  map on the second call, and a map[string]string value from LoadData is parsed
  too. All of them fail on the code before #232.
inhere added a commit that referenced this pull request Oct 27, 2026
Ints and IntMap handed the raw elem/value to mathutil.ToInt, so with ParseEnv a
"${PORT}" elem failed with "cannot convert to int", while the scalar Int, Uint,
Float and Bool read the env value through getString. Strings (#230) and StringMap
(#232) already parse env for their string elem/value, so this was the last of the
group.

- parse a string elem/value with ParseEnvValue before the conversion, through the
  new parseEnvValue helper; parseEnvString is a thin wrapper of it now.
- Ints accepts a []string value from Set, and IntMap a map[string]string one, which
  returned the "value cannot be convert" error before.
- with ParseEnv off nothing is parsed, the conversions are the same as before.

Tests: the four value shapes ([]any, []string, map[string]any, map[string]string)
with ParseEnv on, plus the no-env branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants