Skip to content

Commit 790e50f

Browse files
committed
添加数据类型判断
1 parent ef411d9 commit 790e50f

File tree

2 files changed

+269
-1
lines changed

2 files changed

+269
-1
lines changed

array/array.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,40 @@ func (this *Array) flatten(includeEmpty bool) (map[string]any, error) {
722722
return flattened, nil
723723
}
724724

725+
// 获取 array, slice, map or string 的长度
726+
// get array, slice, map or string len
727+
func (this *Array) Len() (num int) {
728+
defer func() {
729+
if err := recover(); err != nil {
730+
num = 0
731+
}
732+
}()
733+
734+
return reflect.ValueOf(this.source).Len()
735+
}
736+
737+
// 判断是否是 slice 类型
738+
// if the source is slice and return true
739+
func (this *Array) IsSlice() bool {
740+
kind := reflect.TypeOf(this.source).Kind()
741+
742+
return kind == reflect.Slice || kind == reflect.Array
743+
}
744+
745+
// 判断是否是 map 类型
746+
// if the source is map and return true
747+
func (this *Array) IsMap() bool {
748+
kind := reflect.TypeOf(this.source).Kind()
749+
750+
return kind == reflect.Map
751+
}
752+
753+
// 判断是否是 slice or map 类型
754+
// if the source is slice or map and return true
755+
func (this *Array) IsArray() bool {
756+
return this.IsSlice() || this.IsMap()
757+
}
758+
725759
// 搜索
726760
// Search data with key from source
727761
func (this *Array) search(source any, path ...string) any {
@@ -980,7 +1014,7 @@ func (this *Array) anySliceFormat(data any) ([]any, bool) {
9801014
newData := dataValue.Interface()
9811015

9821016
newDataKind := reflect.TypeOf(newData).Kind()
983-
if newDataKind == reflect.Slice {
1017+
if newDataKind == reflect.Slice || newDataKind == reflect.Array {
9841018
newDataValue := reflect.ValueOf(newData)
9851019
newDataLen := newDataValue.Len()
9861020

array/array_test.go

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,23 @@ var (
2525
"ddddd",
2626
"fffff",
2727
},
28+
"ddeef": []any{
29+
"ccc中文",
30+
"ddddd",
31+
"fffff",
32+
},
2833
"ddd": []int64{
2934
22,
3035
333,
3136
555,
3237
},
38+
"qqq": [10]int64{
39+
22,
40+
333,
41+
555,
42+
5557,
43+
5558,
44+
},
3345
"ff": map[any]any{
3446
111: "fccccc",
3547
222: "fddddd",
@@ -347,6 +359,11 @@ func Test_Find(t *testing.T) {
347359
"ddddd",
348360
"[]any",
349361
},
362+
{
363+
"b.qqq.2",
364+
"555",
365+
"[num]any",
366+
},
350367
{
351368
"b.ff.222",
352369
"fddddd",
@@ -425,6 +442,223 @@ func Test_Find_func(t *testing.T) {
425442

426443
}
427444

445+
func Test_Len(t *testing.T) {
446+
assert := assertDeepEqualT(t)
447+
448+
testData := []struct {
449+
key string
450+
expected int
451+
msg string
452+
}{
453+
{
454+
"a",
455+
0,
456+
"int",
457+
},
458+
{
459+
"b.ddeef.1",
460+
5,
461+
"string",
462+
},
463+
{
464+
"b.dd",
465+
3,
466+
"[]any",
467+
},
468+
{
469+
"b.qqq",
470+
10,
471+
"[num]any",
472+
},
473+
{
474+
"b.ff",
475+
3,
476+
"map[any]any",
477+
},
478+
{
479+
"b.hhTy3",
480+
0,
481+
"&map[int]any",
482+
},
483+
{
484+
"b.hhTy3.333",
485+
3,
486+
"map[any]string",
487+
},
488+
{
489+
"b.hhTy3.666",
490+
4,
491+
"Slice",
492+
},
493+
}
494+
495+
for _, v := range testData {
496+
check := New(arrData).Sub(v.key).Len()
497+
498+
assert(check, v.expected, v.msg)
499+
}
500+
501+
}
502+
503+
func Test_IsArray(t *testing.T) {
504+
assert := assertDeepEqualT(t)
505+
506+
testData := []struct {
507+
key string
508+
expected bool
509+
msg string
510+
}{
511+
{
512+
"a",
513+
false,
514+
"int",
515+
},
516+
{
517+
"b.dd",
518+
true,
519+
"[]any",
520+
},
521+
{
522+
"b.qqq",
523+
true,
524+
"[num]any",
525+
},
526+
{
527+
"b.ff",
528+
true,
529+
"map[any]any",
530+
},
531+
{
532+
"b.hhTy3",
533+
false,
534+
"&map[int]any",
535+
},
536+
{
537+
"b.hhTy3.333",
538+
true,
539+
"map[any]string",
540+
},
541+
{
542+
"b.hhTy3.666",
543+
true,
544+
"Slice",
545+
},
546+
}
547+
548+
for _, v := range testData {
549+
check := New(arrData).Sub(v.key).IsArray()
550+
551+
assert(check, v.expected, v.msg)
552+
}
553+
554+
}
555+
556+
func Test_IsMap(t *testing.T) {
557+
assert := assertDeepEqualT(t)
558+
559+
testData := []struct {
560+
key string
561+
expected bool
562+
msg string
563+
}{
564+
{
565+
"a",
566+
false,
567+
"int",
568+
},
569+
{
570+
"b.dd",
571+
false,
572+
"[]any",
573+
},
574+
{
575+
"b.qqq",
576+
false,
577+
"[num]any",
578+
},
579+
{
580+
"b.ff",
581+
true,
582+
"map[any]any",
583+
},
584+
{
585+
"b.hhTy3",
586+
false,
587+
"&map[int]any",
588+
},
589+
{
590+
"b.hhTy3.333",
591+
true,
592+
"map[any]string",
593+
},
594+
{
595+
"b.hhTy3.666",
596+
false,
597+
"Slice",
598+
},
599+
}
600+
601+
for _, v := range testData {
602+
check := New(arrData).Sub(v.key).IsMap()
603+
604+
assert(check, v.expected, v.msg)
605+
}
606+
607+
}
608+
609+
func Test_IsSlice(t *testing.T) {
610+
assert := assertDeepEqualT(t)
611+
612+
testData := []struct {
613+
key string
614+
expected bool
615+
msg string
616+
}{
617+
{
618+
"a",
619+
false,
620+
"int",
621+
},
622+
{
623+
"b.dd",
624+
true,
625+
"[]any",
626+
},
627+
{
628+
"b.qqq",
629+
true,
630+
"[num]any",
631+
},
632+
{
633+
"b.ff",
634+
false,
635+
"map[any]any",
636+
},
637+
{
638+
"b.hhTy3",
639+
false,
640+
"&map[int]any",
641+
},
642+
{
643+
"b.hhTy3.333",
644+
false,
645+
"map[any]string",
646+
},
647+
{
648+
"b.hhTy3.666",
649+
true,
650+
"Slice",
651+
},
652+
}
653+
654+
for _, v := range testData {
655+
check := New(arrData).Sub(v.key).IsSlice()
656+
657+
assert(check, v.expected, v.msg)
658+
}
659+
660+
}
661+
428662
func Test_Search(t *testing.T) {
429663
assert := assertT(t)
430664

0 commit comments

Comments
 (0)