try to abstract matcher

This commit is contained in:
Aleksandr Trushkin
2024-02-10 20:21:55 +03:00
parent 504e5fce29
commit 969ef726eb
2 changed files with 53 additions and 23 deletions

View File

@ -411,8 +411,14 @@ func newViewItemsParamsKnownValues() *cli.Command {
func newViewItemsCountCmd() *cli.Command {
return &cli.Command{
Name: "count",
Usage: "iterates over collection and counts number of items",
Name: "count",
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),
}
}
@ -649,36 +655,47 @@ func viewItemsCountAction(ctx context.Context, c *cli.Command) error {
return fmt.Errorf("getting repository: %w", err)
}
itemChan, err := r.GoodsItem().ListIter(ctx, 10)
if err != nil {
if !errors.Is(err, entity.ErrNotImplemented) {
return fmt.Errorf("getting list iter: %w", err)
filters := c.StringSlice("param-key-match")
m := matcher.NewRadix()
patternMapped := make(map[string]struct{}, len(filters))
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
if err == nil {
var done bool
for !done {
select {
case _, ok := <-itemChan:
if !ok {
done = true
continue
}
count++
case <-ctx.Done():
return ctx.Err()
items, err := r.GoodsItem().List(ctx)
if err != nil {
return fmt.Errorf("getting items: %w", err)
}
for _, item := range items {
seenPatterns := map[string]struct{}{}
for k := range item.Parameters {
pattern := m.MatchByPattern(k)
if pattern == "" {
continue
}
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)
if err != nil {
return fmt.Errorf("getting list: %w", err)
}
println(count)
zerolog.Ctx(ctx).Info().Int("count", count).Int("list_count", len(items)).Msg("read all items")
return nil
}

View 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
}