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

feat: Add keyGet function and related test. #273

Merged
merged 1 commit into from
Aug 12, 2022
Merged
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
19 changes: 19 additions & 0 deletions Casbin.UnitTests/UtilTests/BuiltInFunctionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public class BuiltInFunctionTest
new object[] { "/topic/edit/123s", "/topic/delete/[0-9]+", false }
};

public static IEnumerable<object[]> KeyGetTestData = new[]
{
new object[] { "/foo", "/foo", "" },
new object[] { "/foo", "/foo*", "" },
new object[] { "/foo", "/foo/*", "" },
new object[] { "/foo/bar", "/foo", "" },
new object[] { "/foo/bar", "/foo*", "/bar" },
new object[] { "/foo/bar", "/foo/*", "bar" },
new object[] { "/foobar", "/foo", "" },
new object[] { "/foobar", "/foo*", "bar" },
new object[] { "/foobar", "/foo/*", "" }
};

public static IEnumerable<object[]> keyMatchTestData = new[]
{
new object[] { "/foo", "/foo", true }, new object[] { "/foo", "/foo*", true },
Expand Down Expand Up @@ -140,6 +153,12 @@ public void TestRegexMatch(string key1, string key2, bool expectedResult) =>
Assert.Equal(expectedResult,
BuiltInFunctions.RegexMatch(key1, key2));

[Theory]
[MemberData(nameof(KeyGetTestData))]
public void TestKeyGet(string key1, string key2, string expectedResult) =>
Assert.Equal(expectedResult,
BuiltInFunctions.KeyGet(key1, key2));

[Theory]
[MemberData(nameof(keyMatchTestData))]
public void TestKeyMatch(string key1, string key2, bool expectedResult) =>
Expand Down
1 change: 1 addition & 0 deletions Casbin/Model/FunctionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal static FunctionMap LoadFunctionMap()
FunctionDict = new Dictionary<string, Delegate>()
};

map.AddFunction("keyGet", BuiltInFunctions.KeyGet);
map.AddFunction("keyMatch", BuiltInFunctions.KeyMatch);
map.AddFunction("keyMatch2", BuiltInFunctions.KeyMatch2);
map.AddFunction("keyMatch3", BuiltInFunctions.KeyMatch3);
Expand Down
22 changes: 22 additions & 0 deletions Casbin/Util/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ public static class BuiltInFunctions
private static readonly Regex s_keyMatch4Regex = new(@"\{([^/]+)\}");
private delegate bool GFunction(string subject1, string subject2, string domain = null);

/// <summary>
/// KeyGet returns the matched part
/// For example, "/foo/bar/foo" matches "/foo/*"
/// "bar/foo" will been returned
/// </summary>
/// <param name="key1">The first argument.</param>
/// <param name="key2">The second argument.</param>
/// <returns>Whether key1 matches key2.</returns>
public static string KeyGet(string key1, string key2)
{
int m = key1.Length, n = key2.Length;
Copy link
Member

Choose a reason for hiding this comment

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

m and n may not be the most suitable name, It is difficult to see the association with key1 or key2.

if(m < n) return "";
var span1 = key1.AsSpan();
var span2 = key2.AsSpan();
for (int i = 0; i < n; i++)
{
if(span1[i] != span2[i])
return span2[i] == '*' ? span1.Slice(i, m - i).ToString() : "";
}
return "";
}

/// <summary>
/// Determines whether key1 matches the pattern of key2 (similar to RESTful path),
/// key2 can contain a *. For example, "/foo/bar" matches "/foo/*".
Expand Down