Skip to content

field.path: make squash-aware#39

Merged
kkyr merged 2 commits into
kkyr:masterfrom
coxley:path-squashing
Jun 3, 2025
Merged

field.path: make squash-aware#39
kkyr merged 2 commits into
kkyr:masterfrom
coxley:path-squashing

Conversation

@coxley

@coxley coxley commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

Summary

(previous PR based from master vs. my branch)

Since fig relies on mapstructure, fields can be annotated with ",squash" to flatten the config. It's super helpful for base settings that are embedded.

However, because the environment variable resolution relies on field.path(), it doesn't match the config-file structure.

This makes field.path() omit squashed fields.

Example config that's affected:

env: prod
logging:
  level: info

Example structs:

type Base struct {
  Env string `fig:"env"`
  Logging struct {
    Level string `fig:"level"`
  } `fig:"logging"`
}

type Config struct {
  Base `fig:",squash"`
}

Overriding with env var before and after this change:

BASE_ENV=staging # before
ENV=staging      # after

Test Plan

> go test ./... -count=1
ok      github.com/kkyr/fig     0.238s
ok      github.com/kkyr/fig/examples/config     0.388s
ok      github.com/kkyr/fig/examples/custom     0.693s
ok      github.com/kkyr/fig/examples/env        0.861s
ok      github.com/kkyr/fig/examples/required   0.529s

Since fig relies on mapstructure, fields can be annotated
with `",squash"` to flatten the config. It's super helpful
for base settings that are embedded.

However, because the environment variable resolution relies
on field.path(), it doesn't match the config-file structure.

This makes field.path() omit squashed fields.

Example config that's affected:

```yml
env: prod
logging:
  level: info
```

Example structs:

```go
type Base struct {
  Env string `fig:"env"`
  Logging struct {
    Level string `fig:"level"`
  } `fig:"logging"`
}

type Config struct {
  Base `fig:",squash"`
}
```

Overriding with env var before and after this change:

```bash
BASE_ENV=staging # before
ENV=staging      # after
```

@kkyr kkyr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code change LGTM - thank you!

Can u please document in doc.go? Right after the environment section seems like a good place to add it.

@coxley

coxley commented May 5, 2025

Copy link
Copy Markdown
Contributor Author

@kkyr Done!

@coxley

coxley commented May 28, 2025

Copy link
Copy Markdown
Contributor Author

@kkyr Friendly ping here when you have time. :)

@kkyr kkyr merged commit 650a930 into kkyr:master Jun 3, 2025
0 of 3 checks passed
@kkyr

kkyr commented Jun 3, 2025

Copy link
Copy Markdown
Owner

Merged! Apologies for getting to this so late

@kkyr

kkyr commented Jun 3, 2025

Copy link
Copy Markdown
Owner

Available on v0.5.0

@coxley

coxley commented Jun 4, 2025

Copy link
Copy Markdown
Contributor Author

@kkyr All good! You're already way better at replying to PRs and issues than I am. :)

Thanks a bunch ❤️

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