Skip to content
Snippets Groups Projects
Commit 2a8792be authored by Danis Kirasirov's avatar Danis Kirasirov
Browse files

update array non-strict convert

parent 848a6e30
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/errors" "git.perx.ru/perxis/perxis-go/pkg/errors"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
...@@ -138,8 +139,15 @@ func (ArrayType) Walk(ctx context.Context, field *Field, v interface{}, fn WalkF ...@@ -138,8 +139,15 @@ func (ArrayType) Walk(ctx context.Context, field *Field, v interface{}, fn WalkF
return nil, false, fmt.Errorf("incorrect type: %s, expected array or slice", arr.Kind()) return nil, false, fmt.Errorf("incorrect type: %s, expected array or slice", arr.Kind())
} }
// При использовании опции WalkNonStrict преобразовываем в слайс // При использовании опции WalkNonStrict
arr = reflect.ValueOf([]interface{}{v}) switch {
case arr.Kind() == reflect.String:
// В случае строки пытаемся разбить по пробелам
arr = reflect.ValueOf(strings.Split(arr.String(), " "))
default:
// По умолчанию преобразуем в слайс из одного элемента
arr = reflect.ValueOf([]interface{}{v})
}
} }
var length int var length int
......
...@@ -118,8 +118,15 @@ func TestArrayField_DecodeNonStrict(t *testing.T) { ...@@ -118,8 +118,15 @@ func TestArrayField_DecodeNonStrict(t *testing.T) {
false, false,
}, },
{ {
"Incorrect data type", "String array",
Array(Number(NumberFormatInt)), Array(Number(NumberFormatFloat)),
"1 2 3.5",
[]interface{}{1.0, 2.0, 3.5},
false,
},
{
"Incoorect type",
Array(Bool()),
"1 2 3", "1 2 3",
nil, nil,
true, true,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment