From d7ff3c31078c69af47bb702846a40eb25ef1e8af Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Mon, 22 Jul 2024 01:53:06 -0700 Subject: [PATCH] Fix: wrap error --- internal/toml/decode.go | 7 +++++-- internal/toml/encode.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/toml/decode.go b/internal/toml/decode.go index aa0ae23..0fe95a2 100644 --- a/internal/toml/decode.go +++ b/internal/toml/decode.go @@ -153,13 +153,14 @@ func handleArray(valueString string) (value any) { if intErr == nil { intVals = append(intVals, avalInt) } - } + if len(intVals) == len(asplit) && len(intVals) > 0 { value = intVals } else { value = asplit } + return value } @@ -276,6 +277,7 @@ func (d *decoder) readData() error { } myTable, mapAssertOK := d.tables[string(tableName)].(map[string]interface{}) + if !mapAssertOK { d.tables[string(tableName)] = make(map[string]interface{}) myTable = d.tables[string(tableName)].(map[string]interface{}) //nolint:forcetypeassert @@ -334,7 +336,7 @@ func trimTest(data []byte) (trimmed []byte) { func UnmarshalTOML(data []byte, v interface{}) error { var err error if data, err = unicode.UTF8.NewDecoder().Bytes(data); err != nil { - return err + return fmt.Errorf("unicode decode failure: %w", err) } if !bytes.Contains(data, []byte("[")) { @@ -360,6 +362,7 @@ func UnmarshalTOML(data []byte, v interface{}) error { if ref.Elem().Kind() != reflect.Struct { return fmt.Errorf("%w: non-struct", ErrBadTarget) } + data = bytes.ReplaceAll(data, []byte("\r"), []byte("")) return newDecoder(data, v).start() diff --git a/internal/toml/encode.go b/internal/toml/encode.go index 055d8ad..6de9ef4 100644 --- a/internal/toml/encode.go +++ b/internal/toml/encode.go @@ -118,7 +118,7 @@ func handleBottomLevelField(field reflect.StructField, ref reflect.Value, buf *p _, _ = buf.WriteString(field.Tag.Get("toml")) _, _ = buf.WriteString(" = ") //goland:noinspection GoSwitchMissingCasesForIotaConsts - switch ref.Kind() { + switch ref.Kind() { //nolint:exhaustive case reflect.String: if err := handleString(ref.String(), buf); err != nil { return err