Skip to content

Commit f28b235

Browse files
committed
Update exception handlers and messages
1 parent 73c8c0f commit f28b235

File tree

6 files changed

+42
-25
lines changed

6 files changed

+42
-25
lines changed

JsonSchema/RelogicLabs/JsonSchema/Message/ContextDetail.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ internal static string GetTypeName(JNode node)
2828
? jsonNode.Type.Name
2929
: node.GetType().Name;
3030
}
31+
32+
internal static string GetTypeName(Type type)
33+
{
34+
var jsonType = JsonType.From(type);
35+
return jsonType != null ? jsonType.Name : type.Name;
36+
}
3137
}

JsonSchema/RelogicLabs/JsonSchema/Tree/FunctionManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using RelogicLabs.JsonSchema.Message;
55
using RelogicLabs.JsonSchema.Types;
66
using RelogicLabs.JsonSchema.Utilities;
7+
using static RelogicLabs.JsonSchema.Message.ContextDetail;
78
using static RelogicLabs.JsonSchema.Message.ErrorCode;
8-
using static RelogicLabs.JsonSchema.Tree.MethodPointer;
99

1010
namespace RelogicLabs.JsonSchema.Tree;
1111

@@ -121,8 +121,8 @@ public bool InvokeFunction(JFunction function, JNode target)
121121
if(!IsMatch(_parameters[0], target))
122122
{
123123
mismatchMessage = $"Function {function.GetOutline()} is applicable on {
124-
_parameters[0].ParameterType.Name} but applied on {
125-
target.GetType().Name} of {target}";
124+
GetTypeName(_parameters[0].ParameterType)} but applied on {
125+
GetTypeName(target.GetType())} of {target}";
126126
continue;
127127
}
128128
return method.Invoke(function, AddTarget(schemaArgs, target));

JsonSchema/RelogicLabs/JsonSchema/Types/JValidator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public class JValidator : JBranch
1010
{
1111
public const string OptionalMarker = "?";
1212
private IEnumerable<JNode> _children = Enumerable.Empty<JNode>()!;
13-
13+
1414
public JNode? Value { get; init; }
1515
public required IList<JFunction> Functions { get; init; }
1616
public required IList<JDataType> DataTypes { get; init; }
1717
public bool Optional { get; init; }
1818
public override IEnumerable<JNode> Children => _children;
19-
19+
2020
internal JValidator(IDictionary<JNode, JNode> relations) : base(relations) { }
2121

2222
internal override JValidator Initialize()
@@ -59,8 +59,8 @@ private bool MatchDataType(JNode node)
5959

6060
public override string ToString() => (
6161
$"{Value?.ToString() ?? string.Empty}"
62-
+ $"{Functions.ToString(" ", " ")}"
63-
+ $"{DataTypes.ToString(" ", " ")}"
62+
+ $"{Functions.Join(" ", " ")}"
63+
+ $"{DataTypes.Join(" ", " ")}"
6464
+ $"{(Optional? $" {OptionalMarker}" : string.Empty)}"
6565
).Trim();
6666
}

JsonSchema/RelogicLabs/JsonSchema/Types/JsonType.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
using RelogicLabs.JsonSchema.Message;
44
using RelogicLabs.JsonSchema.Time;
55
using RelogicLabs.JsonSchema.Tree;
6+
using RelogicLabs.JsonSchema.Utilities;
67
using static RelogicLabs.JsonSchema.Message.ErrorCode;
78

89
namespace RelogicLabs.JsonSchema.Types;
910

