/
for_silce.go
52 lines (49 loc) · 1.22 KB
/
for_silce.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gobatis
import (
"fmt"
"reflect"
"strings"
)
type Slice struct {
/*
实现普通切片的数据处理
*/
}
func (s Slice) ForEach(value any, template string, separator string) (string, string, []any, error) {
var v any
var err error
var item, itemSql string
var param []any
items := make([]string, 0)
tempSql := make([]string, 0)
params := make([]any, 0)
valueOf := reflect.ValueOf(value)
for i := 0; i < valueOf.Len(); i++ {
IndexV := valueOf.Index(i)
v = IndexV.Interface()
if IndexV.Kind() == reflect.Slice {
return "", "", nil, fmt.Errorf("'slice' element error")
}
if IndexV.Kind() == reflect.Map {
ctx := v.(map[string]any)
item, itemSql, param, err = AnalysisForTemplate(template, ctx, nil)
if err != nil {
return "", "", nil, err
}
items = append(items, item)
tempSql = append(tempSql, itemSql)
params = append(params, param...)
continue
}
item, itemSql, param, err = AnalysisForTemplate(template, nil, v)
if err != nil {
return "", "", nil, err
}
items = append(items, item)
tempSql = append(tempSql, itemSql)
params = append(params, param...)
}
join := strings.Join(items, separator)
s2 := strings.Join(tempSql, separator)
return join, s2, params, nil
}