Compare commits

...

2 Commits

Author SHA1 Message Date
ryanfkeepers
7693b3a5d4 comment cleanup 2023-04-10 13:00:03 -06:00
ryanfkeepers
5182d96271 match sites with a url path suffix
Adds suffix matching to the site owner id/name
lookup process.  This allows consumers to query
for a site by only its path (ex: "/sites/foo") and still
end up with a well formed id, name tuple.  One
gotcha is that this requires the lookup maps to
be populated.  If a lookup map is not passed in
with a suffix matcher, the fallback lookup will fail.
2023-04-07 16:47:25 -06:00
5 changed files with 114 additions and 17 deletions

View File

@ -18,6 +18,7 @@ import (
"github.com/alcionai/corso/src/internal/operations/inject" "github.com/alcionai/corso/src/internal/operations/inject"
"github.com/alcionai/corso/src/pkg/account" "github.com/alcionai/corso/src/pkg/account"
"github.com/alcionai/corso/src/pkg/fault" "github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/filters"
) )
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -233,6 +234,12 @@ type getOwnerIDAndNamer interface {
// api to fetch the user or site using the owner value. This fallback assumes // api to fetch the user or site using the owner value. This fallback assumes
// that the owner is a well formed ID or display name of appropriate design // that the owner is a well formed ID or display name of appropriate design
// (PrincipalName for users, WebURL for sites). // (PrincipalName for users, WebURL for sites).
//
// Consumers are allowed to pass in a path suffix (eg: /sites/foo) as a site
// owner, but only if they also pass in a nameToID map. A nil map will fallback
// to a lookup by id using the suffix, which will fail for being malformed.
// Suffix matching follows filter PathSuffix rules: only complete elements match,
// case-independant, leading/tailing slashes are optional.
func (r resourceClient) getOwnerIDAndNameFrom( func (r resourceClient) getOwnerIDAndNameFrom(
ctx context.Context, ctx context.Context,
discovery api.Client, discovery api.Client,
@ -254,9 +261,14 @@ func (r resourceClient) getOwnerIDAndNameFrom(
err error err error
) )
// if r.enum == Sites { // check if the provided owner is a suffix of a weburl in the lookup map
// TODO: check all suffixes in nameToID if r.enum == Sites {
// } url, ok := filters.PathSuffix([]string{owner}).CompareAny(ins.Names()...)
if ok {
id, _ := ins.IDOf(url)
return id, url, nil
}
}
id, name, err = r.getter.GetIDAndName(ctx, owner) id, name, err = r.getter.GetIDAndName(ctx, owner)
if err != nil { if err != nil {

View File

@ -65,7 +65,8 @@ func (suite *GraphConnectorUnitSuite) TestPopulateOwnerIDAndNamesFrom() {
enum: Users, enum: Users,
getter: &mockNameIDGetter{id: id, name: name}, getter: &mockNameIDGetter{id: id, name: name},
} }
noLookup = &resourceClient{enum: Users, getter: &mockNameIDGetter{}} noLookup = &resourceClient{enum: Users, getter: &mockNameIDGetter{}}
siteLookup = &resourceClient{enum: Sites, getter: &mockNameIDGetter{}}
) )
table := []struct { table := []struct {
@ -249,6 +250,18 @@ func (suite *GraphConnectorUnitSuite) TestPopulateOwnerIDAndNamesFrom() {
expectName: name, expectName: name,
expectErr: require.NoError, expectErr: require.NoError,
}, },
{
name: "site suffix lookup",
owner: "/url/path",
rc: siteLookup,
ins: common.IDsNames{
IDToName: nil,
NameToID: map[string]string{"http://some/site/url/path": id},
},
expectID: id,
expectName: "http://some/site/url/path",
expectErr: require.NoError,
},
} }
for _, test := range table { for _, test := range table {
suite.Run(test.name, func() { suite.Run(test.name, func() {

View File

@ -359,21 +359,22 @@ func newSliceFilter(c comparator, targets, normTargets []string, negate bool) Fi
// ---------------------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------------------
// CompareAny checks whether any one of all the provided // CompareAny checks whether any one of all the provided
// inputs passes the filter. // inputs passes the filter. If one passes, that value is
// returned. If nothing matches, returns ("", false)
// //
// Note that, as a gotcha, CompareAny can resolve truthily // Note that, as a gotcha, CompareAny can resolve truthily
// for both the standard and negated versions of a filter. // for both the standard and negated versions of a filter.
// Ex: consider the input CompareAny(true, false), which // Ex: consider the input CompareAny(true, false), which
// will return true for both Equals(true) and NotEquals(true), // will return true for both Equals(true) and NotEquals(true),
// because at least one element matches for both filters. // because at least one element matches for both filters.
func (f Filter) CompareAny(inputs ...string) bool { func (f Filter) CompareAny(inputs ...string) (string, bool) {
for _, in := range inputs { for _, in := range inputs {
if f.Compare(in) { if f.Compare(in) {
return true return in, true
} }
} }
return false return "", false
} }
// Compare checks whether the input passes the filter. // Compare checks whether the input passes the filter.
@ -442,6 +443,48 @@ func (f Filter) Compare(input string) bool {
return res return res
} }
// Matches extends Compare by not only checking if
// the input passes the filter, but if it passes, the
// target which matched and its index are returned as well.
// If more than one value matches the input, only the
// first is returned.
// returns ("", -1, false) if no match is found.
// TODO: only partially implemented.
// func (f Filter) Matches(input string) (string, int, bool) {
// var (
// cmp func(string, string) bool
// res bool
// targets = f.NormalizedTargets
// )
// switch f.Comparator {
// case TargetPathPrefix:
// cmp = pathPrefix
// case TargetPathContains:
// cmp = pathContains
// case TargetPathSuffix:
// cmp = pathSuffix
// case TargetPathEquals:
// cmp = pathEquals
// default:
// return "", -1, false
// }
// for i, tgt := range targets {
// res = cmp(norm(tgt), norm(input))
// if !f.Negate && res {
// return f.Targets[i], i, true
// }
// if f.Negate && !res {
// return f.Targets[i], i, true
// }
// }
// return "", -1, false
// }
// true if t == i // true if t == i
func equals(target, input string) bool { func equals(target, input string) bool {
return strings.EqualFold(target, input) return strings.EqualFold(target, input)

View File

@ -45,20 +45,47 @@ func (suite *FiltersSuite) TestEquals_any() {
nf := filters.NotEqual("foo") nf := filters.NotEqual("foo")
table := []struct { table := []struct {
name string name string
input []string input []string
expectF assert.BoolAssertionFunc expectF assert.BoolAssertionFunc
expectNF assert.BoolAssertionFunc expectFVal string
expectFIdx int
expectNF assert.BoolAssertionFunc
expectNFVal string
expectNFIdx int
}{ }{
{"includes target", []string{"foo", "bar"}, assert.True, assert.True}, {
{"not includes target", []string{"baz", "qux"}, assert.False, assert.True}, name: "includes target",
input: []string{"foo", "bar"},
expectF: assert.True,
expectFVal: "foo",
expectFIdx: 0,
expectNF: assert.True,
expectNFVal: "bar",
expectNFIdx: 1,
},
{
name: "not includes target",
input: []string{"baz", "qux"},
expectF: assert.False,
expectFVal: "",
expectFIdx: -1,
expectNF: assert.True,
expectNFVal: "baz",
expectNFIdx: 0,
},
} }
for _, test := range table { for _, test := range table {
suite.Run(test.name, func() { suite.Run(test.name, func() {
t := suite.T() t := suite.T()
test.expectF(t, f.CompareAny(test.input...), "filter") v, b := f.CompareAny(test.input...)
test.expectNF(t, nf.CompareAny(test.input...), "negated filter") test.expectF(t, b, "filter")
assert.Equal(t, test.expectFVal, v, "value")
v, b = nf.CompareAny(test.input...)
test.expectNF(t, b, "neg-filter")
assert.Equal(t, test.expectNFVal, v, "neg-value")
}) })
} }
} }

View File

@ -223,7 +223,9 @@ func matchesAny[T scopeT, C categoryT](s T, cat C, inpts []string) bool {
return false return false
} }
return s[cat.String()].CompareAny(inpts...) _, pass := s[cat.String()].CompareAny(inpts...)
return pass
} }
// getCategory returns the scope's category value. // getCategory returns the scope's category value.