add pattern matching
This commit is contained in:
@ -26,6 +26,7 @@ import (
|
||||
"git.loyso.art/frx/eway/internal/entity"
|
||||
"git.loyso.art/frx/eway/internal/export"
|
||||
"git.loyso.art/frx/eway/internal/interconnect/eway"
|
||||
"git.loyso.art/frx/eway/internal/matcher"
|
||||
|
||||
"github.com/rodaine/table"
|
||||
"github.com/rs/zerolog"
|
||||
@ -387,8 +388,18 @@ func newViewItemsUniqueParams() *cli.Command {
|
||||
|
||||
func newViewItemsParamsKnownValues() *cli.Command {
|
||||
return &cli.Command{
|
||||
Name: "params-values",
|
||||
Usage: "Show all values of requested parameters",
|
||||
Name: "params-values",
|
||||
Usage: "Show all values of requested parameters",
|
||||
Flags: []cli.Flag{
|
||||
&cli.BoolFlag{
|
||||
Name: "case-insensitive",
|
||||
Usage: "Ignores cases of keys",
|
||||
},
|
||||
&cli.StringSliceFlag{
|
||||
Name: "regex",
|
||||
Usage: "Registers regex to match",
|
||||
},
|
||||
},
|
||||
Action: decorateAction(viewItemsParamsKnownValuesAction),
|
||||
}
|
||||
}
|
||||
@ -558,19 +569,45 @@ func viewItemsParamsKnownValuesAction(ctx context.Context, c *cli.Command) error
|
||||
}
|
||||
|
||||
params := c.Args().Slice()
|
||||
requestedValues := make(map[string]map[string]struct{}, len(params))
|
||||
for _, param := range params {
|
||||
log.Debug().Str("param", param).Msg("registering param")
|
||||
requestedValues[param] = make(map[string]struct{}, 16)
|
||||
opts := make([]matcher.RadixOpt, 0, 1)
|
||||
if c.Bool("case-insensitive") {
|
||||
opts = append(opts, matcher.RadixCaseInsensitive())
|
||||
}
|
||||
|
||||
m := matcher.NewRadix(opts...)
|
||||
for _, param := range params {
|
||||
log.Debug().Str("param", param).Msg("registering param")
|
||||
m.Register(param)
|
||||
}
|
||||
for _, regexp := range c.StringSlice("regex") {
|
||||
log.Debug().Str("regexp", regexp).Msg("registering regexp")
|
||||
m.RegisterRegexp(regexp)
|
||||
}
|
||||
|
||||
requestedValues := make(map[string]map[string]struct{}, len(params))
|
||||
requestedValuesByPattern := make(map[string]map[string]struct{}, len(params))
|
||||
iter := getItemsIter(ctx, repository.GoodsItem())
|
||||
for iter.Next() {
|
||||
item := iter.Get()
|
||||
for k, v := range item.Parameters {
|
||||
if _, ok := requestedValues[k]; ok {
|
||||
requestedValues[k][v] = struct{}{}
|
||||
matchedPattern := m.MatchByPattern(k)
|
||||
if matchedPattern == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
values, ok := requestedValues[k]
|
||||
if !ok {
|
||||
values = make(map[string]struct{})
|
||||
}
|
||||
values[v] = struct{}{}
|
||||
requestedValues[k] = values
|
||||
|
||||
values, ok = requestedValuesByPattern[matchedPattern]
|
||||
if !ok {
|
||||
values = map[string]struct{}{}
|
||||
}
|
||||
values[v] = struct{}{}
|
||||
requestedValuesByPattern[matchedPattern] = values
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user