Giter Club home page Giter Club logo

Comments (6)

kkyr avatar kkyr commented on June 6, 2024

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.

comendantmc avatar comendantmc commented on June 6, 2024

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.

kkyr avatar kkyr commented on June 6, 2024

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.

comendantmc avatar comendantmc commented on June 6, 2024

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.

kkyr avatar kkyr commented on June 6, 2024

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.

kkyr avatar kkyr commented on June 6, 2024

Closing due to inactivity.

from fig.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.