lint: fix errors
This commit is contained in:
parent
d0c4864582
commit
081139cf65
3 changed files with 21 additions and 9 deletions
|
@ -435,10 +435,13 @@ linters:
|
||||||
- github.com/alexedwards/argon2id
|
- github.com/alexedwards/argon2id
|
||||||
- github.com/google/uuid
|
- github.com/google/uuid
|
||||||
- github.com/hack-pad/hackpadfs
|
- github.com/hack-pad/hackpadfs
|
||||||
|
- github.com/hashicorp/hcl/v2
|
||||||
- github.com/jessevdk/go-flags
|
- github.com/jessevdk/go-flags
|
||||||
|
- github.com/pelletier/go-toml/v2
|
||||||
- github.com/pressly/goose/v3
|
- github.com/pressly/goose/v3
|
||||||
- github.com/PuerkitoBio/goquery
|
- github.com/PuerkitoBio/goquery
|
||||||
- github.com/stretchr/testify
|
- github.com/stretchr/testify
|
||||||
|
- gopkg.in/ini.v1
|
||||||
- gopkg.in/yaml.v3
|
- gopkg.in/yaml.v3
|
||||||
- modernc.org/sqlite
|
- modernc.org/sqlite
|
||||||
# List of allowed module domains.
|
# List of allowed module domains.
|
||||||
|
@ -2706,7 +2709,7 @@ linters:
|
||||||
- path: _test\.go
|
- path: _test\.go
|
||||||
linters:
|
linters:
|
||||||
- revive
|
- revive
|
||||||
text: "(comments-density|line-length-limit|unchecked-type-assertion)"
|
text: "(comments-density|line-length-limit|unchecked-type-assertion|cognitive-complexity)"
|
||||||
|
|
||||||
# Run some linter only for test files by excluding its issues for everything else.
|
# Run some linter only for test files by excluding its issues for everything else.
|
||||||
#- path-except: _test\.go
|
#- path-except: _test\.go
|
||||||
|
|
23
adapters.go
23
adapters.go
|
@ -18,6 +18,8 @@ import (
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const errMsgConfigGen = "cannot generate config content: %w"
|
||||||
|
|
||||||
func parseError(err error) error {
|
func parseError(err error) error {
|
||||||
return fmt.Errorf("cannot parse config file: %w", err)
|
return fmt.Errorf("cannot parse config file: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -105,7 +107,7 @@ func unmarshalJSON(data []byte, v any) error {
|
||||||
func marshalJSON(v any) ([]byte, error) {
|
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(errMsgConfigGen, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -125,7 +127,7 @@ func unmarshalTOML(data []byte, v any) error {
|
||||||
func marshalTOML(v any) ([]byte, error) {
|
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(errMsgConfigGen, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -185,7 +187,7 @@ func unmarshalYAML(data []byte, v any) error {
|
||||||
func marshalYAML(v any) ([]byte, error) {
|
func marshalYAML(v any) ([]byte, error) {
|
||||||
data, err := yaml.Marshal(v)
|
data, err := yaml.Marshal(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
return nil, fmt.Errorf(errMsgConfigGen, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -194,6 +196,7 @@ func marshalYAML(v any) ([]byte, error) {
|
||||||
// unmarshalINI unmarshals the given data to the given struct.
|
// unmarshalINI unmarshals the given data to the given struct.
|
||||||
func unmarshalINI(data []byte, v any) error {
|
func unmarshalINI(data []byte, v any) error {
|
||||||
opts := ini.LoadOptions{}
|
opts := ini.LoadOptions{}
|
||||||
|
|
||||||
cfg, err := ini.LoadSources(opts, data)
|
cfg, err := ini.LoadSources(opts, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return parseError(err)
|
return parseError(err)
|
||||||
|
@ -220,25 +223,31 @@ func marshalINI(v any) ([]byte, error) {
|
||||||
|
|
||||||
err := ini.ReflectFromWithMapper(cfg, v, iniNameMapper)
|
err := ini.ReflectFromWithMapper(cfg, v, iniNameMapper)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot generate config content: %w", err)
|
return nil, fmt.Errorf("cannot generate config from struct: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
cfg.WriteTo(&buf)
|
|
||||||
data := buf.Bytes()
|
|
||||||
|
|
||||||
return data, nil
|
_, err = cfg.WriteTo(&buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf(errMsgConfigGen, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func iniNameMapper(raw string) string {
|
func iniNameMapper(raw string) string {
|
||||||
newstr := make([]rune, 0, len(raw))
|
newstr := make([]rune, 0, len(raw))
|
||||||
|
|
||||||
for i, chr := range raw {
|
for i, chr := range raw {
|
||||||
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
newstr = append(newstr, '_')
|
newstr = append(newstr, '_')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newstr = append(newstr, unicode.ToLower(chr))
|
newstr = append(newstr, unicode.ToLower(chr))
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(newstr)
|
return string(newstr)
|
||||||
}
|
}
|
||||||
|
|
|
@ -241,7 +241,7 @@ func testLoadFiles(t *testing.T, ext string) {
|
||||||
err := conf.LoadFiles(&c, "test_data/invalid."+ext, "test_data/valid."+ext)
|
err := conf.LoadFiles(&c, "test_data/invalid."+ext, "test_data/valid."+ext)
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
|
|
||||||
assert.Equal(t, "", c.String)
|
assert.Empty(t, c.String)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("with a valid file and invalid data", func(t *testing.T) {
|
t.Run("with a valid file and invalid data", func(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue