Skip to content

Commit

Permalink
Make config format a property of the Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasssadar committed May 12, 2021
1 parent 7b5914d commit fbab766
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 9 deletions.
28 changes: 23 additions & 5 deletions aconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import (
"strings"
)

type ConfigFormat string

const (
JsonFormat ConfigFormat = "json"
YamlFormat ConfigFormat = "yaml"
TomlFormat ConfigFormat = "toml"
HclFormat ConfigFormat = "hcl"
EnvFormat ConfigFormat = "env"
)

const (
defaultValueTag = "default"
usageTag = "usage"
jsonNameTag = "json"
yamlNameTag = "yaml"
tomlNameTag = "toml"
hclNameTag = "hcl"
envNameTag = "env"
jsonNameTag = string(JsonFormat)
yamlNameTag = string(YamlFormat)
tomlNameTag = string(TomlFormat)
hclNameTag = string(HclFormat)
envNameTag = string(EnvFormat)
flagNameTag = "flag"
)

Expand Down Expand Up @@ -96,6 +106,11 @@ type FileDecoder interface {
DecodeFile(filename string) (map[string]interface{}, error)
}

type FileDecoderWithFormat interface {
FileDecoder
FileFormat() ConfigFormat
}

// Field of the user configuration structure.
// Done as an interface to export less things in lib.
type Field interface {
Expand Down Expand Up @@ -287,6 +302,9 @@ func (l *Loader) loadFromFile() error {
}

tag := ext[1:]
if decFormat, ok := decoder.(FileDecoderWithFormat); ok {
tag = string(decFormat.FileFormat())
}

for _, field := range l.fields {
name := l.fullTag(field, tag)
Expand Down
5 changes: 5 additions & 0 deletions aconfigdotenv/dotenv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aconfigdotenv

import (
"github.com/avast/aconfig"
"github.com/joho/godotenv"
)

Expand All @@ -23,3 +24,7 @@ func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
}
return res, nil
}

func (d *Decoder) FileFormat() aconfig.ConfigFormat {
return aconfig.EnvFormat
}
2 changes: 1 addition & 1 deletion aconfigdotenv/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/avast/aconfig/aconfigdotenv
go 1.15

require (
github.com/avast/aconfig v0.11.4
github.com/avast/aconfig v0.14.0
github.com/joho/godotenv v1.3.0
)
2 changes: 1 addition & 1 deletion aconfighcl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/avast/aconfig/aconfighcl
go 1.15

require (
github.com/avast/aconfig v0.11.4
github.com/avast/aconfig v0.14.0
github.com/hashicorp/hcl v1.0.0
)
5 changes: 5 additions & 0 deletions aconfighcl/hcl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aconfighcl

import (
"github.com/avast/aconfig"
"io/ioutil"

"github.com/hashicorp/hcl"
Expand Down Expand Up @@ -30,3 +31,7 @@ func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
}
return raw, nil
}

func (d *Decoder) FileFormat() aconfig.ConfigFormat {
return aconfig.HclFormat
}
2 changes: 1 addition & 1 deletion aconfigtoml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.15

require (
github.com/BurntSushi/toml v0.3.1
github.com/avast/aconfig v0.11.1
github.com/avast/aconfig v0.14.0
)
5 changes: 5 additions & 0 deletions aconfigtoml/toml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aconfigtoml

import (
"github.com/avast/aconfig"
"os"

"github.com/BurntSushi/toml"
Expand All @@ -26,3 +27,7 @@ func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
}
return raw, nil
}

func (d *Decoder) FileFormat() aconfig.ConfigFormat {
return aconfig.TomlFormat
}
2 changes: 1 addition & 1 deletion aconfigyaml/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module github.com/avast/aconfig/aconfigyaml
go 1.15

require (
github.com/avast/aconfig v0.11.1
github.com/avast/aconfig v0.14.0
gopkg.in/yaml.v2 v2.3.0
)
5 changes: 5 additions & 0 deletions aconfigyaml/yaml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aconfigyaml

import (
"github.com/avast/aconfig"
"os"

"gopkg.in/yaml.v2"
Expand All @@ -26,3 +27,7 @@ func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
}
return raw, nil
}

func (d *Decoder) FileFormat() aconfig.ConfigFormat {
return aconfig.YamlFormat
}
4 changes: 4 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ func (d *jsonDecoder) DecodeFile(filename string) (map[string]interface{}, error
return raw, nil
}

func (d *jsonDecoder) FileFormat() ConfigFormat {
return JsonFormat
}

func normalize(curr interface{}) interface{} {
switch curr := curr.(type) {
case []interface{}:
Expand Down

0 comments on commit fbab766

Please sign in to comment.