Skip to content

Commit

Permalink
Restructure repo to minimize dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
greencoda committed Apr 23, 2024
1 parent b50b50c commit e1754fb
Show file tree
Hide file tree
Showing 55 changed files with 1,701 additions and 830 deletions.
5 changes: 2 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ linters:
- varnamelen

linters-settings:
asasalint:
ignore-test: true
cyclop:
max-complexity: 20
depguard:
Expand All @@ -26,9 +28,6 @@ linters-settings:
allow:
- $gostd
- github.com/greencoda
- github.com/hashicorp/go-envparse
- github.com/pelletier/go-toml
- github.com/davecgh/go-spew/spew
ireturn:
allow:
- anon
Expand Down
1 change: 1 addition & 0 deletions .mockery.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: "IValueContainer"
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: deps lint mocks test test-cover
.SILENT: test test-cover
.SILENT: test test-cover test-loaders

TESTABLE_PACKAGES = $(shell go list ./... | grep -v ./mocks)

Expand All @@ -10,9 +10,19 @@ deps:
lint: deps
golangci-lint run -v

mocks:
rm -rf mocks/*
mockery

test:
go test ${TESTABLE_PACKAGES} -count=1 -cover

test-cover:
go test ${TESTABLE_PACKAGES} -count=1 -cover -coverprofile=coverage.out
go tool cover -html=coverage.out

test-loaders:
(for dir in ./loaders/*/; do \
(cd $$dir;go test ./... -count=1 -cover -coverprofile=coverage.out) \
done)

