-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy up loader code a little and make more generic
We weren't using some of this anyway and we could easily just load any file into an object - so lets use it for catalog as well as the config file.
- Loading branch information
Showing
5 changed files
with
15 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,24 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type Loader interface { | ||
Load(context.Context) (*Config, error) | ||
} | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type LoaderFunc func(context.Context) (*Config, error) | ||
func LoadAndParse[T any](path string, obj T) (*T, error) { | ||
b, err := os.ReadFile(path) | ||
if err != nil { | ||
return &obj, errors.Wrapf(err, "unable to read file at path %v", path) | ||
} | ||
|
||
func (l LoaderFunc) Load(ctx context.Context) (*Config, error) { | ||
return l(ctx) | ||
return ParseContents(b, obj) | ||
} | ||
|
||
// FileLoader loads config from a filepath | ||
type FileLoader string | ||
|
||
func (l FileLoader) Load(context.Context) (*Config, error) { | ||
data, err := os.ReadFile(string(l)) | ||
if err != nil { | ||
return nil, err | ||
func ParseContents[T any](content []byte, obj T) (*T, error) { | ||
if err := json.Unmarshal(content, &obj); err != nil { | ||
return &obj, err | ||
} | ||
|
||
return Parse(string(l), data) | ||
return &obj, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters