Skip to content

Commit 52d7ad0

Browse files
committed
Refactor and optimize code
1 parent 1c8d777 commit 52d7ad0

30 files changed

+118
-116
lines changed

JsonSchema/JsonSchema.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
<Description>New JSON Schema prioritizes simplicity, conciseness, readability, and efficiency.
66
It offers precise JSON document definition through ample functionalities and extensibility
77
to meet the diverse web service requirements.</Description>
8+
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>
89
<Authors>Relogic Labs</Authors>
910
<Company>Relogic Labs</Company>
10-
<Version>1.6.0</Version>
11-
<PackageVersion>1.6.0</PackageVersion>
12-
<AssemblyVersion>1.6.0</AssemblyVersion>
11+
<Version>1.7.0</Version>
12+
<PackageVersion>1.7.0</PackageVersion>
13+
<AssemblyVersion>1.7.0</AssemblyVersion>
1314
<PackageTags>JsonSchema;Schema;Json;Validation;Assert;Test</PackageTags>
1415
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1516
<NeutralLanguage>en</NeutralLanguage>
16-
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>
17-
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
17+
<TargetFrameworks>net5.0;net6.0;net7.0</TargetFrameworks>
1818
<ImplicitUsings>enable</ImplicitUsings>
1919
<Nullable>enable</Nullable>
2020
<RootNamespace>RelogicLabs</RootNamespace>
@@ -28,6 +28,7 @@
2828
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
2929
<IncludeSymbols>true</IncludeSymbols>
3030
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
31+
<LangVersion>preview</LangVersion>
3132
</PropertyGroup>
3233

3334
<ItemGroup>
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<TK>
3+
public interface IKeyer<out TK>
44
{
55
TK GetKey();
66
}

JsonSchema/RelogicLabs/JsonSchema/Collections/IndexHashMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace RelogicLabs.JsonSchema.Collections;
55

