Casbin uses a string array to store all policies, so we have ensured the request is always a string. In some situations, we need additional operations to "parse" request values: https://github.com/casbin/casbin/issues/868.
However, this does bring some benefits, all as strings can greatly reduce the complexity of development. like we can provide a clear signature for the custom function:
Request and policy value is like a read-only tuple. We can use the record Request<T1, T2 ...> type to define them. In order to deal with the different numbers and usage scenarios of policies and requests (the request will be much more than policy), so the request should be lower allocated than the policy instance and try best to avoid boxed-transform.
Policy Sample:
public record Policy<T1, T2, T3>(T1 Value1, T2 Value2, T3 Value3) : IPolicyValues
{
public string this[int index] => index switch
{
0 => Policy.ToString(Value1),
1 => Policy.ToString(Value2),
2 => Policy.ToString(Value3),
_ => throw new ArgumentOutOfRangeException(nameof(index))
};
public int Count => 3;
public IEnumerator<string> GetEnumerator() => new PolicyEnumerator<Policy<T1, T2, T3>>(this);
IEnumerator IEnumerable.GetEnumerator() => new PolicyEnumerator<Policy<T1, T2, T3>>(this);
};
Request sample:
public readonly record struct Request<T1, T2, T3>(T1 Value1, T2 Value2, T3 Value3) : IRequestValues
{
public int Count => 3;
public static implicit operator Request<T1, T2, T3>((T1, T2, T3) tuple)
{
return Request.Create(tuple.Item1, tuple.Item2, tuple.Item3);
}
public static implicit operator (T1, T2, T3)(Request<T1, T2, T3> r)
{
return (r.Value1, r.Value2, r.Value3);
}
}
Context and purposes
Casbin uses a string array to store all policies, so we have ensured the request is always a string. In some situations, we need additional operations to "parse" request values: https://github.com/casbin/casbin/issues/868. However, this does bring some benefits, all as strings can greatly reduce the complexity of development. like we can provide a clear signature for the custom function:
So this proposal does not completely make the parameters strongly typed, currently only for the following purposes :
Roadmap
Changes:
1. Generic request and policy values
Request and policy value is like a read-only tuple. We can use the
record Request<T1, T2 ...>
type to define them. In order to deal with the different numbers and usage scenarios of policies and requests (the request will be much more than policy), so the request should be lower allocated than the policy instance and try best to avoid boxed-transform.Policy Sample:
Request sample:
2. Expression and matcher
These will help implement a “real matcher”, we do not need the escaping: https://github.com/casbin/Casbin.NET/blob/bb8b51b43ed2c49f6e8ca70929f609a71cef2f74/NetCasbin/Util/StringUtil.cs#L18-L27
Sample test: