Skip to content

Commit 1f629ac

Browse files
committed
Update syntax tree structure
1 parent ad063b1 commit 1f629ac

26 files changed

+149
-153
lines changed

JsonSchema/RelogicLabs/JsonSchema/Types/IJsonComposite.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

JsonSchema/RelogicLabs/JsonSchema/Types/IJsonFloat.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace RelogicLabs.JsonSchema.Types;
22

3-
public interface IJsonType<out T> where T : JNode
3+
public interface IJsonType
44
{
5-
public T Node => (T) this;
65
public JsonType Type { get; }
6+
public JNode Node { get; }
77
}

JsonSchema/RelogicLabs/JsonSchema/Types/JAlias.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ public override bool Equals(object? obj)
2828
}
2929

3030
public override int GetHashCode() => Name.GetHashCode();
31-
public override string ToJson() => Name;
32-
public override string ToString() => ToJson();
31+
public override string ToString() => Name;
3332
}

JsonSchema/RelogicLabs/JsonSchema/Types/JArray.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
namespace RelogicLabs.JsonSchema.Types;
88

9-
public class JArray : JBranch, IJsonType<JArray>, IJsonComposite
9+
public class JArray : JComposite
1010
{
11-
public JsonType Type => JsonType.ARRAY;
1211
public required IList<JNode> Elements { get; init; }
1312
public override IEnumerable<JNode> Children => Elements;
14-
13+
1514
internal JArray(IDictionary<JNode, JNode> relations) : base(relations) { }
1615

1716
public override bool Match(JNode node)
@@ -31,8 +30,8 @@ public override bool Match(JNode node)
3130
}
3231
return result;
3332
}
34-
35-
public IList<JNode> ExtractComponents() => Elements;
36-
public override string ToJson() => $"[{Elements.ToJson().ToString(", ")}]";
37-
public override string ToString() => ToJson();
33+
34+
public override JsonType Type => JsonType.ARRAY;
35+
public override IList<JNode> GetComponents() => Elements;
36+
public override string ToString() => Elements.ToString(", ", "[", "]");
3837
}

JsonSchema/RelogicLabs/JsonSchema/Types/JBoolean.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
namespace RelogicLabs.JsonSchema.Types;
77

8-
public class JBoolean : JPrimitive, IJsonType<JBoolean>, IPragmaValue<bool>
8+
public class JBoolean : JPrimitive, IPragmaValue<bool>
99
{
10-
public JsonType Type => JsonType.BOOLEAN;
1110
public required bool Value { get; init; }
1211
internal JBoolean(IDictionary<JNode, JNode> relations) : base(relations) { }
1312

@@ -31,7 +30,7 @@ public override bool Equals(object? obj)
3130
return Value == other.Value;
3231
}
3332

33+
public override JsonType Type => JsonType.BOOLEAN;
3434
public override int GetHashCode() => Value.GetHashCode();
35-
public override string ToJson() => Value.ToString().ToLower();
36-
public override string ToString() => ToJson();
35+
public override string ToString() => Value.ToString().ToLower();
3736
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace RelogicLabs.JsonSchema.Types;
2+
3+
public abstract class JComposite : JBranch, IJsonType
4+
{
5+
protected JComposite(IDictionary<JNode, JNode> relations) : base(relations) { }
6+
public abstract IList<JNode> GetComponents();
7+
public virtual JsonType Type => JsonType.ANY;
8+
public JNode Node => this;
9+
}

