try to abstract matcher
This commit is contained in:
@ -411,8 +411,14 @@ func newViewItemsParamsKnownValues() *cli.Command {
|
|||||||
|
|
||||||
func newViewItemsCountCmd() *cli.Command {
|
func newViewItemsCountCmd() *cli.Command {
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "count",
|
Name: "count",
|
||||||
Usage: "iterates over collection and counts number of items",
|
Usage: "iterates over collection and counts number of items",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringSliceFlag{
|
||||||
|
Name: "param-key-match",
|
||||||
|
Usage: "filters by parameters with AND logic",
|
||||||
|
},
|
||||||
|
},
|
||||||
Action: decorateAction(viewItemsCountAction),
|
Action: decorateAction(viewItemsCountAction),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -649,36 +655,47 @@ func viewItemsCountAction(ctx context.Context, c *cli.Command) error {
|
|||||||
return fmt.Errorf("getting repository: %w", err)
|
return fmt.Errorf("getting repository: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
itemChan, err := r.GoodsItem().ListIter(ctx, 10)
|
filters := c.StringSlice("param-key-match")
|
||||||
if err != nil {
|
m := matcher.NewRadix()
|
||||||
if !errors.Is(err, entity.ErrNotImplemented) {
|
patternMapped := make(map[string]struct{}, len(filters))
|
||||||
return fmt.Errorf("getting list iter: %w", err)
|
if len(filters) == 0 {
|
||||||
|
m.Register("*")
|
||||||
|
} else {
|
||||||
|
for _, f := range filters {
|
||||||
|
m.Register(f)
|
||||||
|
}
|
||||||
|
for _, pattern := range m.Patterns() {
|
||||||
|
patternMapped[pattern] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
if err == nil {
|
items, err := r.GoodsItem().List(ctx)
|
||||||
var done bool
|
if err != nil {
|
||||||
for !done {
|
return fmt.Errorf("getting items: %w", err)
|
||||||
select {
|
}
|
||||||
case _, ok := <-itemChan:
|
for _, item := range items {
|
||||||
if !ok {
|
seenPatterns := map[string]struct{}{}
|
||||||
done = true
|
|
||||||
continue
|
for k := range item.Parameters {
|
||||||
}
|
pattern := m.MatchByPattern(k)
|
||||||
count++
|
if pattern == "" {
|
||||||
case <-ctx.Done():
|
continue
|
||||||
return ctx.Err()
|
|
||||||
}
|
}
|
||||||
|
if _, ok := seenPatterns[pattern]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seenPatterns[pattern] = struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(seenPatterns) == len(patternMapped) {
|
||||||
|
println("Item matched", item.Articul, item.Cart)
|
||||||
|
count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := r.GoodsItem().List(ctx)
|
println(count)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("getting list: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
zerolog.Ctx(ctx).Info().Int("count", count).Int("list_count", len(items)).Msg("read all items")
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
internal/matcher/matcher.go
Normal file
13
internal/matcher/matcher.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package matcher
|
||||||
|
|
||||||
|
type Unit interface {
|
||||||
|
MatchByPattern(value string) (pattern string)
|
||||||
|
Match(value string) bool
|
||||||
|
|
||||||
|
// Move this to init stage because some of matchers
|
||||||
|
// might not be applicable to it
|
||||||
|
RegisterRegexp(regexpPattern string)
|
||||||
|
Register(pattern string)
|
||||||
|
|
||||||
|
Patterns() []string
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user