Comments (6)
Can you provide some sample code? The following is working as expected:
type Config struct {
Database struct {
Host string `default:"localhost"`
}
}
func main() {
var cfg Config
_ = fig.Load(&cfg, fig.IgnoreFile())
fmt.Println(cfg.Database.Host)
// localhost
}
from fig.
The code is exactly like you provided, but without "fig.IgnoreFile()". The desired behavior is to use the defaults when there is no file and to use values from the file when it exists
from fig.
The failure when a configuration file is absent is intentional, as this situation can indicate a genuine issue in applications, such as the deployment phase not copying over the configuration file correctly.
The IgnoreFile()
option exists to cater to scenarios where the absence of a configuration file isn't problematic. Combining these behaviours could lead to ambiguity and reduce flexibility.
As a possible solution, you can manage this within your application code. If fig.Load()
results in an ErrFileNotFound
error, you could create a config file and then execute Load()
once more. I acknowledge it's not great, but it'll get you where you want. If you have any ideas for addressing this within fig, while still aligning with the points I've previously mentioned, I'd be interested to hear them.
from fig.
A lot of software I know behaves exactly as I described. If there is no config, it doesn't create a new one, but just uses the default values.
I understand it could lead to misconfiguration issues for some people, that is why you have ErrFileNotFound
error. If this error is ignored that means the developer understand the risks and wants it to behave normally which includes populating the default values. Alternatively there could be a function to populate them, but it wouldn't be intuitive. When I specify default values I expect to have them if nothing else is specified.
from fig.
Default values are designated at the field level to offer a fallback for individual fields when specified. They do not and should not imply any behaviour for when the entire file is absent, as that is a higher-level concern.
Implementing the approach you suggest would necessitate addressing it within the Load()
function's scope, e.g. as an additional Option
, and then defaults at the field-level would work in either case.
from fig.
Closing due to inactivity.
from fig.
Related Issues (17)
- Value from environment HOT 3
- fig should be more customizable via hooks HOT 1
- Allow for multiple possible file names HOT 1
- add mapstructure squash support HOT 1
- Default bool items not being set correctly when not present in config HOT 2
- Fields of type time.Duration should fail when the input is an integer HOT 1
- Add a strict parsing alternative which disallows unknown fields
- Make TOML / YAML dependency optional, somehow HOT 4
- Setting config value to "zero" in file takes default value HOT 1
- validate a values of map HOT 4
- Feature request: auto create config file if not found HOT 5
- Feature request: custom type unmarshalling HOT 4
- feat: being able to add env name for a specific field HOT 6
- Add an option to explicitly set which config file to use HOT 2
- Make it possible to use only environment variables (with no config file)
- Feature request: Updating configuration files
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fig.