Skip to content

Commit

Permalink
added converters for all(ish) rules
Browse files Browse the repository at this point in the history
  • Loading branch information
gregsdennis committed Jul 22, 2022
1 parent 0de753b commit 761aea6
Show file tree
Hide file tree
Showing 34 changed files with 794 additions and 69 deletions.
6 changes: 3 additions & 3 deletions JsonLogic/Rules/AddRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ internal class AddRuleJsonConverter : JsonConverter<AddRule>
{
public override AddRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<List<Rule>>(ref reader, options);
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters == null || parameters.Count == 0)
if (parameters == null || parameters.Length == 0)
throw new JsonException("The + rule needs an array of parameters.");

return new AddRule(parameters[0], parameters.Skip(1).ToArray());
Expand All @@ -67,4 +67,4 @@ public override void Write(Utf8JsonWriter writer, AddRule value, JsonSerializerO
{
throw new NotImplementedException();
}
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/AllRule.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

/// <summary>
/// Handles the `all` operation.
/// </summary>
[Operator("all")]
[JsonConverter(typeof(AllRuleJsonConverter))]
public class AllRule : Rule
{
private readonly Rule _input;
Expand Down Expand Up @@ -38,4 +42,22 @@ internal AllRule(Rule input, Rule rule)
return (results.Any() &&
results.All(result => result.IsTruthy()));
}
}
}

internal class AllRuleJsonConverter : JsonConverter<AllRule>
{
public override AllRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not { Length:2})
throw new JsonException("The all rule needs an array with 2 parameters.");

return new AllRule(parameters[0], parameters[1]);
}

public override void Write(Utf8JsonWriter writer, AllRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
25 changes: 23 additions & 2 deletions JsonLogic/Rules/AndRule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

Expand Down Expand Up @@ -39,4 +42,22 @@ internal AndRule(Rule a, params Rule[] more)

return first;
}
}
}

internal class AndRuleJsonConverter : JsonConverter<AndRule>
{
public override AndRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters == null || parameters.Length == 0)
throw new JsonException("The and rule needs an array of parameters.");

return new AndRule(parameters[0], parameters.Skip(1).ToArray());
}

public override void Write(Utf8JsonWriter writer, AndRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/BooleanCastRule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Text.Json.Nodes;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

/// <summary>
/// Handles the `!!` operation.
/// </summary>
[Operator("!!")]
[JsonConverter(typeof(BooleanCastRuleJsonConverter))]
public class BooleanCastRule : Rule
{
private readonly Rule _value;
Expand All @@ -30,4 +34,22 @@ internal BooleanCastRule(Rule value)

return _value.Apply(data, contextData).IsTruthy();
}
}
}

internal class BooleanCastRuleJsonConverter : JsonConverter<BooleanCastRule>
{
public override BooleanCastRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not { Length: 1 })
throw new JsonException("The !! rule needs an array with a single parameter.");

return new BooleanCastRule(parameters[0]);
}

public override void Write(Utf8JsonWriter writer, BooleanCastRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/CatRule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

Expand Down Expand Up @@ -41,4 +45,22 @@ internal CatRule(Rule a, params Rule[] more)

return result;
}
}
}

internal class CatRuleJsonConverter : JsonConverter<CatRule>
{
public override CatRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters == null || parameters.Length == 0)
throw new JsonException("The cat rule needs an array of parameters.");

return new CatRule(parameters[0], parameters.Skip(1).ToArray());
}

public override void Write(Utf8JsonWriter writer, CatRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/DivideRule.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Text.Json.Nodes;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

/// <summary>
/// Handles the `/` operation.
/// </summary>
[Operator("/")]
[JsonConverter(typeof(DivideRuleJsonConverter))]
public class DivideRule : Rule
{
private readonly Rule _a;
Expand Down Expand Up @@ -42,4 +46,22 @@ internal DivideRule(Rule a, Rule b)

return numberA.Value / numberB.Value;
}
}
}

internal class DivideRuleJsonConverter : JsonConverter<DivideRule>
{
public override DivideRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not { Length: 2 })
throw new JsonException("The / rule needs an array with 2 parameters.");

return new DivideRule(parameters[0], parameters[1]);
}

public override void Write(Utf8JsonWriter writer, DivideRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/FilterRule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Json.More;

namespace Json.Logic.Rules;
Expand All @@ -8,6 +11,7 @@ namespace Json.Logic.Rules;
/// Handles the `filter` operation.
/// </summary>
[Operator("filter")]
[JsonConverter(typeof(FilterRuleJsonConverter))]
public class FilterRule : Rule
{
private readonly Rule _input;
Expand Down Expand Up @@ -37,4 +41,22 @@ internal FilterRule(Rule input, Rule rule)

return arr.Where(i => _rule.Apply(data, i).IsTruthy()).ToJsonArray();
}
}
}

internal class FilterRuleJsonConverter : JsonConverter<FilterRule>
{
public override FilterRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not { Length: 2 })
throw new JsonException("The filter rule needs an array with 2 parameters.");

return new FilterRule(parameters[0], parameters[1]);
}

public override void Write(Utf8JsonWriter writer, FilterRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
21 changes: 20 additions & 1 deletion JsonLogic/Rules/IfRule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Json.Logic.Rules;

Expand Down Expand Up @@ -69,4 +71,21 @@ internal IfRule(params Rule[] components)

throw new NotImplementedException("Something went wrong. This shouldn't happen.");
}
}
}

internal class IfRuleJsonConverter : JsonConverter<IfRule>
{
public override IfRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters == null) return new IfRule();

return new IfRule(parameters);
}

public override void Write(Utf8JsonWriter writer, IfRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
26 changes: 24 additions & 2 deletions JsonLogic/Rules/InRule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Json.More;

namespace Json.Logic.Rules;
Expand All @@ -8,6 +11,7 @@ namespace Json.Logic.Rules;
/// Handles the `in` operation.
/// </summary>
[Operator("in")]
[JsonConverter(typeof(InRuleJsonConverter))]
public class InRule : Rule
{
private readonly Rule _test;
Expand Down Expand Up @@ -48,4 +52,22 @@ internal InRule(Rule test, Rule source)

return false;
}
}
}

internal class InRuleJsonConverter : JsonConverter<InRule>
{
public override InRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not { Length: 2 })
throw new JsonException("The in rule needs an array with 2 parameters.");

return new InRule(parameters[0], parameters[1]);
}

public override void Write(Utf8JsonWriter writer, InRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
27 changes: 25 additions & 2 deletions JsonLogic/Rules/LessThanEqualRule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Text.Json.Nodes;
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Json.More;
#pragma warning disable CS1570

Expand Down Expand Up @@ -65,4 +68,24 @@ internal LessThanEqualRule(Rule a, Rule b, Rule c)

return low <= value && value <= high;
}
}
}

internal class LessThanEqualRuleJsonConverter : JsonConverter<LessThanEqualRule>
{
public override LessThanEqualRule? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var parameters = JsonSerializer.Deserialize<Rule[]>(ref reader, options);

if (parameters is not ({ Length: 2 } or { Length: 3 }))
throw new JsonException("The <= rule needs an array with either 2 or 3 parameters.");

if (parameters.Length == 2) return new LessThanEqualRule(parameters[0], parameters[1]);

return new LessThanEqualRule(parameters[0], parameters[1], parameters[2]);
}

public override void Write(Utf8JsonWriter writer, LessThanEqualRule value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
Loading

0 comments on commit 761aea6

Please sign in to comment.