Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: manipbolt #175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,50 @@ module go.aporeto.io/manipulate
go 1.20

require (
go.aporeto.io/elemental v1.122.0
go.aporeto.io/wsc v1.51.0
go.aporeto.io/elemental v1.122.1-0.20230127211758-51fb47178716
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please stay on releases

go.aporeto.io/wsc v1.51.1-0.20230127211509-870916470c0c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please stay on releases

)

require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/ghodss/yaml v1.0.0
github.com/asdine/storm v2.1.2+incompatible
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8
github.com/gofrs/uuid v4.4.0+incompatible
github.com/golang/mock v1.6.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-memdb v1.3.4
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f
github.com/json-iterator/go v1.1.12
github.com/mitchellh/copystructure v1.2.0
github.com/olekukonko/tablewriter v0.0.5
github.com/opentracing/opentracing-go v1.2.0
github.com/smartystreets/goconvey v1.7.2
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
go.uber.org/zap v1.24.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.5.0
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22
k8s.io/helm v2.17.0+incompatible
)

require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.2.0 // indirect
github.com/Sereal/Sereal v0.0.0-20200820125258-a016b7cda3f3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
Expand All @@ -50,15 +59,20 @@ require (
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/smartystreets/assertions v1.2.0 // indirect
github.com/smartystreets/assertions v1.13.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
go.etcd.io/bbolt v1.3.6 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
Expand Down
75 changes: 39 additions & 36 deletions go.sum

Large diffs are not rendered by default.

101 changes: 101 additions & 0 deletions manipbolt/compiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package manipbolt

import (
"fmt"

"github.com/asdine/storm/q"
"go.aporeto.io/elemental"
"go.aporeto.io/manipulate"
)

func compileFilter(f *elemental.Filter) (q.Matcher, error) {

if len(f.Operators()) == 0 {
return q.And(), nil
}

matchers := []q.Matcher{}

for i, operator := range f.Operators() {

switch operator {

case elemental.AndOperator:

k := f.Keys()[i]
values := f.Values()[i]
items := []q.Matcher{}

switch f.Comparators()[i] {

case elemental.EqualComparator:

items = append(items, containsOrEqual(k, values[0]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked that function yet, but either it is not doing what it says it does (equality check) or should not be used. From what I understand, a == "y" would match x if x == "ay"


case elemental.MatchComparator:

subs := []q.Matcher{}
for _, value := range values {

v, ok := value.(string)
if !ok {
return nil, manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("regex only supports string: %v", value)}
}

subs = append(subs, regexMatcher(k, v))
}

items = append(items, q.Or(subs...))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check or is the behavior of other filter compilers.


case elemental.ContainComparator:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should also be the behavior for elemental.InComparator


subs := []q.Matcher{}
for _, value := range values {
subs = append(subs, containsOrEqual(k, value))
}

items = append(items, q.Or(subs...))

default:
return nil, manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("invalid comparator: %d", f.Comparators()[i])}
}

matchers = append(matchers, items...)

case elemental.AndFilterOperator:

subs := []q.Matcher{}
for _, sub := range f.AndFilters()[i] {

matcher, err := compileFilter(sub)
if err != nil {
return nil, err
}

subs = append(subs, matcher)
}

matchers = append(matchers, q.And(subs...))

case elemental.OrFilterOperator:

subs := []q.Matcher{}
for _, sub := range f.OrFilters()[i] {

matcher, err := compileFilter(sub)
if err != nil {
return nil, err
}

subs = append(subs, matcher)
}

matchers = append(matchers, q.Or(subs...))

default:
return nil, manipulate.ErrCannotBuildQuery{Err: fmt.Errorf("invalid operator: %d", operator)}
}
}

return q.And(matchers...), nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this misses other basic comparators, namely

  • elemental.NotEqualComparator
  • elemental.NotContainComparator
  • elemental.NotInComparator
  • elemental.GreaterComparator
  • elemental.GreaterOrEqualComparator
  • elemental.LesserComparator
  • elemental.GreaterOrEqualComparator
  • elemental.ExistsComparator
  • elemental.NotExistsComparator

Loading