Skip to content

Commit 1670284

Browse files
committed
Update and optimize pragma handlers
1 parent f28b235 commit 1670284

File tree

4 files changed

+78
-33
lines changed

4 files changed

+78
-33
lines changed

JsonSchema/RelogicLabs/JsonSchema/Tree/PragmaDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal abstract class PragmaDescriptor
99
public static readonly PragmaProfile<bool> IgnoreUndefinedProperties
1010
= new(nameof(IgnoreUndefinedProperties), typeof(JBoolean), false);
1111
public static readonly PragmaProfile<double> FloatingPointTolerance
12-
= new(nameof(FloatingPointTolerance), typeof(JNumber), 1E-7);
12+
= new(nameof(FloatingPointTolerance), typeof(JNumber), 1E-10);
1313
public static readonly PragmaProfile<bool> IgnoreObjectPropertyOrder
1414
= new(nameof(IgnoreObjectPropertyOrder), typeof(JBoolean), true);
1515

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using RelogicLabs.JsonSchema.Exceptions;
2+
using RelogicLabs.JsonSchema.Message;
3+
using RelogicLabs.JsonSchema.Types;
4+
using static RelogicLabs.JsonSchema.Message.ErrorCode;
5+
6+
namespace RelogicLabs.JsonSchema.Tree;
7+
8+
internal class PragmaManager
9+
{
10+
public const string IGNORE_UNDEFINED_PROPERTIES = "IgnoreUndefinedProperties";
11+
public const string FLOATING_POINT_TOLERANCE = "FloatingPointTolerance";
12+
public const string IGNORE_OBJECT_PROPERTY_ORDER = "IgnoreObjectPropertyOrder";
13+
14+
private readonly Dictionary<string, JPragma> _pragmas = new();
15+
16+
public bool IgnoreUndefinedProperties { get; private set; }
17+
= PragmaDescriptor.IgnoreUndefinedProperties.DefaultValue;
18+
public double FloatingPointTolerance { get; private set; }
19+
= PragmaDescriptor.FloatingPointTolerance.DefaultValue;
20+
public bool IgnoreObjectPropertyOrder { get; private set; }
21+
= PragmaDescriptor.IgnoreObjectPropertyOrder.DefaultValue;
22+
23+
public JPragma AddPragma(JPragma pragma) {
24+
if(_pragmas.ContainsKey(pragma.Name))
25+
throw new DuplicatePragmaException(MessageFormatter.FormatForSchema(
26+
PRAG03, $"Duplication found for {pragma.GetOutline()}", pragma.Context));
27+
_pragmas.Add(pragma.Name, pragma);
28+
SetPragmaValue(pragma.Name, pragma.Value);
29+
return pragma;
30+
}
31+
32+
private void SetPragmaValue(string name, JPrimitive value) {
33+
switch(name) {
34+
case IGNORE_UNDEFINED_PROPERTIES:
35+
IgnoreUndefinedProperties = ((IPragmaValue<bool>) value).Value;
36+
break;
37+
case FLOATING_POINT_TOLERANCE:
38+
FloatingPointTolerance = ((IPragmaValue<double>) value).Value;
39+
break;
40+
case IGNORE_OBJECT_PROPERTY_ORDER:
41+
IgnoreObjectPropertyOrder = ((IPragmaValue<bool>) value).Value;
42+
break;
43+
}
44+
}
45+
46+
public T GetPragmaValue<T>(string name)
47+
{
48+
var entry = PragmaDescriptor.From(name);
49+
_pragmas.TryGetValue(entry!.Name, out var pragma);
50+
return pragma == null
51+
? ((PragmaProfile<T>) entry).DefaultValue
52+
: ((IPragmaValue<T>) pragma.Value).Value;
53+
}
54+
55+
public JPragma? GetPragma(string name) {
56+
var entry = PragmaDescriptor.From(name);
57+
_pragmas.TryGetValue(entry!.Name, out var pragma);
58+
return pragma;
59+
}
60+
}

