chore: update golangci-lint config
This commit is contained in:
parent
15aa3ea21c
commit
57ff8f2911
5 changed files with 1047 additions and 757 deletions
1764
.golangci.yml
1764
.golangci.yml
File diff suppressed because it is too large
Load diff
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
- Add support for TOML configuration files
|
- Add support for TOML configuration files
|
||||||
- Use stdlib for tests instead of convey
|
- Use stdlib for tests instead of convey
|
||||||
- Update golangci-lint config
|
- Update golangci-lint configuration
|
||||||
|
|
||||||
## v0.2.0 (2021-12-19)
|
## v0.2.0 (2021-12-19)
|
||||||
|
|
||||||
|
|
19
adapters.go
19
adapters.go
|
@ -16,6 +16,7 @@ const (
|
||||||
typeTOML
|
typeTOML
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// getType returns the type of the config file.
|
||||||
func getType(filename string) filetype {
|
func getType(filename string) filetype {
|
||||||
switch {
|
switch {
|
||||||
case strings.HasSuffix(filename, ".json"):
|
case strings.HasSuffix(filename, ".json"):
|
||||||
|
@ -27,7 +28,8 @@ func getType(filename string) filetype {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshal(ft filetype, data []byte, v interface{}) error {
|
// unmarshal unmarshals the given data to the given struct.
|
||||||
|
func unmarshal(ft filetype, data []byte, v any) error {
|
||||||
switch ft {
|
switch ft {
|
||||||
case typeJSON:
|
case typeJSON:
|
||||||
return unmarshalJSON(data, v)
|
return unmarshalJSON(data, v)
|
||||||
|
@ -38,7 +40,8 @@ func unmarshal(ft filetype, data []byte, v interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshal(ft filetype, v interface{}) ([]byte, error) {
|
// marshal marshals the given struct to bytes.
|
||||||
|
func marshal(ft filetype, v any) ([]byte, error) {
|
||||||
switch ft {
|
switch ft {
|
||||||
case typeJSON:
|
case typeJSON:
|
||||||
return marshalJSON(v)
|
return marshalJSON(v)
|
||||||
|
@ -49,7 +52,8 @@ func marshal(ft filetype, v interface{}) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalJSON(data []byte, v interface{}) error {
|
// unmarshalJSON unmarshals the given data to the given struct.
|
||||||
|
func unmarshalJSON(data []byte, v any) error {
|
||||||
err := json.Unmarshal(data, v)
|
err := json.Unmarshal(data, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot parse config file: %w", err)
|
return fmt.Errorf("cannot parse config file: %w", err)
|
||||||
|
@ -58,7 +62,8 @@ func unmarshalJSON(data []byte, v interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalJSON(v interface{}) ([]byte, error) {
|
// marshalJSON marshals the given struct to bytes.
|
||||||
|
func marshalJSON(v any) ([]byte, error) {
|
||||||
data, err := json.MarshalIndent(v, "", " ")
|
data, err := json.MarshalIndent(v, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
||||||
|
@ -67,7 +72,8 @@ func marshalJSON(v interface{}) ([]byte, error) {
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func unmarshalTOML(data []byte, v interface{}) error {
|
// unmarshalTOML unmarshals the given data to the given struct.
|
||||||
|
func unmarshalTOML(data []byte, v any) error {
|
||||||
err := toml.Unmarshal(data, v)
|
err := toml.Unmarshal(data, v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot parse config file: %w", err)
|
return fmt.Errorf("cannot parse config file: %w", err)
|
||||||
|
@ -76,7 +82,8 @@ func unmarshalTOML(data []byte, v interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func marshalTOML(v interface{}) ([]byte, error) {
|
// marshalTOML marshals the given struct to bytes.
|
||||||
|
func marshalTOML(v any) ([]byte, error) {
|
||||||
data, err := toml.Marshal(v)
|
data, err := toml.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
||||||
|
|
17
config.go
17
config.go
|
@ -22,7 +22,7 @@ var ErrUnsupportedFileType = errors.New("unsupported config type")
|
||||||
|
|
||||||
// LoadFile reads the file at path, parses its json content and fills the struct
|
// LoadFile reads the file at path, parses its json content and fills the struct
|
||||||
// with the content of the file.
|
// with the content of the file.
|
||||||
func LoadFile(path string, data interface{}) error {
|
func LoadFile(path string, data any) error {
|
||||||
return read(path, data)
|
return read(path, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ func LoadFile(path string, data interface{}) error {
|
||||||
//
|
//
|
||||||
// It returns an error only if the content of a file is invalid, i.e. it
|
// It returns an error only if the content of a file is invalid, i.e. it
|
||||||
// cannot be unmarshaled to the struct.
|
// cannot be unmarshaled to the struct.
|
||||||
func LoadFiles(data interface{}, paths ...string) error {
|
func LoadFiles(data any, paths ...string) error {
|
||||||
for _, p := range paths {
|
for _, p := range paths {
|
||||||
err := read(p, data)
|
err := read(p, data)
|
||||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
|
@ -44,21 +44,21 @@ func LoadFiles(data interface{}, paths ...string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveFile writes the given data serialized in JSON in the given path.
|
// SaveFile writes the given data serialized in JSON in the given path.
|
||||||
func SaveFile(path string, data interface{}) error {
|
func SaveFile(path string, data any) error {
|
||||||
return write(path, data)
|
return write(path, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadAndUpdateFile reads the config fileat path and
|
// LoadAndUpdateFile reads the config fileat path and
|
||||||
// updates it, meaning that it adds new options, removes
|
// updates it, meaning that it adds new options, removes
|
||||||
// old ones, and update it by calling the Update method of
|
// old ones, and update it by calling the Update method of
|
||||||
// data if it implements the interface Updater.
|
// data if it implements the interface [Updater].
|
||||||
//
|
//
|
||||||
// If no file is found at path, it is created and
|
// If no file is found at path, it is created and
|
||||||
// initialized with the default values.
|
// initialized with the default values.
|
||||||
//
|
//
|
||||||
// An error is returned only if the config file cannot be
|
// An error is returned only if the config file cannot be
|
||||||
// written.
|
// written.
|
||||||
func LoadAndUpdateFile(path string, data interface{}) error {
|
func LoadAndUpdateFile(path string, data any) error {
|
||||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||||
err2 := read(path, data)
|
err2 := read(path, data)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
|
@ -75,14 +75,15 @@ func LoadAndUpdateFile(path string, data interface{}) error {
|
||||||
|
|
||||||
// Updater is the interface that can be implemented by
|
// Updater is the interface that can be implemented by
|
||||||
// config structs. If it is implemented, Update() is
|
// config structs. If it is implemented, Update() is
|
||||||
// called by LoadAndUpdateFile(). It allows one to modify
|
// called by [LoadAndUpdateFile]. It allows one to modify
|
||||||
// the data and persist those changes, for example to
|
// the data and persist those changes, for example to
|
||||||
// change default values.
|
// change default values.
|
||||||
type Updater interface {
|
type Updater interface {
|
||||||
|
// Update is called by LoadAndUpdateFile
|
||||||
Update()
|
Update()
|
||||||
}
|
}
|
||||||
|
|
||||||
func read(path string, data interface{}) error {
|
func read(path string, data any) error {
|
||||||
content, err := os.ReadFile(filepath.Clean(path))
|
content, err := os.ReadFile(filepath.Clean(path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot read config file: %w", err)
|
return fmt.Errorf("cannot read config file: %w", err)
|
||||||
|
@ -91,7 +92,7 @@ func read(path string, data interface{}) error {
|
||||||
return unmarshal(getType(path), content, data)
|
return unmarshal(getType(path), content, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func write(path string, data interface{}) error {
|
func write(path string, data any) error {
|
||||||
content, err := marshal(getType(path), data)
|
content, err := marshal(getType(path), data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"code.bcarlin.xyz/go/conf"
|
"code.bcarlin.net/go/conf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJSONFiles(t *testing.T) {
|
func TestJSONFiles(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue