From 0afa4d5b6cf143c2e1326a6fa9bfe5162e47db6f Mon Sep 17 00:00:00 2001 From: Kyriacos <58787538+kkyr@users.noreply.github.com> Date: Tue, 12 Dec 2023 22:55:56 +0100 Subject: [PATCH] Update godoc (#31) * Update godoc * Update tabs * Update content * Update readme * Typos * ANother typo --- README.md | 37 ++++++++++++++----------------------- fig.go | 49 +++++++++++++++++++++++++++---------------------- 2 files changed, 41 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 95a95d5..2cf5d11 100644 --- a/README.md +++ b/README.md @@ -12,16 +12,15 @@ # fig -fig is a tiny library for loading an application's config file and its environment into a Go struct. Individual fields can have default values defined or be marked as required. +fig is a tiny library for loading an application's configuration into a Go struct. ## Why fig? -- Define your **configuration**, **validations** and **defaults** in a single location -- Optionally **load from the environment** as well -- Only **3** external dependencies -- Full support for`time.Time`, `time.Duration` & `regexp.Regexp` -- Tiny API -- Decoders for `.yaml`, `.json` and `.toml` files +- 🛠️ Define your **configuration**, **validations** and **defaults** all within a single struct. +- 🌍 Easily load your configuration from a **file**, the **environment**, or both. +- ⏰ Decode strings into `Time`, `Duration`, `Regexp`, or any custom type that satisfies the `StringUnmarshaler` interface. +- 🗂️ Compatible with `yaml`, `json`, and `toml` file formats. +- 🧩 Only three external dependencies. ## Getting Started @@ -72,40 +71,32 @@ type Config struct { func main() { var cfg Config err := fig.Load(&cfg) - // handle your err + // error handling omitted fmt.Printf("%+v\n", cfg) - // Output: {Build:2019-12-25 00:00:00 +0000 UTC Server:{Host:127.0.0.1 Ports:[8080] Cleanup:1h0m0s} Logger:{Level:warn Pattern:.* Trace:true}} + // {Build:2019-12-25T00:00:00Z Server:{Host:127.0.0.1 Ports:[8080] Cleanup:1h0m0s} Logger:{Level:warn Pattern:.* Trace:true}} } ``` -If a field is not set and is marked as *required* then an error is returned. If a *default* value is defined instead then that value is used to populate the field. - -Fig searches for a file named `config.yaml` in the directory it is run from. Change the lookup behaviour by passing additional parameters to `Load()`: - -```go -fig.Load(&cfg, - fig.File("settings.json"), - fig.Dirs(".", "/etc/myapp", "/home/user/myapp"), -) // searches for ./settings.json, /etc/myapp/settings.json, /home/user/myapp/settings.json - -``` +Fields marked as _required_ are checked to ensure they're not empty, and _default_ values are applied to fill in those that are empty. ## Environment -Need to additionally fill fields from the environment? It's as simple as: +By default, fig will only look for values in a config file. To also include values from the environment, use the `UseEnv` option: ```go -fig.Load(&cfg, fig.UseEnv("MYAPP")) +fig.Load(&cfg, fig.UseEnv("APP_PREFIX")) ``` +In case of conflicts, values from the environment take precedence. + ## Usage See usage [examples](/examples). ## Documentation -See [go.dev](https://pkg.go.dev/github.com/kkyr/fig?tab=doc) for detailed documentation. +For detailed documentation, visit [go.dev](https://pkg.go.dev/github.com/kkyr/fig?tab=doc). ## Contributing diff --git a/fig.go b/fig.go index 0add1af..7632ae4 100644 --- a/fig.go +++ b/fig.go @@ -28,38 +28,43 @@ const ( DefaultTimeLayout = time.RFC3339 ) -// StringUnmarshaler is an interface for custom unmarshaling of strings +// StringUnmarshaler is an interface designed for custom string unmarshaling. // -// If a field with a local type asignment satisfies this interface, it allows the user -// to implment their own custom type unmarshaling method. +// This interface is used when a field of a custom type needs to define its own +// method for unmarshaling from a string. This is particularly useful for handling +// different string representations that need to be converted into a specific type. // -// Example: +// To use this, the custom type must implement this interface and a corresponding +// string value should be provided in the configuration. Fig automatically detects +// this and handles the rest. +// +// Example usage: // // type ListenerType uint // // const ( -// ListenerUnix ListenerType = iota -// ListenerTCP -// ListenerTLS +// ListenerUnix ListenerType = iota +// ListenerTCP +// ListenerTLS // ) // -// type Config struct { -// Listener ListenerType `fig:"listener_type" default:"unix"` +// func (l *ListenerType) UnmarshalType(v string) error { +// switch strings.ToLower(v) { +// case "unix": +// *l = ListenerUnix +// case "tcp": +// *l = ListenerTCP +// case "tls": +// *l = ListenerTLS +// default: +// return fmt.Errorf("unknown listener type: %s", v) +// } +// return nil // } // -// func (l *ListenerType) UnmarshalType(v string) error { -// switch strings.ToLower(v) { -// case "unix": -// *l = ListenerUnix -// case "tcp": -// *l = ListenerTCP -// case "tls": -// *l = ListenerTLS -// default: -// return fmt.Errorf("unknown listener type: %s", v) -// } -// return nil -// } +// type Config struct { +// Listener ListenerType `fig:"listener_type" default:"tcp"` +// } type StringUnmarshaler interface { UnmarshalString(s string) error }