Skip to content

Commit dac0340

Browse files
committed
feat: supports Slices #4
1 parent 7f75065 commit dac0340

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

generator.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"io"
55
"log"
6+
"strings"
67
"text/template"
78
)
89

@@ -87,6 +88,9 @@ func (g *generator) generate(writer io.Writer) {
8788
}
8889
return field + fn
8990
},
91+
"HasPrefixSlice": func(types string) bool {
92+
return strings.HasPrefix(types, "[]")
93+
},
9094
}
9195
t := template.Must(template.New("tmpl").Funcs(funcMap).Parse(tmpl))
9296

@@ -329,6 +333,7 @@ func (repo *{{ .RepositoryStructName }}) List(ctx context.Context, req *{{ .Stru
329333
filters := xian.NewFilters({{ .StructName }}IndexesConfig)
330334
{{- end }}
331335
{{- range $fi := .FieldInfos }}
336+
{{ $PrefixIsSlice := HasPrefixSlice $fi.FieldType}}
332337
{{- if eq $fi.FieldType "bool" }}
333338
if req.{{ $fi.Field }} != "" {
334339
{{- if eq $Enable true }}
@@ -369,6 +374,18 @@ func (repo *{{ .RepositoryStructName }}) List(ctx context.Context, req *{{ .Stru
369374
q = q.Filter("{{ $fi.DsTag }} =", req.{{ $fi.Field }})
370375
{{- end }}
371376
}
377+
{{- else if eq $PrefixIsSlice true }}
378+
if len(req.{{ $fi.Field }}) > 0 {
379+
{{- if eq $Enable true }}
380+
{{- range $idx := $fi.Indexes }}
381+
filters.{{ $idx.Method }}({{ $idx.ConstName }}, req.{{ $fi.Field }})
382+
{{- end }}
383+
{{- else }}
384+
for _, x := range req.{{ $fi.Field }} {
385+
q = q.Filter("{{ $fi.DsTag }} =", x)
386+
}
387+
{{- end }}
388+
}
372389
{{- end }}
373390
{{- end }}
374391
{{ if eq $Enable true }}

testfiles/a/name.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ type Name struct {
1313
Created time.Time `datastore:"created"`
1414
// supported indexer tags word: e/equal(Default), l/like, p/prefix,
1515
// TODO s/suffix
16-
Desc string `datastore:"description" indexer:"l"`
17-
Desc2 string `datastore:"description2" indexer:"p"`
18-
Done bool `datastore:"done"`
19-
Count int `datastore:"count"`
20-
Indexes []string `datastore:"indexes"`
16+
Desc string `datastore:"description" indexer:"l"`
17+
Desc2 string `datastore:"description2" indexer:"p"`
18+
Done bool `datastore:"done"`
19+
Count int `datastore:"count"`
20+
PriceList []int `datastore:"priceList"`
21+
Indexes []string `datastore:"indexes"`
2122
}

testfiles/a/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ type Task struct {
1818
Count64 int64 `datastore:"count64"`
1919
Proportion float64 `datastore:"proportion"`
2020
Flag BoolCriteria `datastore:"flag" type:"string"` // If you want to use your unique type, set the original type to `type`
21+
NameList []string `datastore:"nameList"`
2122
}

0 commit comments

Comments
 (0)