lint: fix errors
Some checks failed
/ linting (push) Failing after 1m13s
/ tests (push) Successful in 1m10s

This commit is contained in:
Bruno Carlin 2025-06-09 17:01:42 +02:00
parent d0c4864582
commit 081139cf65
3 changed files with 21 additions and 9 deletions

View file

@ -435,10 +435,13 @@ linters:
- github.com/alexedwards/argon2id
- github.com/google/uuid
- github.com/hack-pad/hackpadfs
- github.com/hashicorp/hcl/v2
- github.com/jessevdk/go-flags
- github.com/pelletier/go-toml/v2
- github.com/pressly/goose/v3
- github.com/PuerkitoBio/goquery
- github.com/stretchr/testify
- gopkg.in/ini.v1
- gopkg.in/yaml.v3
- modernc.org/sqlite
# List of allowed module domains.
@ -2706,7 +2709,7 @@ linters:
- path: _test\.go
linters:
- 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.
#- path-except: _test\.go

View file

@ -18,6 +18,8 @@ import (
"gopkg.in/yaml.v3"
)
const errMsgConfigGen = "cannot generate config content: %w"
func parseError(err error) error {
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) {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return nil, fmt.Errorf("cannot generate config content: %w", err)
return nil, fmt.Errorf(errMsgConfigGen, err)
}
return data, nil
@ -125,7 +127,7 @@ func unmarshalTOML(data []byte, v any) error {
func marshalTOML(v any) ([]byte, error) {
data, err := toml.Marshal(v)
if err != nil {
return nil, fmt.Errorf("cannot generate config content: %w", err)
return nil, fmt.Errorf(errMsgConfigGen, err)
}
return data, nil
@ -185,7 +187,7 @@ func unmarshalYAML(data []byte, v any) error {
func marshalYAML(v any) ([]byte, error) {
data, err := yaml.Marshal(v)
if err != nil {
return nil, fmt.Errorf("cannot generate config content: %w", err)
return nil, fmt.Errorf(errMsgConfigGen, err)
}
return data, nil
@ -194,6 +196,7 @@ func marshalYAML(v any) ([]byte, error) {
// unmarshalINI unmarshals the given data to the given struct.
func unmarshalINI(data []byte, v any) error {
opts := ini.LoadOptions{}
cfg, err := ini.LoadSources(opts, data)
if err != nil {
return parseError(err)
@ -220,25 +223,31 @@ func marshalINI(v any) ([]byte, error) {
err := ini.ReflectFromWithMapper(cfg, v, iniNameMapper)
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
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 {
newstr := make([]rune, 0, len(raw))
for i, chr := range raw {
if isUpper := 'A' <= chr && chr <= 'Z'; isUpper {
if i > 0 {
newstr = append(newstr, '_')
}
}
newstr = append(newstr, unicode.ToLower(chr))
}
return string(newstr)
}

View file

@ -241,7 +241,7 @@ func testLoadFiles(t *testing.T, ext string) {
err := conf.LoadFiles(&c, "test_data/invalid."+ext, "test_data/valid."+ext)
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) {