Skip to content

Commit

Permalink
release v1.0;
Browse files Browse the repository at this point in the history
  • Loading branch information
Jagerente committed Nov 18, 2023
0 parents commit 3c4b4c9
Show file tree
Hide file tree
Showing 13 changed files with 1,164 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.21.x'
- name: Install dependencies
run: go get .
- name: Test with the Go CLI
run: go test -coverprofile=coverage.txt -covermode=atomic -v ./...
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Jagerente

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
266 changes: 266 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
[![CI](https://github.com/Jagerente/gocfg/actions/workflows/ci.yml/badge.svg)](https://github.com/Jagerente/gocfg/actions/workflows/ci.yml)
[![CodeQL](https://github.com/Jagerente/gocfg/workflows/CodeQL/badge.svg)](https://github.com/Jagerente/gocfg/actions?query=workflow%3ACodeQL)
[![Go Report Card](https://goreportcard.com/badge/github.com/Jagerente/gocfg)](https://goreportcard.com/report/github.com/Jagerente/gocfg)
[![codecov](https://codecov.io/gh/Jagerente/gocfg/graph/badge.svg?token=7M88UL4ZG4)](https://codecov.io/gh/Jagerente/gocfg)
[![Go Reference](https://pkg.go.dev/badge/github.com/Jagerente/gocfg.svg)](https://pkg.go.dev/github.com/Jagerente/gocfg)

## GoCfg

## Key Features

- Unmarshal from **Environment Variables**, **.env** and any other sources right to your structs.
- Set default values for each field using tags.
- Easy to inject as much custom parsers as you need.
- Easy to inject your own values providers as much as you need and use them all at once with priority.

## Quick start

### Install package:

```bash
go get -u github.com/Jagerente/gocfg
```

### Basic usage:

It will use environment variables and default values defined in tags.

```go
package main

import (
"github.com/Jagerente/gocfg"
"github.com/Jagerente/gocfg/pkg/parsers"
"github.com/Jagerente/gocfg/pkg/values"
"time"
)

type LoggerConfig struct {
LogLevel string `env:"LOG_LEVEL" default:"debug"`
}

type RedisConfig struct {
RedisHost string `env:"REDIS_HOST" default:"localhost"`
RedisPort uint16 `env:"REDIS_PORT" default:"6379"`
RedisUser string `env:"REDIS_USER,omitempty"`
RedisPassword string `env:"REDIS_PASS"`
RedisDatabase string `env:"REDIS_DATABASE"`
}

type AppConfig struct {
// Supported Tags:
// - env: Specifies the environment variable name.
// - default: Specifies the default value for the field.
// - omitempty: Allows empty fields. FOR STRINGS ONLY!

LogLevel LoggerConfig
RedisConfig RedisConfig
BoolField bool `env:"BOOL_FIELD"`
StringField string `env:"STRING_FIELD"`
IntField int `env:"INT_FIELD"`
Int8Field int8 `env:"INT8_FIELD"`
Int16Field int16 `env:"INT16_FIELD"`
Int32Field int32 `env:"INT32_FIELD"`
Int64Field int64 `env:"INT64_FIELD"`
UintField uint `env:"UINT_FIELD"`
Uint8Field uint8 `env:"UINT8_FIELD"`
Uint16Field uint16 `env:"UINT16_FIELD"`
Uint32Field uint32 `env:"UINT32_FIELD"`
Uint64Field uint64 `env:"UINT64_FIELD"`
Float32Field float32 `env:"FLOAT32_FIELD"`
Float64Field float64 `env:"FLOAT64_FIELD"`
TimeDurationField time.Duration `env:"TIME_DURATION_FIELD"`
EmptyField string `env:"EMPTY_FIELD,omitempty"`
WithDefaultField string `env:"WITH_DEFAULT_FIELD" default:"ave"`
}

func main() {
cfg := gocfg.NewDefault()

// Equals to
cfg = gocfg.NewEmpty().
UseDefaults().
AddParserProviders(parsers.NewDefaultParserProvider()).
AddValueProviders(values.NewEnvProvider())

appConfig := new(AppConfig)
if err := cfg.Unmarshal(appConfig); err != nil {
panic(err)
}
}

```

### Default Type Parsers

> The following types are supported by default parsers:
- time.Duration
- bool
- string
- int, int8, int16, int32, int64
- uint, uint8, uint16, uint32, uint64
- float32, float64

### .env file

```go
package main

import (
"github.com/Jagerente/gocfg"
"github.com/Jagerente/gocfg/pkg/parsers"
"github.com/Jagerente/gocfg/pkg/values"
)

type AppConfig struct {
BoolField bool `env:"BOOL_FIELD"`
StringField string `env:"STRING_FIELD"`
IntField int `env:"INT_FIELD"`
}

func main() {
// With default '.env' file
dotEnvProvider, _ := values.NewDotEnvProvider()

// With custom env file path
dotEnvProvider, _ = values.NewDotEnvProvider("local.env")

// With multiple env files
dotEnvProvider, _ = values.NewDotEnvProvider("local.env", "dev.env")

cfg := gocfg.NewDefault().
AddValueProviders(dotEnvProvider)

// Equals to
cfg = gocfg.NewEmpty().
UseDefaults().
AddParserProviders(parsers.NewDefaultParserProvider()).
AddValueProviders(
values.NewEnvProvider(),
dotEnvProvider,
)

appConfig := new(AppConfig)
if err := cfg.Unmarshal(appConfig); err != nil {
panic(err)
}
}
```

### Custom key tag

```go
package main

import (
"github.com/Jagerente/gocfg"
)

type AppConfig struct {
BoolField bool `mapstructure:"BOOL_FIELD"`
StringField string `mapstructure:"STRING_FIELD"`
IntField int `mapstructure:"INT_FIELD"`
}

func main() {
cfg := gocfg.NewDefault().
UseCustomKeyTag("mapstructure")

appConfig := new(AppConfig)
if err := cfg.Unmarshal(appConfig); err != nil {
panic(err)
}
}

```

### Custom parser provider

```go
package main

import (
"github.com/Jagerente/gocfg"
"reflect"
"time"
)

type CustomParserProvider struct {
}

func NewCustomParserProvider() *CustomParserProvider {
return &CustomParserProvider{}
}

func (p *CustomParserProvider) Get(field reflect.Value) (func(v string) (any, error), bool) {
switch field.Type() {
case reflect.TypeOf(time.Duration(83)):
return func(v string) (any, error) {
return time.ParseDuration(v)
}, true
default:
return nil, false
}
}

type AppConfig struct {
BoolField bool `env:"BOOL_FIELD"`
StringField string `env:"STRING_FIELD"`
IntField int `env:"INT_FIELD"`
}

func main() {
customParserProvider := NewCustomParserProvider()

cfg := gocfg.NewDefault().
AddParserProviders(customParserProvider)

appConfig := new(AppConfig)
if err := cfg.Unmarshal(appConfig); err != nil {
panic(err)
}
}

```

### Custom value provider

```go
package main

import (
"github.com/Jagerente/gocfg"
"os"
)

type CustomValueProvider struct {
}

func NewCustomValueProvider() *CustomValueProvider {
return &CustomValueProvider{}
}

func (p *CustomValueProvider) Get(key string) string {
return os.Getenv("CUSTOM_" + key)
}

type AppConfig struct {
BoolField bool `env:"BOOL_FIELD"`
StringField string `env:"STRING_FIELD"`
IntField int `env:"INT_FIELD"`
}

func main() {
customValueProvider := NewCustomValueProvider()

cfg := gocfg.NewDefault().
AddValueProviders(customValueProvider)

appConfig := new(AppConfig)
if err := cfg.Unmarshal(appConfig); err != nil {
panic(err)
}
}

```
Loading

0 comments on commit 3c4b4c9

Please sign in to comment.