JsonSchema/RelogicLabs/JsonSchema/Tree/RuntimeContext.cs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,38 @@ namespace RelogicLabs.JsonSchema.Tree;
88
public class RuntimeContext
99
{
1010
private readonly FunctionManager _functionManager;
11-
11+
private readonly PragmaManager _pragmaManager;
12+
1213
internal MessageFormatter MessageFormatter { get; set; }
1314
public bool ThrowException { get; set; }
14-
public Dictionary<string, JPragma> Pragmas { get; }
1515
public Dictionary<JAlias, JValidator> Definitions { get; }
1616
public Queue<Exception> Exceptions { get; }
17-
18-
public bool IgnoreUndefinedProperties
19-
=> GetPragmaValue<bool>(nameof(IgnoreUndefinedProperties));
20-
public double FloatingPointTolerance
21-
=> GetPragmaValue<double>(nameof(FloatingPointTolerance));
22-
public bool IgnoreObjectPropertyOrder
23-
=> GetPragmaValue<bool>(nameof(IgnoreObjectPropertyOrder));
17+
18+
public bool IgnoreUndefinedProperties => _pragmaManager.IgnoreUndefinedProperties;
19+
public double FloatingPointTolerance => _pragmaManager.FloatingPointTolerance;
20+
public bool IgnoreObjectPropertyOrder => _pragmaManager.IgnoreObjectPropertyOrder;
21+
2422

2523
internal RuntimeContext(MessageFormatter messageFormatter, bool throwException)
2624
{
2725
_functionManager = new FunctionManager(this);
26+
_pragmaManager = new PragmaManager();
2827
MessageFormatter = messageFormatter;
2928
ThrowException = throwException;
30-
Pragmas = new Dictionary<string, JPragma>();
3129
Definitions = new Dictionary<JAlias, JValidator>();
3230
Exceptions = new Queue<Exception>();
3331
}
34-
35-
private T GetPragmaValue<T>(string name)
36-
{
37-
var entry = PragmaDescriptor.From(name);
38-
Pragmas.TryGetValue(entry!.Name, out var pragma);
39-
return pragma == null ? ((PragmaProfile<T>) entry).DefaultValue
40-
: ((IPragmaValue<T>) pragma.Value).Value;
41-
}
42-
43-
public JPragma AddPragma(JPragma pragma)
44-
{
45-
if(Pragmas.ContainsKey(pragma.Name))
46-
throw new DuplicatePragmaException(MessageFormatter.FormatForSchema(
47-
PRAG03, $"Duplication found for {pragma.GetOutline()}", pragma.Context));
48-
Pragmas.Add(pragma.Name, pragma);
49-
return pragma;
50-
}
32+
33+
public JPragma AddPragma(JPragma pragma) => _pragmaManager.AddPragma(pragma);
34+
public T GetPragmaValue<T>(string name) => _pragmaManager.GetPragmaValue<T>(name);
5135

5236
public JInclude AddClass(JInclude include)
5337
{
5438
AddClass(include.ClassName, include.Context);
5539
return include;
5640
}
5741

58-
public void AddClass(string className, Context? context = null)
42+
public void AddClass(string className, Context? context = null)
5943
=> _functionManager.AddClass(className, context);
6044

6145
public bool InvokeFunction(JFunction function, JNode target)
@@ -66,15 +50,15 @@ public JDefinition AddDefinition(JDefinition definition)
6650
if(Definitions.TryGetValue(definition.Alias, out var previous))
6751
throw new DuplicateDefinitionException(MessageFormatter.FormatForSchema(
6852
DEFI01, $"Duplicate definition of {definition.Alias
69-
} is found and already defined as {previous.GetOutline()}",
53+
} is found and already defined as {previous.GetOutline()}",
7054
definition.Context));
7155
Definitions.Add(definition.Alias, definition.Validator);
7256
return definition;
7357
}
74-
75-
internal bool AreEqual(double value1, double value2)
58+
59+
internal bool AreEqual(double value1, double value2)
7660
=> Math.Abs(value1 - value2) < FloatingPointTolerance;
77-
61+
7862
internal bool FailWith(Exception exception)
7963
{
8064
if(ThrowException) throw exception;

JsonSchema/RelogicLabs/JsonSchema/Types/JBoolean.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ public override bool Equals(object? obj)
3232

3333
public override JsonType Type => JsonType.BOOLEAN;
3434
public override int GetHashCode() => Value.GetHashCode();
35+
public static implicit operator bool(JBoolean @bool) => @bool.Value;
3536
public override string ToString() => Value.ToString().ToLower();
3637
}

0 commit comments

Comments
 (0)