Skip to content

Conversation

@coxley
Copy link
Contributor

@coxley coxley commented Apr 29, 2025

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
```
Copy link
Owner

@kkyr kkyr left a comment

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
Copy link
Contributor Author

coxley commented May 5, 2025

@kkyr Done!

@coxley
Copy link
Contributor Author

coxley commented May 28, 2025

@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
Copy link
Owner

kkyr commented Jun 3, 2025

Merged! Apologies for getting to this so late

@kkyr
Copy link
Owner

kkyr commented Jun 3, 2025

Available on v0.5.0

@coxley
Copy link
Contributor Author

coxley commented Jun 4, 2025

@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