JsonSchema/RelogicLabs/JsonSchema/Types/JDataType.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ internal override JNode Initialize()
2828
public override bool Match(JNode node)
2929
{
3030
if(!Nested) return JsonType.Match(node);
31-
if(node is not IJsonComposite composite) return FailWith(
31+
if(node is not JComposite composite) return FailWith(
3232
new JsonSchemaException(
33-
new ErrorDetail(DTYP01, InvalidNestedDataType),
33+
new ErrorDetail(DTYP02, InvalidNestedDataType),
3434
ExpectedDetail.AsInvalidDataType(this),
3535
ActualDetail.AsInvalidDataType(node)));
36-
IList<JNode> components = composite.ExtractComponents();
36+
IList<JNode> components = composite.GetComponents();
3737
return components.Select(MatchCurrent).AllTrue();
3838
}
3939

@@ -52,20 +52,20 @@ private bool MatchCurrent(JNode node)
5252
internal bool MatchForReport(JNode node)
5353
{
5454
if(!Nested && !JsonType.Match(node)) return FailWith(
55-
new JsonSchemaException(new ErrorDetail(DTYP02, DataTypeMismatch),
55+
new JsonSchemaException(new ErrorDetail(DTYP04, DataTypeMismatch),
5656
ExpectedDetail.AsDataTypeMismatch(this),
5757
ActualDetail.AsDataTypeMismatch(node)));
58-
if(node is not IJsonComposite composite) return FailWith(
58+
if(node is not JComposite composite) return FailWith(
5959
new JsonSchemaException(
60-
new ErrorDetail(DTYP01, InvalidNestedDataType),
60+
new ErrorDetail(DTYP05, InvalidNestedDataType),
6161
ExpectedDetail.AsInvalidDataType(this),
6262
ActualDetail.AsInvalidDataType(node)));
63-
IList<JNode> components = composite.ExtractComponents();
63+
IList<JNode> components = composite.GetComponents();
6464
bool result = true;
6565
foreach(var c in components)
6666
{
6767
if(!MatchCurrent(c)) result &= FailWith(new JsonSchemaException(
68-
new ErrorDetail(DTYP02, DataTypeMismatch),
68+
new ErrorDetail(DTYP06, DataTypeMismatch),
6969
ExpectedDetail.AsDataTypeMismatch(this),
7070
ActualDetail.AsDataTypeMismatch(c)));
7171
}
@@ -82,15 +82,14 @@ public override bool Equals(object? obj)
8282
}
8383

8484
internal bool IsMatchNull() => !Nested && JsonType == JsonType.NULL;
85-
public bool IsApplicable(JNode node) => !Nested || node is IJsonComposite;
85+
public bool IsApplicable(JNode node) => !Nested || node is JComposite;
8686
public override int GetHashCode() => JsonType.GetHashCode();
87-
public override string ToJson()
87+
public override string ToString() => ToString(false);
88+
public string ToString(bool baseForm)
8889
{
89-
var builder = new StringBuilder(JsonType.ToString());
90-
if(Nested) builder.Append(INestedMode.NestedMarker);
91-
if(Alias != null) builder.Append($"({Alias.Name})");
90+
StringBuilder builder = new(JsonType.ToString());
91+
if(Nested && !baseForm) builder.Append(INestedMode.NestedMarker);
92+
if(Alias != null && !baseForm) builder.Append($"({Alias})");
9293
return builder.ToString();
9394
}
94-
95-
public override string ToString() => ToJson();
9695
}

JsonSchema/RelogicLabs/JsonSchema/Types/JDefinition.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ public override IEnumerable<JNode> Children
1010

1111
internal JDefinition(IDictionary<JNode, JNode> relations) : base(relations) { }
1212
internal override JDefinition Initialize() => (JDefinition) base.Initialize();
13-
public override string ToJson() => $"{DefineMarker} {Alias.Name} {Validator}";
14-
public override string ToString() => ToJson();
13+
public override string ToString() => $"{DefineMarker} {Alias} {Validator}";
1514
}

JsonSchema/RelogicLabs/JsonSchema/Types/JDouble.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
namespace RelogicLabs.JsonSchema.Types;
77

8-
public class JDouble : JNumber, IJsonFloat, IPragmaValue<double>
8+
public class JDouble : JNumber, IPragmaValue<double>
99
{
1010
public required double Value { get; init; }
11-
public override JsonType Type => JsonType.DOUBLE;
12-
11+
1312
internal JDouble(IDictionary<JNode, JNode> relations) : base(relations) { }
1413
public override bool Match(JNode node)
1514
{
1615
var other = CastType<JDouble>(node);
1716
if(other == null) return false;
18-
if (IsEquivalent(Value, other.Value)) return true;
17+
if (AreEqual(Value, other.Value)) return true;
1918
return FailWith(new JsonSchemaException(
2019
new ErrorDetail(DUBL01, ValueMismatch),
2120
ExpectedDetail.AsValueMismatch(this),
@@ -28,12 +27,12 @@ public override bool Equals(object? obj)
2827
if(ReferenceEquals(this, obj)) return true;
2928
if(obj.GetType() != this.GetType()) return false;
3029
JDouble other = (JDouble) obj;
31-
return IsEquivalent(Value, other.Value);
30+
return AreEqual(Value, other.Value);
3231
}
3332

33+
public override JsonType Type => JsonType.DOUBLE;
3434
public static implicit operator double(JDouble @double) => @double.Value;
3535
public override int GetHashCode() => Value.GetHashCode();
3636
protected override double ToDouble() => Convert.ToDouble(Value);
37-
public override string ToJson() => $"{Value:0.###############E+0}";
38-
public override string ToString() => ToJson();
37+
public override string ToString() => $"{Value:0.###############E+0}";
3938
}

0 commit comments

Comments
 (0)