[#30] Client: Add object model, use structs instead of classes
All checks were successful
DCO / DCO (pull_request) Successful in 25s
lint-build / dotnet8.0 (pull_request) Successful in 43s

Signed-off-by: Pavel Gross <p.gross@yadro.com>
This commit is contained in:
Pavel Gross 2025-02-12 17:03:55 +03:00
parent 13da97520d
commit a785357268
6 changed files with 100 additions and 51 deletions

View file

@ -1,8 +1,36 @@
namespace FrostFS.SDK.Client;
public class Actions
public struct Actions(bool inverted, string[] names) : System.IEquatable<Actions>
{
public bool Inverted { get; set; }
public bool Inverted { get; set; } = inverted;
public string[] Names { get; set; } = [];
public string[] Names { get; set; } = names;
public override bool Equals(object obj)
{
if (obj == null || obj is not Actions)
return false;
return Equals((Actions)obj);
}
public override readonly int GetHashCode()
{
return Inverted.GetHashCode() ^ string.Join(string.Empty, Names).GetHashCode();
}
public static bool operator ==(Actions left, Actions right)
{
return left.Equals(right);
}
public static bool operator !=(Actions left, Actions right)
{
return !(left == right);
}
public readonly bool Equals(Actions other)
{
return this.GetHashCode().Equals(other.GetHashCode());
}
}

View file

@ -1,6 +1,6 @@
namespace FrostFS.SDK.Client;
public class Condition
public struct Condition : System.IEquatable<Condition>
{
public ConditionType Op { get; set; }
@ -9,4 +9,35 @@ public class Condition
public string? Key { get; set; }
public string? Value { get; set; }
public override bool Equals(object obj)
{
if (obj == null || obj is not Condition)
return false;
return Equals((Condition)obj);
}
public override readonly int GetHashCode()
{
return Op.GetHashCode()
^ Kind.GetHashCode()
^ (Key != null ? Key.GetHashCode() : 0)
^ (Value != null ? Value.GetHashCode() : 0);
}
public static bool operator ==(Condition left, Condition right)
{
return left.Equals(right);
}
public static bool operator !=(Condition left, Condition right)
{
return !(left == right);
}
public readonly bool Equals(Condition other)
{
return this.GetHashCode().Equals(other.GetHashCode());
}
}

View file

@ -5,10 +5,10 @@ public class FrostFsRule
public RuleStatus Status { get; set; }
// Actions the operation is applied to.
public Actions? Actions { get; set; }
public Actions Actions { get; set; }
// List of the resources the operation is applied to.
public Resource? Resources { get; set; }
public Resource Resources { get; set; }
// True iff individual conditions must be combined with the logical OR.
// By default AND is used, so _each_ condition must pass.

View file

@ -1,8 +1,36 @@
namespace FrostFS.SDK.Client;
public class Resource
public struct Resource(bool inverted, string[] names) : System.IEquatable<Resource>
{
public bool Inverted { get; set; }
public bool Inverted { get; set; } = inverted;
public string[] Names { get; set; } = [];
public string[] Names { get; set; } = names;
public override bool Equals(object obj)
{
if (obj == null || obj is not Resource)
return false;
return Equals((Resource)obj);
}
public override readonly int GetHashCode()
{
return Inverted.GetHashCode() ^ string.Join(string.Empty, Names).GetHashCode();
}
public static bool operator ==(Resource left, Resource right)
{
return left.Equals(right);
}
public static bool operator !=(Resource left, Resource right)
{
return !(left == right);
}
public readonly bool Equals(Resource other)
{
return this.GetHashCode().Equals(other.GetHashCode());
}
}

View file

@ -90,33 +90,18 @@ internal static class RuleSerializer
private static int ActionsSize(Actions action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
return BoolSize // Inverted
+ SliceSize(action.Names, StringSize);
}
private static int ResourcesSize(Resource resource)
{
if (resource is null)
{
throw new ArgumentNullException(nameof(resource));
}
return BoolSize // Inverted
+ SliceSize(resource.Names, StringSize);
}
private static int ConditionSize(Condition condition)
{
if (condition is null)
{
throw new ArgumentNullException(nameof(condition));
}
return ByteSize // Op
+ ByteSize // Object
+ StringSize(condition.Key)
@ -250,11 +235,6 @@ internal static class RuleSerializer
private static int MarshalActions(byte[] buf, int offset, Actions action)
{
if (action is null)
{
throw new ArgumentNullException(nameof(action));
}
offset = BoolMarshal(buf, offset, action.Inverted);
return SliceMarshal(buf, offset, action.Names, StringMarshal);
@ -262,11 +242,6 @@ internal static class RuleSerializer
private static int MarshalCondition(byte[] buf, int offset, Condition condition)
{
if (condition is null)
{
throw new ArgumentNullException(nameof(condition));
}
offset = ByteMarshal(buf, offset, (byte)condition.Op);
offset = ByteMarshal(buf, offset, (byte)condition.Kind);
@ -285,9 +260,9 @@ internal static class RuleSerializer
offset = ByteMarshal(buf, offset, (byte)rule.Status);
offset = MarshalActions(buf, offset, rule.Actions!);
offset = MarshalActions(buf, offset, rule.Actions);
offset = MarshalResources(buf, offset, rule.Resources!);
offset = MarshalResources(buf, offset, rule.Resources);
offset = BoolMarshal(buf, offset, rule.Any);
@ -296,11 +271,6 @@ internal static class RuleSerializer
private static int MarshalResources(byte[] buf, int offset, Resource resources)
{
if (resources is null)
{
throw new ArgumentNullException(nameof(resources));
}
offset = BoolMarshal(buf, offset, resources.Inverted);
return SliceMarshal(buf, offset, resources.Names, StringMarshal);

View file

@ -723,16 +723,8 @@ public class SmokeClientTests : SmokeTestsBase
new FrostFsRule
{
Status = RuleStatus.Allow,
Actions = new Actions
{
Inverted = false,
Names =["*"]
},
Resources = new Resource
{
Inverted = false,
Names = [$"native:object/*"]
},
Actions = new Actions(inverted: false, names: ["*"]),
Resources = new Resource (inverted: false, names: [$"native:object/*"]),
Any = false,
Conditions = []
}