1011
public class JsonType
1112
{
12-
private static readonly Dictionary<string, JsonType> _DataTypes = new();
13+
private static readonly Dictionary<string, JsonType> _StringMapTypes = new();
14+
private static readonly Dictionary<Type, JsonType> _ClassMapTypes = new();
1315
private static readonly DateTimeValidator _Iso8601Date = new(DateTimeValidator.ISO_8601_DATE);
1416
private static readonly DateTimeValidator _Iso8601Time = new(DateTimeValidator.ISO_8601_TIME);
15-
17+
1618
public static readonly JsonType BOOLEAN = new("#boolean", typeof(JBoolean));
1719
public static readonly JsonType STRING = new("#string", typeof(JString));
1820
public static readonly JsonType INTEGER = new("#integer", typeof(JInteger));
@@ -25,17 +27,18 @@ public class JsonType
2527
public static readonly JsonType DATE = new("#date", typeof(JString));
2628
public static readonly JsonType TIME = new("#time", typeof(JString));
2729
public static readonly JsonType ANY = new("#any", typeof(IJsonType));
28-
30+
2931
public string Name { get; }
3032
public Type Type { get; }
3133

3234

33-
internal static JsonType From(ITerminalNode node)
35+
internal static JsonType? From(Type type) => _ClassMapTypes.GetValue(type);
36+
internal static JsonType From(ITerminalNode node)
3437
=> From(node.GetText(), Location.From(node.Symbol));
35-
38+
3639
internal static JsonType From(string name, Location location)
3740
{
38-
var result = _DataTypes.TryGetValue(name, out var type);
41+
var result = _StringMapTypes.TryGetValue(name, out var type);
3942
if(!result) throw new InvalidDataTypeException(MessageFormatter.FormatForSchema(
4043
DTYP01, $"Invalid data type {name}", location));
4144
return type!;
@@ -45,7 +48,8 @@ private JsonType(string name, Type type)
4548
{
4649
Name = name;
4750
Type = type;
48-
_DataTypes[name] = this;
51+
_StringMapTypes[name] = this;
52+
_ClassMapTypes.TryAdd(type, this);
4953
}
5054

5155
public override string ToString() => Name;

JsonSchema/RelogicLabs/JsonSchema/Utilities/CollectionExtension.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ namespace RelogicLabs.JsonSchema.Utilities;
55

66
internal static class CollectionExtension
77
{
8-
public static string Join(this IEnumerable<string> source, string separator)
9-
=> string.Join(separator, source);
8+
public static string Join(this IEnumerable<object> source, string separator,
9+
string prefix = "", string suffix = "")
10+
{
11+
var result = string.Join(separator, source);
12+
return string.IsNullOrEmpty(result) ? result : $"{prefix}{result}{suffix}";
13+
}
1014

1115
public static string ToString(this IEnumerable<JNode> source, string separator,
1216
string prefix = "", string suffix = "")
@@ -76,4 +80,7 @@ public static IEnumerable<JNode> ContainsValues(
7680
source.ForEach(p => _values.Remove(p.Value));
7781
return _values;
7882
}
83+
84+
public static TV? GetValue<TK, TV>(this IDictionary<TK, TV> dict, TK key)
85+
=> dict.TryGetValue(key, out var value) ? value : default;
7986
}

JsonSchema/RelogicLabs/JsonSchema/Utilities/StringExtension.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ namespace RelogicLabs.JsonSchema.Utilities;
44

55
internal static class StringExtension
66
{
7-
public static string Affix(this string source, string prefix = "", string suffix = "")
7+
public static string Affix(this string source, string prefix = "", string suffix = "")
88
=> $"{prefix}{source}{suffix}";
9-
10-
public static string ToUpperFirstLetter(this string source)
9+
10+
public static string ToUpperFirstLetter(this string source)
1111
=> source[..1].ToUpper() + source[1..];
12-
13-
public static string ToLowerFirstLetter(this string source)
12+
13+
public static string ToLowerFirstLetter(this string source)
1414
=> source[..1].ToLower() + source[1..];
15-
15+
1616
public static string ToEncoded(this string source)
1717
{
1818
StringBuilder builder = new();
19-
if(source.StartsWith('"') && source.EndsWith('"'))
19+
if(source.StartsWith('"') && source.EndsWith('"'))
2020
source = source[1..^1];
21-
21+
2222
for(int i = 0; i < source.Length; i++)
2323
{
2424
char current = source[i];
@@ -42,7 +42,7 @@ public static string ToEncoded(this string source)
4242
}
4343
return builder.ToString();
4444
}
45-
45+
4646
public static string Quote(this string source)
4747
=> $"\"{source}\"";
4848
}

0 commit comments

Comments
 (0)