Skip to content

Commit 008edbc

Browse files
Single configuration file extensions
The application configuration file loader supported multiple file types with multiple file extensions, e.g. `yml` and `yaml` for YAML encoded data. This could lead to priority problems when merging multiple loaded configuration states when there are multiple YAML files with different supported file extension in the same directory. There were no rules how to decide which extension takes precedence over another extension causing unexpected merged configurations. To prevent such problems each file type now only supports a single official file extension. The currently supported encodings are JSON (1) and YAML (2) where the following file extension are only supported: 1. JSON - before: `*.json` - after: `*.json` 2. YAML - before: `*.yml, *.yaml` - after: `*.yml` Note that this won't affect the precedences of different file types! YAML files still take precedence over JSON files since YAML is a superset of JSON and JSON is also valid YAML. References: (1) https://www.json.org (2) https://yaml.org Epic GH-33 Resolves GH-67
1 parent 8531f47 commit 008edbc

File tree

2 files changed

+10
-23
lines changed

2 files changed

+10
-23
lines changed

pkg/config/constants.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,16 @@ func init() {
5353
func genConfigPaths() []*file.File {
5454
var files []*file.File
5555

56-
// Include user-level dot-file configurations from the user's home directory.
56+
// Include the user-level dotfile configuration from user's home directory.
5757
home, err := homedir.Dir()
5858
if err == nil {
59-
for _, ext := range encoder.ExtensionsJson {
60-
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, ext))))
61-
}
62-
// Since YAML is a superset of JSON, YAML files take precedence over pure JSON based configurations.
63-
for _, ext := range encoder.ExtensionsYaml {
64-
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, ext))))
65-
}
59+
files = append(files, file.NewFile(filepath.Join(home, fmt.Sprintf(".%s.%s", ProjectName, encoder.ExtensionsYaml))))
6660
}
6761

68-
// Files placed in the current working directory take precedence over user-level configurations.
62+
// A file placed in the current working directory takes precedence over the user-level configuration.
6963
pwd, err := os.Getwd()
7064
if err == nil {
71-
for _, ext := range encoder.ExtensionsJson {
72-
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, ext))))
73-
}
74-
// Since YAML is a superset of JSON, YAML files take precedence over pure JSON based configurations.
75-
for _, ext := range encoder.ExtensionsYaml {
76-
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, ext))))
77-
}
65+
files = append(files, file.NewFile(filepath.Join(pwd, fmt.Sprintf("%s.%s", ProjectName, encoder.ExtensionsYaml))))
7866
}
7967

8068
return files

pkg/config/encoder/constants.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import (
1919
var (
2020
// ExtensionMapping maps supported file extensions to their compatible encoders.
2121
ExtensionMapping = map[string]Encoder{
22-
"json": json.NewJsonEncoder(),
23-
"yaml": yaml.NewYamlEncoder(),
24-
"yml": yaml.NewYamlEncoder(),
22+
ExtensionsJson: json.NewJsonEncoder(),
23+
ExtensionsYaml: yaml.NewYamlEncoder(),
2524
}
26-
// ExtensionsJson stores all supported extensions for files containing JSON data.
27-
ExtensionsJson = []string{"json"}
28-
// ExtensionsYaml stores all supported extensions for files containing YAML data.
29-
ExtensionsYaml = []string{"yaml", "yml"}
25+
// ExtensionsJson is the supported extension for files containing JSON data.
26+
ExtensionsJson = "json"
27+
// ExtensionsYaml is the supported extension for files containing YAML data.
28+
ExtensionsYaml = "yml"
3029
)

0 commit comments

Comments
 (0)