Skip to content

Commit 0c21072

Browse files
committed
refactor(input): parsing array
1 parent ee82987 commit 0c21072

File tree

8 files changed

+45
-30
lines changed

8 files changed

+45
-30
lines changed

input/iterable.go renamed to input/array.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ package input
99

1010
import "reflect"
1111

12-
func ParseIterable(arg any) []any {
12+
func ParseArray(arg any) []any {
1313
val := reflect.ValueOf(arg)
14-
return parseIterableValue(&val)
14+
return parseArrayValue(&val)
1515
}
1616

17-
func parseIterableValue(v *reflect.Value) []any {
17+
func parseArrayValue(v *reflect.Value) []any {
1818
e := v.Type().Elem()
1919

2020
switch e.Kind() {
@@ -29,7 +29,7 @@ func parseIterableValue(v *reflect.Value) []any {
2929

3030
pv := parseForeignValue(&field)
3131
if pv == nil {
32-
panic("error: unsupported nested struct")
32+
panic("error: unsupported struct")
3333
}
3434

3535
res[i] = pv

input/iterable_test.go renamed to input/array_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/durudex/go-polybase/input"
1515
)
1616

17-
var ParseIterableTests = map[string]struct {
17+
var ParseArrayTests = map[string]struct {
1818
arg any
1919
want []any
2020
}{
@@ -48,10 +48,10 @@ var ParseIterableTests = map[string]struct {
4848
},
4949
}
5050

51-
func TestParseIterable(t *testing.T) {
52-
for name, test := range ParseIterableTests {
51+
func TestParseArray(t *testing.T) {
52+
for name, test := range ParseArrayTests {
5353
t.Run(name, func(t *testing.T) {
54-
got := input.ParseIterable(test.arg)
54+
got := input.ParseArray(test.arg)
5555

5656
if !reflect.DeepEqual(got, test.want) {
5757
t.Fatal("error: want does not match")
@@ -60,11 +60,11 @@ func TestParseIterable(t *testing.T) {
6060
}
6161
}
6262

63-
func BenchmarkParseIterable(b *testing.B) {
64-
for name, test := range ParseIterableTests {
63+
func BenchmarkParseArray(b *testing.B) {
64+
for name, test := range ParseArrayTests {
6565
b.Run(name, func(b *testing.B) {
6666
for i := 0; i < b.N; i++ {
67-
input.ParseIterable(test.arg)
67+
input.ParseArray(test.arg)
6868
}
6969
})
7070
}

input/input.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func Parse(args []any) []any {
1717

1818
switch v.Kind() {
1919
case reflect.Array, reflect.Slice:
20-
pv := parseIterableValue(&v)
20+
pv := parseArrayValue(&v)
2121

2222
if res == nil {
2323
res = pv

input/input_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ var ParseTests = map[string]struct{ args, want []any }{
4848
1, "durudex", true, &input.Foreign{
4949
CollectionID: "example/example",
5050
ID: "1",
51-
}, "", 0, false,
51+
}, new(string), new(int), new(bool),
5252
},
5353
},
5454
"Map": {

input/pointer.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,29 @@ func ParsePointer(arg any) []any {
1717
func parsePointerValue(v *reflect.Value) []any {
1818
e := v.Elem()
1919

20+
pv := parseSimplePointerValue(v)
21+
if pv == nil {
22+
return parseStructValue(&e)
23+
}
24+
25+
return []any{pv}
26+
}
27+
28+
func parseSimplePointerValue(v *reflect.Value) any {
29+
e := v.Elem()
30+
2031
if v.IsNil() {
21-
panic("error: unsupported nil value")
32+
panic("error: unsupported pointer nil value")
2233
} else if e.Kind() == reflect.Struct {
23-
pf := parseForeignValue(v)
24-
if pf != nil {
25-
return []any{pf}
34+
pv := parseForeignValue(v)
35+
if pv == nil {
36+
return nil
2637
}
2738

28-
return parseStructValue(&e)
39+
return pv
2940
} else if !AllowedKindType[e.Kind()] {
30-
panic("error: unsupported type")
41+
panic("error: unsupported pointer type")
3142
}
3243

33-
return []any{e.Interface()}
44+
return v.Interface()
3445
}

input/pointer_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ var ParsePointerTests = map[string]struct {
2020
}{
2121
"String": {
2222
arg: new(string),
23-
want: []any{""},
23+
want: []any{new(string)},
2424
},
2525
"Integer": {
2626
arg: new(int),
27-
want: []any{0},
27+
want: []any{new(int)},
2828
},
2929
"Boolean": {
3030
arg: new(bool),
31-
want: []any{false},
31+
want: []any{new(bool)},
3232
},
3333
"Struct": {
3434
arg: &ParseStructMock{

input/struct.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,35 @@ func ParseStruct(arg any) []any {
1616

1717
func parseStructValue(v *reflect.Value) []any {
1818
n := v.NumField()
19-
res := make([]any, 0, n)
19+
res := make([]any, n)
2020

2121
for i := 0; i < n; i++ {
2222
field := v.Field(i)
2323

2424
switch field.Kind() {
2525
case reflect.Pointer:
26-
pv := parsePointerValue(&field)
27-
res = append(res, pv...)
26+
pv := parseSimplePointerValue(&field)
27+
if pv == nil {
28+
panic("error: unsupported pointer value")
29+
}
30+
31+
res[i] = pv
2832
case reflect.Map:
2933
pv := parseMapValue(&field)
30-
res = append(res, pv)
34+
res[i] = pv
3135
case reflect.Struct:
3236
pv := parseForeignValue(&field)
3337
if pv == nil {
3438
panic("error: unsupported struct type")
3539
}
3640

37-
res = append(res, pv)
41+
res[i] = pv
3842
default:
3943
if !AllowedKindType[field.Kind()] {
4044
panic("error: unsupported type")
4145
}
4246

43-
res = append(res, field.Interface())
47+
res[i] = field.Interface()
4448
}
4549
}
4650

input/struct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var ParseStructTests = map[string]struct {
4444
String: new(string),
4545
Boolean: new(bool),
4646
},
47-
want: []any{0, "", false},
47+
want: []any{new(int), new(string), new(bool)},
4848
},
4949
"Foreign": {
5050
arg: struct{ F input.Foreign }{

0 commit comments

Comments
 (0)