Skip to content

Commit

Permalink
Use github.com/pelletier/go-toml to support time.Duration (#10)
Browse files Browse the repository at this point in the history
* go: use github.com/pelletier/go-toml package

This package has native time.Duration parser and serializer.

* config: remove Duration type since new package supports out of the box

* github: add ci workflow

* github: add codeowners

* github: add gofmt check

* github: fix gofmt check
  • Loading branch information
igungor authored Sep 21, 2021
1 parent 5743e38 commit fda9b31
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 49 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @peak/big-data
64 changes: 64 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: ci
on: [push, pull_request]
jobs:
build:
strategy:
matrix:
go-version:
- 1.17.x
- 1.16.x
- 1.15.x
os:
- ubuntu

name: build (${{ matrix.os }}/go-${{ matrix.go-version }})
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- run: go build

test:
strategy:
matrix:
go-version:
- 1.17.x
- 1.16.x
- 1.15.x
os:
- ubuntu

name: test (${{ matrix.os }}/go-${{ matrix.go-version }})
runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- run: go test

qa:
strategy:
matrix:
go-version:
- 1.17.x
os:
- ubuntu

runs-on: ${{ matrix.os }}-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then echo "Go code is not formatted"; exit 1; fi
- run: go vet
- run: go get honnef.co/go/tools/cmd/staticcheck
- run: staticcheck
- run: go get mvdan.cc/unparam
- run: unparam
26 changes: 10 additions & 16 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strconv"
"strings"

"github.com/BurntSushi/toml"
"github.com/fatih/structs"
"github.com/pelletier/go-toml"
)

const (
Expand All @@ -21,16 +21,20 @@ const (

// Load loads filepath into dst. It also handles "flag" binding.
func Load(filepath string, dst interface{}) error {
metadata, err := toml.DecodeFile(filepath, dst)
tree, err := toml.LoadFile(filepath)
if err != nil {
return err
}

if err := tree.Unmarshal(dst); err != nil {
return err
}

if err := bindEnvVariables(dst); err != nil {
return err
}

return bindFlags(dst, metadata, "")
return bindFlags(dst, tree, "")
}

// bindEnvVariables will bind CLI flags to their respective elements in dst, defined by the struct-tag "env".
Expand Down Expand Up @@ -68,7 +72,7 @@ func bindEnvVariables(dst interface{}) error {
}

// bindFlags will bind CLI flags to their respective elements in dst, defined by the struct-tag "flag".
func bindFlags(dst interface{}, metadata toml.MetaData, fieldPath string) error {
func bindFlags(dst interface{}, tree *toml.Tree, fieldPath string) error {
fields := structs.Fields(dst)
for _, field := range fields {
if !field.IsExported() {
Expand All @@ -93,7 +97,7 @@ func bindFlags(dst interface{}, metadata toml.MetaData, fieldPath string) error
path += field.Name()
}

if err := bindFlags(dstElem.Addr().Interface(), metadata, path); err != nil {
if err := bindFlags(dstElem.Addr().Interface(), tree, path); err != nil {
return err
}

Expand All @@ -117,7 +121,7 @@ func bindFlags(dst interface{}, metadata toml.MetaData, fieldPath string) error
tomlKey = fmt.Sprintf("%s.%s", fieldPath, field.Tag(tomlTag))
}

if envHasKey || tomlHasKey(metadata, tomlKey) {
if envHasKey || tree.Has(strings.ToLower(tomlKey)) {
continue
} else {
useFlagDefaultValue = true
Expand Down Expand Up @@ -215,13 +219,3 @@ func isFlagSet(tag string) bool {
})
return flagSet
}

// tomlHasKey will check if the toml key presents in toml metadata
func tomlHasKey(metadata toml.MetaData, tomlKey string) bool {
for _, key := range metadata.Keys() {
if strings.EqualFold(key.String(), tomlKey) {
return true
}
}
return false
}
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,16 +744,16 @@ func TestLoad_Duration(t *testing.T) {
}

var cfg struct {
FlushInterval Duration `toml:"flush-interval"`
FlushInterval time.Duration `toml:"flush-interval"`
}

if err := Load(tmp.Name(), &cfg); err != nil {
t.Fatalf("unexpected error %v", err)
}

const expected = 12*time.Hour + 34*time.Minute + 56*time.Second
if cfg.FlushInterval.Duration != expected {
t.Errorf("got: %v, expected: %v", cfg.FlushInterval.Duration, expected)
if cfg.FlushInterval != expected {
t.Errorf("got: %v, expected: %v", cfg.FlushInterval, expected)
}
}

Expand Down
21 changes: 0 additions & 21 deletions duration.go

This file was deleted.

10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/peak/go-config

go 1.16

require (
github.com/BurntSushi/toml v0.3.0
github.com/fatih/structs v1.0.0
github.com/fsnotify/fsnotify v1.4.7
github.com/google/go-cmp v0.5.6 // indirect
golang.org/x/sys v0.0.0-20180313075820-8c0ece68c283 // indirect
github.com/google/go-cmp v0.5.6
github.com/pelletier/go-toml v1.9.4
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 // indirect
)

go 1.11
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
github.com/BurntSushi/toml v0.3.0 h1:e1/Ivsx3Z0FVTV0NSOv/aVgbUWyQuzj7DDnFblkRvsY=
github.com/BurntSushi/toml v0.3.0/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/fatih/structs v1.0.0 h1:BrX964Rv5uQ3wwS+KRUAJCBBw5PQmgJfJ6v4yly5QwU=
github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
golang.org/x/sys v0.0.0-20180313075820-8c0ece68c283 h1:DE/w7won1Ns86VoWjUZ4cJS6//TObJntGkxuZ63asRc=
golang.org/x/sys v0.0.0-20180313075820-8c0ece68c283/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 h1:7ZDGnxgHAMw7thfC5bEos0RDAccZKxioiWBhfIe+tvw=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 comments on commit fda9b31

Please sign in to comment.