Skip to content

Commit

Permalink
add Format method to decoders (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed May 22, 2021
1 parent ae11180 commit 7b7dfeb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
6 changes: 2 additions & 4 deletions aconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Config struct {

// FileDecoder is used to read config from files. See aconfig submodules.
type FileDecoder interface {
Format() string
DecodeFile(filename string) (map[string]interface{}, error)
}

Expand Down Expand Up @@ -142,9 +143,6 @@ func (l *Loader) init() {
}
l.config.FileDecoders[".json"] = &jsonDecoder{}
}
if dec, ok := l.config.FileDecoders[".yaml"]; ok {
l.config.FileDecoders[".yml"] = dec
}

if l.config.Args == nil {
l.config.Args = os.Args[1:]
Expand Down Expand Up @@ -292,7 +290,7 @@ func (l *Loader) loadFromFile() error {
return err
}

tag := getTagForExt(ext)
tag := decoder.Format()

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
Expand Up @@ -10,6 +10,11 @@ type Decoder struct{}
// New .ENV decoder for aconfig.
func New() *Decoder { return &Decoder{} }

// Format of the decoder.
func (d *Decoder) Format() string {
return "env"
}

// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
raw, err := godotenv.Read(filename)
Expand Down
5 changes: 5 additions & 0 deletions aconfighcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type Decoder struct{}
// New HCL decoder for aconfig.
func New() *Decoder { return &Decoder{} }

// Format of the decoder.
func (d *Decoder) Format() string {
return "hcl"
}

// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
b, err := ioutil.ReadFile(filename)
Expand Down
5 changes: 5 additions & 0 deletions aconfigtoml/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type Decoder struct{}
// New TOML decoder for aconfig.
func New() *Decoder { return &Decoder{} }

// Format of the decoder.
func (d *Decoder) Format() string {
return "toml"
}

// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
f, err := os.Open(filename)
Expand Down
5 changes: 5 additions & 0 deletions aconfigyaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type Decoder struct{}
// New YAML decoder for aconfig.
func New() *Decoder { return &Decoder{} }

// Format of the decoder.
func (d *Decoder) Format() string {
return "yaml"
}

// DecodeFile implements aconfig.FileDecoder.
func (d *Decoder) DecodeFile(filename string) (map[string]interface{}, error) {
f, err := os.Open(filename)
Expand Down
13 changes: 5 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ func assertStruct(x interface{}) {
}
}

func getTagForExt(ext string) string {
tag := ext[1:]
if tag == "yml" {
tag = "yaml"
}
return tag
}

func getEnv() map[string]interface{} {
env := os.Environ()
res := make(map[string]interface{}, len(env))
Expand Down Expand Up @@ -137,6 +129,11 @@ func splitNameByWords(src string) []string {

type jsonDecoder struct{}

// Format of the decoder.
func (d *jsonDecoder) Format() string {
return "json"
}

// DecodeFile implements FileDecoder.
func (d *jsonDecoder) DecodeFile(filename string) (map[string]interface{}, error) {
f, err := os.Open(filename)
Expand Down

0 comments on commit 7b7dfeb

Please sign in to comment.