Skip to content

Commit 2b711f6

Browse files
feat(ConfigFileType): makes naming consistent and simplifies implementation due to functionality provided by ConfigType.
1 parent b5fbc38 commit 2b711f6

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

file_provider.go renamed to config_file_type.go

+16-22
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ import (
55
"path/filepath"
66
)
77

8-
// NewFileProvider provides most functionality required to support a new file
8+
// NewConfigFileType provides most functionality required to support a new file
99
// type. If manual unmarshaling is required, the Unmarshaler can be provided.
10-
func NewFileProvider(config ConfigImplementer, fileExtensions []string, unmarshaler Unmarshaler) FileProvider {
11-
return FileProvider{
10+
func NewConfigFileType(
11+
config ConfigImplementer,
12+
fileExtensions []string,
13+
unmarshaler Unmarshaler,
14+
) ConfigFileType {
15+
return ConfigFileType{
1216
unmarshaler: unmarshaler,
1317
Extensions: fileExtensions,
1418
ConfigType: ConfigType{
15-
Data: config,
19+
Config: config,
1620
},
1721
}
1822
}
1923

20-
// Unmarshaler is a function that unmarshals a byte slice into a given interface.
24+
// Unmarshaler unmarshals a byte slice into the given interface.
2125
type Unmarshaler func(data []byte, v interface{}) error
2226

23-
// FileProvider provides a ConfigImplementer that reads a file from disk and
24-
// unmarshals it into the Data field. Unmarshaling expects implementations to
27+
// ConfigFileType provides a ConfigImplementer that reads a file from disk and
28+
// unmarshals it into the Config field. Unmarshaling expects implementations to
2529
// match the standard library interface for Unmarshal.
26-
type FileProvider struct {
30+
type ConfigFileType struct {
2731
// unmarshaler is a function that unmarshals a byte slice into a given
2832
// interface.
2933
unmarshaler Unmarshaler
@@ -37,7 +41,7 @@ type FileProvider struct {
3741
}
3842

3943
// Stat checks if the file exists and computes the platform specific Path.
40-
func (f *FileProvider) Stat(cfg *Config, dirPath string) bool {
44+
func (f *ConfigFileType) Stat(cfg *Config, dirPath string) bool {
4145
for _, ext := range f.Extensions {
4246
cfgFilePath := dirPath + string(filepath.Separator) + cfg.FileName + "." + ext
4347
if _, err := os.Stat(cfgFilePath); err == nil {
@@ -49,22 +53,12 @@ func (f *FileProvider) Stat(cfg *Config, dirPath string) bool {
4953
}
5054

5155
// Parse reads the file based on the generated path computed from Stat and
52-
// unmarshals it into the Data field.
53-
func (f *FileProvider) Parse(_ *Config) error {
56+
// unmarshals it into the Config field.
57+
func (f *ConfigFileType) Parse(_ *Config) error {
5458
file, err := os.ReadFile(f.Path)
5559
if err != nil {
5660
return err
5761
}
5862

59-
return f.unmarshaler(file, f.Data)
60-
}
61-
62-
// Validate calls the Data's validation function.
63-
func (f *FileProvider) Validate() error {
64-
return f.Data.Validate()
65-
}
66-
67-
// Merge calls the Data's merge function.
68-
func (f *FileProvider) Merge(t any) any {
69-
return f.Data.Merge(t)
63+
return f.unmarshaler(file, f.Config)
7064
}

0 commit comments

Comments
 (0)