From e56a54839916503ff66e2ffde46a1ddbe25fae6b Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 24 Oct 2021 23:50:51 +0800 Subject: [PATCH] + range --- data/bindata.go | 52 ++++++++--------- helper/process.go | 30 ++++++++++ helper/range.go | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 198 insertions(+), 26 deletions(-) create mode 100644 helper/range.go diff --git a/data/bindata.go b/data/bindata.go index 9133ea10..1c811914 100644 --- a/data/bindata.go +++ b/data/bindata.go @@ -204,7 +204,7 @@ func uiFooBarHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "ui/foo/bar.html", size: 21, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "ui/foo/bar.html", size: 21, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -224,7 +224,7 @@ func uiIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "ui/index.html", size: 12, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "ui/index.html", size: 12, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -244,7 +244,7 @@ func xiangDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -264,7 +264,7 @@ func xiangApisReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/README.md", size: 13, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -284,7 +284,7 @@ func xiangApisChartHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/chart.http.json", size: 624, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -304,7 +304,7 @@ func xiangApisPageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/page.http.json", size: 609, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -324,7 +324,7 @@ func xiangApisStorageHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/storage.http.json", size: 1092, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -344,7 +344,7 @@ func xiangApisTableHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/table.http.json", size: 2632, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/table.http.json", size: 2632, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -364,7 +364,7 @@ func xiangApisUserHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/user.http.json", size: 1453, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -384,7 +384,7 @@ func xiangApisXiangHttpJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/apis/xiang.http.json", size: 797, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/apis/xiang.http.json", size: 797, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -404,7 +404,7 @@ func xiangDataAppJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/app.json", size: 245, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/data/app.json", size: 245, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -424,7 +424,7 @@ func xiangDataIconsDs_store() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/data/icons/.DS_Store", size: 6148, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -444,7 +444,7 @@ func xiangDataIcons404Png() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/data/icons/404.png", size: 9342, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -464,7 +464,7 @@ func xiangDataIconsIconIcns() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.icns", size: 75636, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.icns", size: 75636, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -484,7 +484,7 @@ func xiangDataIconsIconIco() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.ico", size: 31310, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.ico", size: 31310, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -504,7 +504,7 @@ func xiangDataIconsIconPng() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/icons/icon.png", size: 22420, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/data/icons/icon.png", size: 22420, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -524,7 +524,7 @@ func xiangDataIndexHtml() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/data/index.html", size: 302, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/data/index.html", size: 302, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -544,7 +544,7 @@ func xiangFlowsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/flows/README.md", size: 12, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -564,7 +564,7 @@ func xiangFlowsMenuFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/flows/menu.flow.json", size: 1045, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -584,7 +584,7 @@ func xiangFlowsTableGetFlowJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/flows/table/get.flow.json", size: 541, mode: os.FileMode(420), modTime: time.Unix(1635080110, 0)} + info := bindataFileInfo{name: "xiang/flows/table/get.flow.json", size: 541, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -604,7 +604,7 @@ func xiangModelsReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/models/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -624,7 +624,7 @@ func xiangModelsMenuJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/menu.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/models/menu.json", size: 2378, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -644,7 +644,7 @@ func xiangModelsUserJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/models/user.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/models/user.json", size: 8826, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -664,7 +664,7 @@ func xiangTablesReadmeMd() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/tables/README.md", size: 15, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -684,7 +684,7 @@ func xiangTablesMenuJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/menu.json", size: 10382, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/tables/menu.json", size: 10382, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } @@ -704,7 +704,7 @@ func xiangTablesUserJson() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "xiang/tables/user.json", size: 1422, mode: os.FileMode(420), modTime: time.Unix(1635080109, 0)} + info := bindataFileInfo{name: "xiang/tables/user.json", size: 1422, mode: os.FileMode(420), modTime: time.Unix(1635090607, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/helper/process.go b/helper/process.go index 4d114c7e..44f1881f 100644 --- a/helper/process.go +++ b/helper/process.go @@ -7,6 +7,7 @@ import ( "github.com/yaoapp/gou" "github.com/yaoapp/gou/query/share" "github.com/yaoapp/kun/exception" + "github.com/yaoapp/kun/utils" ) func init() { @@ -17,6 +18,9 @@ func init() { gou.RegisterProcessHandler("xiang.helper.ArrayKeep", ProcessArrayKeep) gou.RegisterProcessHandler("xiang.helper.MapKeys", ProcessMapKeys) gou.RegisterProcessHandler("xiang.helper.MapValues", ProcessMapValues) + gou.RegisterProcessHandler("xiang.helper.For", ProcessFor) + gou.RegisterProcessHandler("xiang.helper.Each", ProcessEach) + gou.RegisterProcessHandler("xiang.helper.Print", ProcessPrint) } // ProcessArrayPluck xiang.helper.ArrayPluck 将多个数据记录集合,合并为一个数据记录集合 @@ -180,3 +184,29 @@ func ProcessMapKeys(process *gou.Process) interface{} { record := process.ArgsMap(0) return MapKeys(record) } + +// ProcessEach xiang.helper.Each 循环过程控制 +func ProcessEach(process *gou.Process) interface{} { + process.ValidateArgNums(2) + v := process.Args[0] + p := ProcessOf(process.ArgsMap(1)) + Range(v, p) + return nil +} + +// ProcessFor xiang.helper.For 循环过程控制 +func ProcessFor(process *gou.Process) interface{} { + process.ValidateArgNums(3) + from := process.ArgsInt(0) + to := process.ArgsInt(1) + p := ProcessOf(process.ArgsMap(2)) + For(from, to, p) + return nil +} + +// ProcessPrint xiang.helper.Print 打印语句 +func ProcessPrint(process *gou.Process) interface{} { + process.ValidateArgNums(1) + utils.Dump(process.Args...) + return nil +} diff --git a/helper/range.go b/helper/range.go new file mode 100644 index 00000000..0037b3d8 --- /dev/null +++ b/helper/range.go @@ -0,0 +1,142 @@ +package helper + +import ( + "reflect" + "regexp" + + jsoniter "github.com/json-iterator/go" + "github.com/yaoapp/gou" + "github.com/yaoapp/gou/query/share" + "github.com/yaoapp/kun/any" + "github.com/yaoapp/kun/exception" +) + +var reVar = regexp.MustCompile("::([a-z]+)") // ::key, ::value + +// Process 处理器参数 +type Process struct { + Process string `json:"process"` + Args []interface{} `json:"args,omitempty"` +} + +// Range 过程控制 +func Range(v interface{}, process Process) { + value := reflect.ValueOf(v) + value = reflect.Indirect(value) + switch value.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + For(0, any.Of(v).CInt(), process) + break + case reflect.Array, reflect.Slice: + data, err := jsoniter.Marshal(v) + if err != nil { + exception.New("数值格式不能使用Range %s", 400, err.Error()).Throw() + } + v := []interface{}{} + err = jsoniter.Unmarshal(data, v) + rangeArray(v, process) + break + case reflect.String: + break + case reflect.Map: + data, err := jsoniter.Marshal(v) + if err != nil { + exception.New("数值格式不能使用Range %s", 400, err.Error()).Throw() + } + v := map[string]interface{}{} + err = jsoniter.Unmarshal(data, v) + rangeMap(v, process) + break + case reflect.Struct: + data, err := jsoniter.Marshal(v) + if err != nil { + exception.New("数值格式不能使用Range %s", 400, err.Error()).Throw() + } + v := map[string]interface{}{} + err = jsoniter.Unmarshal(data, v) + rangeMap(v, process) + break + } +} + +// For 过程控制 +func For(from int, to int, p Process) { + for i := from; i < to; i++ { + args := p.Args + bindings := map[string]interface{}{ + "key": i, + "value": i, + } + args = bindArgs(args, bindings) + gou.NewProcess(p.Process, args...).Run() + } +} + +func bindArgs(args []interface{}, bindings map[string]interface{}) []interface{} { + for i := range args { + args[i] = share.Bind(args[i], bindings, reVar) + } + return args +} + +func rangeString(v string, p Process) { + var bytes = []byte(v) + for key, value := range bytes { + args := p.Args + bindings := map[string]interface{}{ + "::key": key, + "::value": value, + } + args = bindArgs(args, bindings) + gou.NewProcess(p.Process, args...).Run() + } + +} + +func rangeMap(v map[string]interface{}, p Process) { + for key, value := range v { + args := p.Args + bindings := map[string]interface{}{ + "::key": key, + "::value": value, + } + args = bindArgs(args, bindings) + gou.NewProcess(p.Process, args...).Run() + } +} + +func rangeArray(v []interface{}, p Process) { + for key, value := range v { + args := p.Args + bindings := map[string]interface{}{ + "::key": key, + "::value": value, + } + args = bindArgs(args, bindings) + gou.NewProcess(p.Process, args...).Run() + } +} + +// ProcessOf 转换映射表 +func ProcessOf(v map[string]interface{}) Process { + process, ok := v["process"] + if !ok { + exception.New("参数错误: 缺少 process", 400).Throw() + } + + processStr, ok := process.(string) + if !ok { + exception.New("参数错误: process 应该为字符串 ", 400).Throw() + } + + if args, ok := v["args"].([]interface{}); ok { + return Process{ + Process: processStr, + Args: args, + } + } + return Process{ + Process: processStr, + Args: []interface{}{}, + } +}