Skip to content

Commit 7469d2d

Browse files
committed
Refactor and optimize code
1 parent c281f8f commit 7469d2d

35 files changed

+130
-107
lines changed

JsonSchema/RelogicLabs/JsonSchema/Collections/IIndexMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace RelogicLabs.JsonSchema.Collections;
22

33
public interface IIndexMap<TK, TV> : IList<TV>
4-
where TV : IKeyer<TK> where TK : notnull
4+
where TV : IKeyed<TK> where TK : notnull
55
{
66
TV this[TK key] { get; }
77
IEnumerable<TK> Keys { get; }

JsonSchema/RelogicLabs/JsonSchema/Collections/IKeyer.cs renamed to JsonSchema/RelogicLabs/JsonSchema/Collections/IKeyed.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace RelogicLabs.JsonSchema.Collections;
22

3-
public interface IKeyer<out TK>
3+
public interface IKeyed<out TK>
44
{
55
TK GetKey();
66
}

JsonSchema/RelogicLabs/JsonSchema/Collections/IndexHashMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace RelogicLabs.JsonSchema.Collections;
55

66
public sealed class IndexHashMap<TK, TV> : IIndexMap<TK, TV>
7-
where TV : IKeyer<TK> where TK : notnull
7+
where TV : IKeyed<TK> where TK : notnull
88
{
99
private IDictionary<TK, TV> _dictionary;
1010
private IList<TV> _list;

JsonSchema/RelogicLabs/JsonSchema/JsonAssert.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ public class JsonAssert
2121
public JsonAssert(string schema) : this(schema, SCHEMA_TREE) { }
2222

2323
/// <summary>
24-
/// Initializes a new instance of the <see cref="JsonAssert"/> class for the
25-
/// specified <paramref name="expected"/> string which can be either a Schema or a JSON
26-
/// representation.
24+
/// Initializes a new instance of the <see cref="JsonAssert"/> class for the specified
25+
/// <paramref name="expected"/> string which can be either a Schema or a JSON representation.
2726
/// </summary>
2827
/// <param name="expected">An expected Schema or JSON string for validation or conformation.</param>
2928
/// <param name="type">The type of string provided by <paramref name="expected"/>, indicating

JsonSchema/RelogicLabs/JsonSchema/Time/JsonDateTime.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class JsonDateTime
1313
private const int DEFAULT_MINUTE = 0;
1414
private const int DEFAULT_SECOND = 0;
1515
private const int DEFAULT_FRACTION = 0;
16-
private const int DEFAULT_UTC_HOUR = 0;
17-
private const int DEFAULT_UTC_MINUTE = 0;
16+
private const int DEFAULT_UTC_OFFSET_HOUR = 0;
17+
private const int DEFAULT_UTC_OFFSET_MINUTE = 0;
1818

1919
public const int UNSET = -1000;
2020

@@ -26,15 +26,15 @@ public class JsonDateTime
2626
public int Minute { get; }
2727
public int Second { get; }
2828
public int Fraction { get; }
29-
public int UtcHour { get; }
30-
public int UtcMinute { get; }
29+
public int UtcOffsetHour { get; }
30+
public int UtcOffsetMinute { get; }
3131

3232
private DateTimeOffset _dateTimeOffset;
3333
private TimeSpan _utcOffset;
3434

3535
internal JsonDateTime(DateTimeType type, int year = UNSET, int month = UNSET,
3636
int day = UNSET, int hour = UNSET, int minute = UNSET, int second = UNSET,
37-
int fraction = UNSET, int utcHour = UNSET, int utcMinute = UNSET)
37+
int fraction = UNSET, int utcOffsetHour = UNSET, int utcOffsetMinute = UNSET)
3838
{
3939
Type = type;
4040
Year = year;
@@ -44,12 +44,12 @@ internal JsonDateTime(DateTimeType type, int year = UNSET, int month = UNSET,
4444
Minute = minute;
4545
Second = second;
4646
Fraction = fraction;
47-
UtcHour = utcHour;
48-
UtcMinute = utcMinute;
47+
UtcOffsetHour = utcOffsetHour;
48+
UtcOffsetMinute = utcOffsetMinute;
4949

5050
_utcOffset = new TimeSpan(
51-
DefaultIfUnset(utcHour, DEFAULT_UTC_HOUR),
52-
DefaultIfUnset(utcMinute, DEFAULT_UTC_MINUTE), 0);
51+
DefaultIfUnset(utcOffsetHour, DEFAULT_UTC_OFFSET_HOUR),
52+
DefaultIfUnset(utcOffsetMinute, DEFAULT_UTC_OFFSET_MINUTE), 0);
5353

5454
_dateTimeOffset = new DateTimeOffset(
5555
DefaultIfUnset(year, DEFAULT_YEAR),
@@ -64,7 +64,6 @@ internal JsonDateTime(DateTimeType type, int year = UNSET, int month = UNSET,
6464
private static int DefaultIfUnset(int value, int defaultValue)
6565
=> value == UNSET ? defaultValue : value;
6666

67-
6867
private static bool IsAllSet(params int[] values)
6968
=> values.All(value => value != UNSET);
7069

@@ -85,6 +84,13 @@ public int Compare(JsonDateTime other)
8584
return result;
8685
}
8786

87+
internal JDateTime CreateNode(JString dateTime)
88+
{
89+
if(Type == DATE_TYPE) return new JDate(dateTime, this);
90+
if(Type == TIME_TYPE) return new JTime(dateTime, this);
91+
throw new InvalidOperationException("Invalid date time type");
92+
}
93+
8894
public override string ToString()
8995
{
9096
StringBuilder builder = new("{");
@@ -95,17 +101,10 @@ public override string ToString()
95101
if(Minute != UNSET) builder.Append($"Minute: {Minute}, ");
96102
if(Second != UNSET) builder.Append($"Second: {Second}, ");
97103
if(Fraction != UNSET) builder.Append($"Fraction: {Fraction}, ");
98-
if(UtcHour != UNSET) builder.Append($"UTC Offset Hour: {UtcHour}, ");
99-
if(UtcMinute != UNSET) builder.Append($"UTC Offset Minute: {UtcMinute}, ");
104+
if(UtcOffsetHour != UNSET) builder.Append($"UTC Offset Hour: {UtcOffsetHour}, ");
105+
if(UtcOffsetMinute != UNSET) builder.Append($"UTC Offset Minute: {UtcOffsetMinute}, ");
100106
var result = builder.ToString();
101107
if(result.EndsWith(", ")) result = result[..^2];
102108
return result + "}";
103109
}
104-
105-
internal JDateTime Create(JString dateTime)
106-
{
107-
if(Type == DATE_TYPE) return new JDate(dateTime, this);
108-
if(Type == TIME_TYPE) return new JTime(dateTime, this);
109-
throw new InvalidOperationException("Invalid date time type");
110-
}
111110
}

JsonSchema/RelogicLabs/JsonSchema/Tree/FunctionCache.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using RelogicLabs.JsonSchema.Types;
3+
using static RelogicLabs.JsonSchema.Utilities.CommonUtilities;
34

45
namespace RelogicLabs.JsonSchema.Tree;
56

@@ -8,19 +9,17 @@ internal class FunctionCache : IEnumerable<FunctionCache.Entry>
89
public record Entry(MethodPointer MethodPointer, object?[] Arguments)
910
{
1011
public bool IsTargetMatch(JNode target) => MethodPointer.Parameters[0]
11-
.ParameterType.IsInstanceOfType(target.Derived);
12+
.ParameterType.IsInstanceOfType(GetDerived(target));
1213

1314
public object Invoke(JFunction function, JNode target)
1415
{
15-
Arguments[0] = target.Derived;
16+
Arguments[0] = GetDerived(target);
1617
return MethodPointer.Invoke(function, Arguments);
1718
}
1819
}
1920

2021
public static int SizeLimit { get; set; } = 10;
21-
private List<Entry> _cache;
22-
23-
public FunctionCache() => _cache = new List<Entry>(SizeLimit);
22+
private List<Entry> _cache = new(SizeLimit);
2423

2524
public void Add(MethodPointer methodPointer, object?[] arguments)
2625
{

JsonSchema/RelogicLabs/JsonSchema/Tree/FunctionRegistry.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using RelogicLabs.JsonSchema.Utilities;
77
using static RelogicLabs.JsonSchema.Message.ContextDetail;
88
using static RelogicLabs.JsonSchema.Message.ErrorCode;
9+
using static RelogicLabs.JsonSchema.Utilities.CommonUtilities;
910

1011
namespace RelogicLabs.JsonSchema.Tree;
1112

@@ -116,12 +117,12 @@ private static int GetParameterCount(ICollection<ParameterInfo> parameters)
116117
}
117118

118119
private static bool IsMatch(ParameterInfo parameter, JNode argument)
119-
=> parameter.ParameterType.IsInstanceOfType(argument.Derived);
120+
=> parameter.ParameterType.IsInstanceOfType(GetDerived(argument));
120121

121122
private static bool IsParams(ParameterInfo parameter)
122123
=> parameter.IsDefined(typeof(ParamArrayAttribute), false);
123124

124-
private bool HandleValidator(object result)
125+
private bool HandleFuture(object result)
125126
{
126127
return result is FutureValidator validator
127128
? _runtime.AddValidator(validator)
@@ -133,7 +134,7 @@ public bool InvokeFunction(JFunction function, JNode target)
133134
foreach(var e in function.Cache)
134135
{
135136
if(e.IsTargetMatch(target))
136-
return HandleValidator(e.Invoke(function, target));
137+
return HandleFuture(e.Invoke(function, target));
137138
}
138139
var methods = GetMethods(function);
139140
ParameterInfo? mismatchParameter = null;
@@ -149,7 +150,7 @@ public bool InvokeFunction(JFunction function, JNode target)
149150
object?[] allArgs = AddTarget(schemaArgs, target).ToArray();
150151
var result = method.Invoke(function, allArgs);
151152
function.Cache.Add(method, allArgs);
152-
return HandleValidator(result);
153+
return HandleFuture(result);
153154
}
154155
mismatchParameter = _parameters[0];
155156
}
@@ -167,7 +168,7 @@ public bool InvokeFunction(JFunction function, JNode target)
167168

168169
private static List<object> AddTarget(List<object> arguments, JNode target)
169170
{
170-
arguments.Insert(0, target.Derived);
171+
arguments.Insert(0, GetDerived(target));
171172
return arguments;
172173
}
173174

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using RelogicLabs.JsonSchema.Types;
2+
3+
namespace RelogicLabs.JsonSchema.Tree;
4+
5+
public class ReceiverRegistry
6+
{
7+
public Dictionary<JReceiver, List<JNode>> Receivers { get; } = new();
8+
9+
public void Register(IEnumerable<JReceiver> receivers)
10+
{
11+
foreach(var r in receivers) Receivers[r] = new List<JNode>();
12+
}
13+
14+
public void Receive(IEnumerable<JReceiver> receivers, JNode node)
15+
{
16+
foreach(var r in receivers) Receivers[r].Add(node);
17+
}
18+
19+
public List<JNode>? Fetch(JReceiver receiver)
20+
{
21+
Receivers.TryGetValue(receiver, out var list);
22+
return list;
23+
}
24+
25+
public void Clear()
26+
{
27+
foreach(var pair in Receivers) pair.Value.Clear();
28+
}
29+
}

JsonSchema/RelogicLabs/JsonSchema/Tree/RuntimeContext.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public sealed class RuntimeContext
1313
public Dictionary<JAlias, JValidator> Definitions { get; }
1414
public ExceptionRegistry Exceptions { get; }
1515
public Dictionary<string, FutureValidator> Validators { get; }
16-
public Dictionary<JReceiver, List<JNode>> Receivers { get; }
16+
public ReceiverRegistry Receivers { get; }
1717
public Dictionary<string, object> Storage { get; }
1818
internal MessageFormatter MessageFormatter { get; }
1919

@@ -25,7 +25,7 @@ internal RuntimeContext(MessageFormatter messageFormatter, bool throwException)
2525
Definitions = new Dictionary<JAlias, JValidator>();
2626
Exceptions = new ExceptionRegistry(throwException);
2727
Validators = new Dictionary<string, FutureValidator>();
28-
Receivers = new Dictionary<JReceiver, List<JNode>>();
28+
Receivers = new ReceiverRegistry();
2929
Storage = new Dictionary<string, object>();
3030
}
3131

@@ -51,22 +51,6 @@ internal bool FailWith(Exception exception)
5151
return false;
5252
}
5353

54-
internal void Register(IEnumerable<JReceiver> receivers)
55-
{
56-
foreach(var r in receivers) Receivers[r] = new List<JNode>();
57-
}
58-
59-
internal void Receive(IEnumerable<JReceiver> receivers, JNode node)
60-
{
61-
foreach(var r in receivers) Receivers[r].Add(node);
62-
}
63-
64-
internal List<JNode>? Fetch(JReceiver receiver)
65-
{
66-
Receivers.TryGetValue(receiver, out var list);
67-
return list;
68-
}
69-
7054
public bool AddValidator(FutureValidator validator)
7155
=> Validators.TryAdd(Guid.NewGuid().ToString(), validator);
7256

@@ -81,6 +65,6 @@ public void Clear()
8165
{
8266
Exceptions.Clear();
8367
Storage.Clear();
84-
foreach(var pair in Receivers) pair.Value.Clear();
68+
Receivers.Clear();
8569
}
8670
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace RelogicLabs.JsonSchema.Types;
2+
3+
public interface IDerived
4+
{
5+
public JNode? Derived { get; set; }
6+
}

0 commit comments

Comments
 (0)