6-
public class IndexHashMap<TK, TV> : IIndexMap<TK, TV>
6+
public sealed class IndexHashMap<TK, TV> : IIndexMap<TK, TV>
77
where TV : IKeyer<TK> where TK : notnull
88
{
99
private IDictionary<TK, TV> _dictionary;
@@ -71,6 +71,7 @@ public bool TryGetValue(TK key, out TV? value)
7171

7272
public IIndexMap<TK, TV> AsReadOnly()
7373
{
74+
if(IsReadOnly) return this;
7475
_list = new ReadOnlyCollection<TV>(_list);
7576
_dictionary = new ReadOnlyDictionary<TK, TV>(_dictionary);
7677
return this;

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions1.cs

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

77
namespace RelogicLabs.JsonSchema.Functions;
88

9-
public partial class CoreFunctions : FunctionBase
9+
public sealed partial class CoreFunctions : FunctionBase
1010
{
1111
public CoreFunctions(RuntimeContext runtime) : base(runtime) { }
1212

@@ -19,7 +19,7 @@ public bool Length(JString target, JInteger length)
1919
new ActualDetail(target, $"found {_length} for {target}")));
2020
return true;
2121
}
22-
22+
2323
public bool Length(JArray target, JInteger length)
2424
{
2525
var _length = target.Elements.Count;
@@ -44,7 +44,7 @@ public bool Length(JString target, JInteger minimum, JInteger maximum)
4444
{
4545
var length = target.Value.Length;
4646
if(length < minimum)
47-
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN02,
47+
return FailWith(new JsonSchemaException(new ErrorDetail(SLEN02,
4848
$"String {target.GetOutline()} length is outside of range"),
4949
new ExpectedDetail(Function, $"length in range [{minimum}, {maximum}]"),
5050
new ActualDetail(target, $"found {length} that is less than {minimum}")));
@@ -55,7 +55,7 @@ public bool Length(JString target, JInteger minimum, JInteger maximum)
5555
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
5656
return true;
5757
}
58-
58+
5959
public bool Length(JString target, JInteger minimum, JUndefined undefined)
6060
{
6161
var length = target.Value.Length;
@@ -66,7 +66,7 @@ public bool Length(JString target, JInteger minimum, JUndefined undefined)
6666
new ActualDetail(target, $"found {length} that is less than {minimum}")));
6767
return true;
6868
}
69-
69+
7070
public bool Length(JString target, JUndefined undefined, JInteger maximum)
7171
{
7272
var length = target.Value.Length;
@@ -77,7 +77,7 @@ public bool Length(JString target, JUndefined undefined, JInteger maximum)
7777
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
7878
return true;
7979
}
80-
80+
8181
public bool Length(JArray target, JInteger minimum, JInteger maximum)
8282
{
8383
var length = target.Elements.Count;
@@ -93,7 +93,7 @@ public bool Length(JArray target, JInteger minimum, JInteger maximum)
9393
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
9494
return true;
9595
}
96-
96+
9797
public bool Length(JArray target, JInteger minimum, JUndefined undefined)
9898
{
9999
var length = target.Elements.Count;
@@ -104,7 +104,7 @@ public bool Length(JArray target, JInteger minimum, JUndefined undefined)
104104
new ActualDetail(target, $"found {length} that is less than {minimum}")));
105105
return true;
106106
}
107-
107+
108108
public bool Length(JArray target, JUndefined undefined, JInteger maximum)
109109
{
110110
var length = target.Elements.Count;
@@ -131,7 +131,7 @@ public bool Length(JObject target, JInteger minimum, JInteger maximum)
131131
new ActualDetail(target, $"found {length} that is greater than {maximum}")));
132132
return true;
133133
}
134-
134+
135135
public bool Length(JObject target, JInteger minimum, JUndefined undefined)
136136
{
137137
var length = target.Properties.Count;
@@ -142,7 +142,7 @@ public bool Length(JObject target, JInteger minimum, JUndefined undefined)
142142
new ActualDetail(target, $"found {length} that is less than {minimum}")));
143143
return true;
144144
}
145-
145+
146146
public bool Length(JObject target, JUndefined undefined, JInteger maximum)
147147
{
148148
var length = target.Properties.Count;

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace RelogicLabs.JsonSchema.Functions;
88

9-
public partial class CoreFunctions
9+
public sealed partial class CoreFunctions
1010
{
1111
public bool Enum(JString target, params JString[] items)
1212
{

JsonSchema/RelogicLabs/JsonSchema/Functions/CoreFunctions3.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace RelogicLabs.JsonSchema.Functions;
1010

11-
public partial class CoreFunctions
11+
public sealed partial class CoreFunctions
1212
{
1313
// Based on SMTP protocol RFC 5322
1414
private static readonly Regex EmailRegex = new(
@@ -85,31 +85,31 @@ public bool Url(JString target)
8585
// Handle Uri based on RFC 3986
8686
bool result = Uri.TryCreate(target, UriKind.Absolute, out Uri? uriResult);
8787
if(!result || uriResult == null) return FailWith(new JsonSchemaException(
88-
new ErrorDetail(URLA01, $"Invalid url address"),
88+
new ErrorDetail(URLA01, "Invalid url address"),
8989
new ExpectedDetail(Function, "a valid url address"),
9090
new ActualDetail(target, $"found {target} that is invalid")));
9191
result &= uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps;
9292
if(!result) return FailWith(new JsonSchemaException(
9393
new ErrorDetail(URLA02, "Invalid url address scheme"),
9494
new ExpectedDetail(Function, "HTTP or HTTPS scheme"),
95-
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {target} " +
96-
$"that has invalid scheme")));
95+
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {
96+
target} that has invalid scheme")));
9797
return true;
9898
}
9999

100100
public bool Url(JString target, JString scheme)
101101
{
102102
bool result = Uri.TryCreate(target, UriKind.Absolute, out Uri? uriResult);
103103
if(!result || uriResult == null) return FailWith(
104-
new JsonSchemaException(new ErrorDetail(URLA03, $"Invalid url address"),
104+
new JsonSchemaException(new ErrorDetail(URLA03, "Invalid url address"),
105105
new ExpectedDetail(Function, "a valid url address"),
106106
new ActualDetail(target, $"found {target} that is invalid")));
107107
result &= uriResult.Scheme.Equals(scheme);
108108
if(!result) return FailWith(new JsonSchemaException(
109109
new ErrorDetail(URLA04, "Mismatch url address scheme"),
110110
new ExpectedDetail(Function, $"scheme {scheme} for url address"),
111-
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {target} " +
112-
$"that does not matched")));
111+
new ActualDetail(target, $"found {uriResult.Scheme.Quote()} from {
112+
target} that does not matched")));
113113
return true;
114114
}
115115

JsonSchema/RelogicLabs/JsonSchema/Message/ActualDetail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace RelogicLabs.JsonSchema.Message;
66

7-
public class ActualDetail : ContextDetail
7+
public sealed class ActualDetail : ContextDetail
88
{
99
public ActualDetail(Context context, string message)
1010
: base(context, message) { }

JsonSchema/RelogicLabs/JsonSchema/Message/ErrorDetail.cs

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

3-
public class ErrorDetail
3+
public sealed class ErrorDetail
44
{
55
internal const string ValidationFailed = "Validation failed";
66
internal const string ValueMismatch = "Value mismatch";

JsonSchema/RelogicLabs/JsonSchema/Message/ExpectedDetail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace RelogicLabs.JsonSchema.Message;
55

6-
public class ExpectedDetail : ContextDetail
6+
public sealed class ExpectedDetail : ContextDetail
77
{
88
public ExpectedDetail(Context context, string message)
99
: base(context, message) { }

JsonSchema/RelogicLabs/JsonSchema/Message/MatchReport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace RelogicLabs.JsonSchema.Message;
44

5-
internal class MatchReport
5+
internal sealed class MatchReport
66
{
77
public static readonly MatchReport Success = new();
88
public static readonly MatchReport TypeError = new(DTYP04, DTYP06);

0 commit comments

Comments
 (0)