Skip to content

Commit

Permalink
code clean up, LoadSettings() is renamed to Load()
Browse files Browse the repository at this point in the history
  • Loading branch information
kaatinga committed Apr 26, 2024
1 parent c595fd2 commit c98a1bd
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 203 deletions.
20 changes: 0 additions & 20 deletions .circleci/config.yml

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
/.idea/
95 changes: 0 additions & 95 deletions .golangci.yml

This file was deleted.

63 changes: 28 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,31 @@

# settings

The package looks up necessary environment variables and use them to specify settings for your application. In addition, the
package validates the final struct using standard `validate` tags.
The package looks up necessary environment variables and uses them to specify settings for your application. In addition, the package validates the final struct using standard `validate` tags.

## Contents

1. [Installation](#installation)
2. [Example](#example)
3. [Limitations](#limits)

<a name=installation></a>
1. [Installation](#1-installation)
2. [Description](#2-description)
3. [Limitations](#3-limitations)

## 1. Installation

Use go get.

go get github.com/kaatinga/settings

Then import the validator package into your own code.
Use `go get` to install the package:
```bash
go get github.com/kaatinga/settings
````

import "github.com/kaatinga/settings"

<a name=example></a>
Then, import the package into your own code:
```go
import "github.com/kaatinga/settings"
```

## 2. Description

### How to use

Create a settings model where you can use tags `env`, `default` and `validate`. Announce a variable and call `LoadSettings()`:
Create a settings model where you can use tags `env`, `default` and `validate`. Announce a variable and call `Load()`:
```go
type Settings struct {
Expand All @@ -45,7 +42,7 @@ type Settings struct {
}

var settings Settings
err := LoadSettings(&settings)
err := Load(&settings)
if err != nil {
return err
}
Expand All @@ -57,13 +54,13 @@ The `validate` tag may contain an optional validation rule fallowing the documen
### Supported types
| Type | Real type |
| ------------- | ------------- |
| string | - |
| boolean | - |
| any int | - |
| any uint | - |
| time.Duration | int64 |
| Type | Real type |
|---------------| ------------- |
| string | - |
| boolean | - |
| ~int | - |
| ~uint | - |
| time.Duration | int64 |
### Nested structs
Expand All @@ -89,8 +86,7 @@ The nested structs that added via pointer must not be necessarily initialized:
```go
var settings Model1
err := LoadSettings(&settings)
if err != nil {
if err := Load(&settings); err != nil {
return err
}
```
Expand All @@ -99,28 +95,25 @@ Nonetheless, if you want, you can do it.
```go
var settings = Model1{Model2: new(Model2)}
err := LoadSettings(&settings)
if err != nil {
if err := Load(&settings); err != nil {
return err
}
```
<a name=limits></a>

## 3. Limitations
The configuration model has some limitations in the way how it is arranged and used.
The configuration model has some limitations in how it is arranged and used.
### Empty structs are not allowed
If you add an empty struct to your configuration model, `LoadSettings()` returns error.
If you add an empty struct to your configuration model, `Load()` returns error.
### LoadSettings() accepts only pointer to your configuration model
### Load() accepts only pointer to your configuration model
The root model must be initialized and added to the `LoadSettings()` signature via pointer:
The root model must be initialized and added to the `Load()` signature via pointer:
```go
err := LoadSettings(&EnvironmentSettings)
err := Load(&EnvironmentSettings)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions env.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build amd64 || arm64

package settings

import (
Expand All @@ -10,8 +8,8 @@ import (
"time"
)

// LoadSettings loads settings to a struct.
func LoadSettings(settings interface{}) error {
// Load loads settings to a struct.
func Load(settings interface{}) error {
engine, nestedStruct := settings.(*Engine)
if !nestedStruct {
engine = newEngine(settings)
Expand All @@ -34,7 +32,7 @@ func LoadSettings(settings interface{}) error {
engine.Field.value.Kind() == reflect.Struct {
// we check whether the field is pointer or struct

err = LoadSettings(&Engine{
err = Load(&Engine{
Value: engine.Field.value,
Type: engine.Field.value.Type(),
})
Expand Down
18 changes: 9 additions & 9 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type emptySettings struct{}

type Settings struct {
SessionName string `env:"SESSION" validate:"required"`
SessionName string `env:"SESSION" validate:"required"`
PublicKeyPath string `env:"KEY_PATH" validate:"required"`
}

Expand Down Expand Up @@ -111,25 +111,25 @@ type badEnvironmentSettings4 struct {
// test structure #7
type goodEnvironmentSettings1PlusValidation struct {
Port string `env:"PORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
PathToDatabase string `env:"DB" validate:"required"`
}

// test structure #8
type badEnvironmentSettings1PlusValidation struct {
Port string `env:"BADPORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
PathToDatabase string `env:"DB" validate:"required"`
}

// test structure #9
type badEnvironmentSettings2PlusValidation struct {
Port string `env:"PORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
Port string `env:"PORT" validate:"numeric"`
PathToDatabase string `env:"DB" validate:"required"`
CacheSize byte `env:"CACHE" validate:"min=10"`
}

type simpleConfig struct {
DBURL string `default:"127.0.0.1" env:"DB_URL" validate:"required"`
Timeout time.Duration `default:"5s" env:"DB_TIMEOUT"`
DBURL string `default:"127.0.0.1" env:"DB_URL" validate:"required"`
Timeout time.Duration `default:"5s" env:"DB_TIMEOUT"`
}

func TestLoadUsingReflect(t *testing.T) {
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestLoadUsingReflect(t *testing.T) {
v := validator.New()

t.Run(tt.name, func(t *testing.T) {
err = LoadSettings(tt.settings)
err = Load(tt.settings)

if errors.Is(err, tt.wantErr) {
if err != nil {
Expand All @@ -230,7 +230,7 @@ func TestLoadUsingReflect(t *testing.T) {
}
}

t.Errorf("LoadSettings() error is incorrect\nhave %v\nwant %v", err, tt.wantErr)
t.Errorf("Load() error is incorrect\nhave %v\nwant %v", err, tt.wantErr)
})
}
}
5 changes: 3 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build ignore
//go:build ignore
// +build ignore

package main

Expand All @@ -22,7 +23,7 @@ func main() {
log.Fatalf("set env error, %s", err)
}
var sett settings
err = env.LoadSettings(&sett)
err = env.Load(&sett)
if err != nil {
log.Fatalf("load error happened, %s", err)
}
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/kaatinga/settings
go 1.18

require (
github.com/go-playground/validator/v10 v10.14.1
github.com/go-playground/validator/v10 v10.19.0
github.com/kaatinga/const-errs v1.0.0
)

require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
Loading

0 comments on commit c98a1bd

Please sign in to comment.