Skip to content

Commit

Permalink
fix: fix bugs in policy equal and persist (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
dacongda authored Apr 3, 2024
1 parent 9379fba commit 3306375
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Casbin/Abstractions/Model/IPolicyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface IPolicyValues : IList<string>
public string ToText();

public bool Equals(IPolicyValues other);

int GetRealCount();
}
20 changes: 19 additions & 1 deletion Casbin/Model/PolicyValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public record PolicyValues : IPolicyValues
public bool IsReadOnly => false;

int ICollection<string>.Count => Values.Count;


int IPolicyValues.GetRealCount() => Count;

This comment has been minimized.

Copy link
@sagilio0728

sagilio0728 Apr 4, 2024

Collaborator

Why was this method added, and under what circumstances would the Count property differ from the Count field?


string IList<string>.this[int index] { get => Values[index]; set => new NotImplementedException(); }

Expand All @@ -34,7 +37,22 @@ internal static string ToText(IEnumerable<string> values)
=> string.Join(PermConstants.PolicySeparatorString, values);

internal static bool Equals<T>(T values, T other) where T : IPolicyValues
=> values.Count == other.Count && values.ToText().Equals(other.ToText());
{
if (values.GetRealCount() != other.GetRealCount())
{
return false;
}

for(int i = 0; i< values.GetRealCount(); i++)
{
if (values[i] != other[i])
{
return false;
}
}

return true;
}

internal static string ToStringValue<T>(T value) => value as string ?? value.ToString();

Expand Down
4 changes: 2 additions & 2 deletions Casbin/Persist/PersistantPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static TPersistPolicy Create<TPersistPolicy>(string section, string type,
where TPersistPolicy : IPersistPolicy, new()
{
TPersistPolicy persistPolicy = new TPersistPolicy { Section = section, Type = type };
switch (values.Count)
switch (values.GetRealCount())
{
case 1:
persistPolicy.Value1 = values[0];
Expand Down Expand Up @@ -136,7 +136,7 @@ public static TPersistPolicy Create<TPersistPolicy>(string section, string type,
persistPolicy.Value12 = values[11];
break;
default:
throw new ArgumentOutOfRangeException(nameof(values), values.Count,
throw new ArgumentOutOfRangeException(nameof(values), values.GetRealCount(),
"The number of values must be between 1 and 12.");
}

Expand Down

0 comments on commit 3306375

Please sign in to comment.