8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![godoc for greencoda/confiq][godoc-badge]][godoc-url]
[![Go 1.22][goversion-badge]][goversion-url]
[![Build Status][actions-badge]][actions-url]
[![Go Coverage](https://github.com/greencoda/confiq/wiki/coverage.svg)](https://raw.githack.com/wiki/greencoda/confiq/coverage.html)
[![Go Coverage][gocoverage-badge]][gocoverage-url]
[![Go Report card][goreportcard-badge]][goreportcard-url]

<p align="center"><img src=".github/splash_image.png" width="500"></p>
Expand Down Expand Up @@ -45,7 +45,9 @@ In this example we're loading it from the file system:
``` go
configSet := confiq.New()

if err := configSet.LoadJSONFromFile("./config.json"); err != nil {
if err := configSet.Load(
confiqjson.Load().FromFile("./config.json"),
); err != nil {
log.Fatal(err)
}
```
Expand Down Expand Up @@ -169,3 +171,5 @@ func (t *customType) Decode(value any) error {
[goversion-url]: https://golang.org/doc/go1.22
[goreportcard-badge]: https://goreportcard.com/badge/github.com/greencoda/confiq
[goreportcard-url]: https://goreportcard.com/report/github.com/greencoda/confiq
[gocoverage-badge]: https://github.com/greencoda/confiq/wiki/coverage.svg
[gocoverage-url]: https://raw.githack.com/wiki/greencoda/confiq/coverage.html
7 changes: 6 additions & 1 deletion _examples/01_simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/greencoda/confiq"

confiqjson "github.com/greencoda/confiq/loaders/json"
)

// ConfigSettingAPIGroup is a struct to test the config set.
Expand Down Expand Up @@ -39,7 +41,10 @@ func main() {
configSet := confiq.New()

// Load the JSON file into the config set.
if err := configSet.LoadJSONFromFile("./config.json"); err != nil {
if err := configSet.Load(
confiqjson.Load().FromFile("./config.json"),
confiq.WithPrefix(""),
); err != nil {
log.Fatal(err)
}

Expand Down
16 changes: 10 additions & 6 deletions _examples/02_complex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/greencoda/confiq"
confiqenv "github.com/greencoda/confiq/loaders/env"
confiqjson "github.com/greencoda/confiq/loaders/json"
confiqtoml "github.com/greencoda/confiq/loaders/toml"
confiqyaml "github.com/greencoda/confiq/loaders/yaml"
)

// APISettings is a struct which holds the API settings.
Expand Down Expand Up @@ -46,22 +50,22 @@ func main() {
configSet := confiq.New()

// Load the API settings from a TOML file into the config set, with a prefix of "apiSettings".
if err := configSet.LoadTOMLFromFile("./apiSettings.toml", confiq.WithPrefix("apiSettings")); err != nil {
if err := configSet.Load(confiqtoml.Load().FromFile("./apiSettings.toml"), confiq.WithPrefix("apiSettings")); err != nil {
log.Fatal(err)
}

// Load the DB settings from a JSON file into the config set.
if err := configSet.LoadJSONFromFile("./dbSettings.json", confiq.WithPrefix("dbSettings")); err != nil {
if err := configSet.Load(confiqjson.Load().FromFile("./dbSettings.json"), confiq.WithPrefix("dbSettings")); err != nil {
log.Fatal(err)
}

// Load the DB settings from an Env file into the config set.
if err := configSet.LoadEnvFromFile("./osSettings.env", confiq.WithPrefix("osSettings")); err != nil {
// Load the OS settings from an Env file into the config set.
if err := configSet.Load(confiqenv.Load().FromFile("./osSettings.env"), confiq.WithPrefix("apiSettings")); err != nil {
log.Fatal(err)
}

// Load the DB settings from a YAML file into the config set.
if err := configSet.LoadYAMLFromFile("./userSettings.yaml", confiq.WithPrefix("userSettings")); err != nil {
// Load the User settings from a YAML file into the config set.
if err := configSet.Load(confiqyaml.Load().FromFile("./userSettings.yaml"), confiq.WithPrefix("userSettings")); err != nil {
log.Fatal(err)
}

Expand Down
6 changes: 4 additions & 2 deletions _examples/03_layered/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/greencoda/confiq"

confiqjson "github.com/greencoda/confiq/loaders/json"
)

// DBSettings is a struct which holds the database settings.
Expand All @@ -25,7 +27,7 @@ func main() {
configSet := confiq.New()

// Load the base DB settings from a JSON file into the config set, with a prefix of "dbSettings".
if err := configSet.LoadJSONFromFile("./dbSettingsBase.json", confiq.WithPrefix("dbSettings")); err != nil {
if err := configSet.Load(confiqjson.Load().FromFile("./dbSettingsBase.json"), confiq.WithPrefix("dbSettings")); err != nil {
log.Fatal(err)
}

Expand Down Expand Up @@ -63,7 +65,7 @@ func main() {
}

func loadDBConfig(configSet *confiq.ConfigSet, configFile string) {
if err := configSet.LoadYAMLFromFile(configFile, confiq.WithPrefix("dbSettings")); err != nil {
if err := configSet.Load(confiqjson.Load().FromFile(configFile), confiq.WithPrefix("dbSettings")); err != nil {
log.Fatal(err)
}
}
6 changes: 5 additions & 1 deletion _examples/04_customStruct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/greencoda/confiq"

confiqjson "github.com/greencoda/confiq/loaders/json"
)

var errValueIsNotString = errors.New("value is not a string")
Expand Down Expand Up @@ -52,7 +54,9 @@ func main() {
configSet := confiq.New()

// Load the DB settings from a JSON file into the config set.
if err := configSet.LoadJSONFromFile("./plumbus.json"); err != nil {
if err := configSet.Load(
confiqjson.Load().FromFile("./plumbus.json"),
); err != nil {
log.Fatal(err)
}

Expand Down
69 changes: 55 additions & 14 deletions commonDecoders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"time"

"github.com/greencoda/confiq"
"github.com/greencoda/confiq/mocks"
"github.com/stretchr/testify/suite"
)

type CommonDecodersTestSuite struct {
suite.Suite

configSet *confiq.ConfigSet
configSet *confiq.ConfigSet
valueContainer *mocks.IValueContainer
}

func Test_CommonDecoders(t *testing.T) {
Expand All @@ -27,12 +29,16 @@ func (s *CommonDecodersTestSuite) SetupTest() {
s.configSet = confiq.New(
confiq.WithTag("cfg"),
)
s.valueContainer = mocks.NewIValueContainer(s.T())

s.Require().NotNil(s.configSet)
}

func (s *CommonDecodersTestSuite) Test_Decode_Duration() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_duration": "15s"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -51,7 +57,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_Duration() {
}

func (s *CommonDecodersTestSuite) Test_Decode_Duration_FromInvalidFormat() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_duration_invalid_format": "fifteen seconds"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -66,7 +75,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_Duration_FromInvalidFormat() {
}

func (s *CommonDecodersTestSuite) Test_Decode_IP() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_ip": "127.0.0.1"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -85,7 +97,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_IP() {
}

func (s *CommonDecodersTestSuite) Test_Decode_IP_FromInvalidFormat() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_ip_invalid_format": "127.0.0.0.1"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -100,7 +115,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_IP_FromInvalidFormat() {
}

func (s *CommonDecodersTestSuite) Test_Decode_JSONRawMessage() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_rawMessage": map[string]any{"rawMessage": "It's raw"}}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -119,7 +137,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_JSONRawMessage() {
}

func (s *CommonDecodersTestSuite) Test_Decode_JSONRawMessage_WithUnmarshalable() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_rawMessage": map[string]any{"rawMessage": "It's raw"}}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -136,7 +157,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_JSONRawMessage_WithUnmarshalable()
}

func (s *CommonDecodersTestSuite) Test_Decode_URL() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_url": "http://www.test.com"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand Down Expand Up @@ -167,7 +191,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_URL() {
}

func (s *CommonDecodersTestSuite) Test_Decode_URL_FromInvalidFormat() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_url": "http://www.test.com"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -182,7 +209,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_URL_FromInvalidFormat() {
}

func (s *CommonDecodersTestSuite) Test_Decode_Time() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_time": "2025-01-13T16:00:00+09:00"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -201,7 +231,12 @@ func (s *CommonDecodersTestSuite) Test_Decode_Time() {
}

func (s *CommonDecodersTestSuite) Test_Decode_Time_FromTime() {
loadErr := s.configSet.LoadTOMLFromFile("./testdata/common.toml")
timeValue := time.Date(2025, 1, 13, 16, 0, 0, 0, time.FixedZone("", 9*60*60))

s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_time": timeValue}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -210,7 +245,7 @@ func (s *CommonDecodersTestSuite) Test_Decode_Time_FromTime() {

var (
target targetStruct
expected = time.Date(2025, 1, 13, 16, 0, 0, 0, time.FixedZone("", 9*60*60))
expected = timeValue
)

decodeErr := s.configSet.Decode(&target)
Expand All @@ -220,7 +255,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_Time_FromTime() {
}

func (s *CommonDecodersTestSuite) Test_Decode_Time_FromInvalidType() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_time_invalid_type": false}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand All @@ -235,7 +273,10 @@ func (s *CommonDecodersTestSuite) Test_Decode_Time_FromInvalidType() {
}

func (s *CommonDecodersTestSuite) Test_Decode_Time_FromInvalidFormat() {
loadErr := s.configSet.LoadJSONFromFile("./testdata/common.json")
s.valueContainer.On("Errors").Return([]error{})
s.valueContainer.On("Get").Return([]any{map[string]any{"test_time_invalid_format": "2025. 01. 13. 16:00:00 UTC"}})

loadErr := s.configSet.Load(s.valueContainer)
s.Require().NoError(loadErr)

type targetStruct struct {
Expand Down
Loading

0 comments on commit e1754fb

Please sign in to comment.