Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jieggii/lookupcfg
:pencil2: Easily define and populate your configs from any kind of source using lookup function!
https://github.com/jieggii/lookupcfg
config-parser
Last synced: 13 days ago
JSON representation
:pencil2: Easily define and populate your configs from any kind of source using lookup function!
- Host: GitHub
- URL: https://github.com/jieggii/lookupcfg
- Owner: jieggii
- License: mit
- Created: 2022-12-08T08:46:05.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-07-28T17:58:47.000Z (over 1 year ago)
- Last Synced: 2024-10-12T01:09:27.756Z (about 1 month ago)
- Topics: config-parser
- Language: Go
- Homepage:
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# lookupcfg
Easily define and populate your configs from any kind of source using lookup function!## What is lookup function?
***Lookup function*** is a function which accepts only one argument: **key** (of type `string`) and returns two values:
* **value** (of type string `string`) - value which matches with the provided **key**
* **found** (of type `bool`) - is equal to `true` if provided **key** exists and **value** was found. Otherwise, it is equal to `false`Signature of lookup functions (in Go):
```go
func(key string) (value string, found bool)
```## Installation
```shell
go get github.com/jieggii/lookupcfg
```## Usage
It's so simple!First of all, define your config struct and create config instance:
```go
package mainimport "github.com/jieggii/lookupcfg"
func main() {
type Config struct {
Host string `env:"HOST"` // use tags to define source name (key)
// and value name in this source (value)
Port int `env:"PORT"`
}
config := Config{}
}
```Then choose lookup function which will be used to fill fields of the config struct.
In this example we will define own lookup function, which simply uses `os.LookupEnv` function.
```go
package mainimport (
"github.com/jieggii/lookupcfg"
"os"
)func lookup(key string) (value string, found bool) {
return os.LookupEnv(key)
}func main() {
type Config struct {
Host string `env:"HOST"` // use tags to define source name (key)
// and value name in this source (value)
Port int `env:"PORT"`
}
config := Config{}
}
```Let's finally fill our config with some values from environment variables and print the result!
```go
package mainimport (
"fmt"
"github.com/jieggii/lookupcfg"
"os"
)func lookup(key string) (value string, found bool) {
return os.LookupEnv(key)
}func main() {
type Config struct {
Host string `env:"HOST"` // use tags to define source name (key)
// and value name in this source (value)
Port int `env:"PORT"`
}
config := Config{}
result := lookupcfg.PopulateConfig(
"env", // name of the source defined in struct's field tags
lookup, // our lookup function
&config, // pointer to our config instance
) // populating our config instance with values from environmental variablesfmt.Printf("Population result: %+v\n", result)
// >>> Population result: &{MissingFields:[] IncorrectTypeFields:[]}// print our populated config instance
fmt.Printf("My config: %+v\n", config)
// >>> My config: {Host:localhost Port:8888}
}
```
Done!More examples can be found in the [examples](https://github.com/jieggii/lookupcfg/tree/master/examples) directory (´。• ◡ •。`) ♡.