Skip to content

Commit 101f6b2

Browse files
committed
Refactor and optimize code for script
1 parent c47f6f9 commit 101f6b2

21 files changed

+452
-254
lines changed

.gitignore

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/.idea/*
2-
/JsonSchema/bin/*
3-
/JsonSchema/obj/*
4-
/JsonSchema/.vscode/*
5-
/JsonSchema.Tests/bin/*
6-
/JsonSchema.Tests/obj/*
7-
/JsonSchema.Tests/.vscode/*
2+
/JSchema/bin/*
3+
/JSchema/obj/*
4+
/JSchema/.vscode/*
5+
/JSchema.Tests/bin/*
6+
/JSchema.Tests/obj/*
7+
/JSchema.Tests/.vscode/*
8+
/Temp*

JSchema.Tests/JSchema.Tests.csproj

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<AssemblyName>RelogicLabs.JsonSchema.Tests</AssemblyName>
5-
<Description>This project contains test cases for JsonSchema project.</Description>
4+
<AssemblyName>RelogicLabs.JSchema.Tests</AssemblyName>
5+
<Description>This project contains test cases for JSchema project.</Description>
66
<Authors>Relogic Labs</Authors>
77
<Company>Relogic Labs</Company>
8-
<Version>1.12.1</Version>
9-
<AssemblyVersion>1.12.1</AssemblyVersion>
8+
<Version>2.0.0</Version>
9+
<AssemblyVersion>2.0.0</AssemblyVersion>
1010
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1111
<NeutralLanguage>en</NeutralLanguage>
1212
<TargetFrameworks>net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
@@ -18,14 +18,14 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
22-
<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
23-
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />
24-
<PackageReference Include="coverlet.collector" Version="6.0.0" />
21+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
22+
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
23+
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
24+
<PackageReference Include="coverlet.collector" Version="6.0.1" />
2525
</ItemGroup>
2626

2727
<ItemGroup>
28-
<ProjectReference Include="..\JsonSchema\JsonSchema.csproj" />
28+
<ProjectReference Include="..\JSchema\JSchema.csproj" />
2929
</ItemGroup>
3030

3131
</Project>

JSchema.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonSchema", "JsonSchema\JsonSchema.csproj", "{0876A8A3-F8F2-493D-AB6E-D854660B20D0}"
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSchema", "JSchema\JSchema.csproj", "{0876A8A3-F8F2-493D-AB6E-D854660B20D0}"
44
EndProject
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonSchema.Tests", "JsonSchema.Tests\JsonSchema.Tests.csproj", "{D2AA1DBC-1E02-4711-98D1-FA6097A56FA2}"
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSchema.Tests", "JSchema.Tests\JSchema.Tests.csproj", "{D2AA1DBC-1E02-4711-98D1-FA6097A56FA2}"
66
EndProject
77
Global
88
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,4 +19,4 @@ Global
1919
{D2AA1DBC-1E02-4711-98D1-FA6097A56FA2}.Release|Any CPU.ActiveCfg = Release|Any CPU
2020
{D2AA1DBC-1E02-4711-98D1-FA6097A56FA2}.Release|Any CPU.Build.0 = Release|Any CPU
2121
EndGlobalSection
22-
EndGlobal
22+
EndGlobal

JSchema/JSchema.csproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<PackageId>RelogicLabs.JsonSchema</PackageId>
5-
<Description>New JSON Schema prioritizes simplicity, conciseness, readability, and efficiency.
6-
It offers precise JSON document definition through ample functionalities and extensibility
7-
to meet the diverse web service requirements.</Description>
8-
<AssemblyName>RelogicLabs.JsonSchema</AssemblyName>
4+
<PackageId>RelogicLabs.JSchema</PackageId>
5+
<Description>JSchema, a new Json Schema, prioritizes simplicity, conciseness, readability,
6+
and efficiency. It offers precise Json document definition through effective functionalities
7+
and extensibility to meet diverse web service requirements.</Description>
8+
<AssemblyName>RelogicLabs.JSchema</AssemblyName>
99
<Authors>Relogic Labs</Authors>
1010
<Company>Relogic Labs</Company>
11-
<Version>1.12.1</Version>
12-
<PackageVersion>1.12.1</PackageVersion>
13-
<AssemblyVersion>1.12.1</AssemblyVersion>
11+
<Version>2.0.0</Version>
12+
<PackageVersion>2.0.0</PackageVersion>
13+
<AssemblyVersion>2.0.0</AssemblyVersion>
1414
<PackageTags>JSchema;JsonSchema;Schema;Json;Validation;Assert;Test</PackageTags>
1515
<Copyright>Copyright © Relogic Labs. All rights reserved.</Copyright>
1616
<NeutralLanguage>en</NeutralLanguage>
@@ -20,7 +20,7 @@
2020
<RootNamespace>RelogicLabs</RootNamespace>
2121
<PackageReadmeFile>README.md</PackageReadmeFile>
2222
<PackageIcon>icon.png</PackageIcon>
23-
<PackageProjectUrl>https://www.relogiclabs.com/p/json-schema.html</PackageProjectUrl>
23+
<PackageProjectUrl>https://www.relogiclabs.com/p/jschema.html</PackageProjectUrl>
2424
<RepositoryUrl>https://github.com/relogiclabs/JSchema-DotNet.git</RepositoryUrl>
2525
<RepositoryType>git</RepositoryType>
2626
<RepositoryBranch>master</RepositoryBranch>
@@ -33,7 +33,7 @@
3333

3434
<ItemGroup>
3535
<PackageReference Include="Antlr4.Runtime.Standard" Version="4.13.1"/>
36-
<InternalsVisibleTo Include="RelogicLabs.JsonSchema.Tests"/>
36+
<InternalsVisibleTo Include="RelogicLabs.JSchema.Tests"/>
3737
<AssemblyAttribute Include="System.CLSCompliantAttribute">
3838
<_Parameter1>true</_Parameter1>
3939
</AssemblyAttribute>

JSchema/RelogicLabs/JSchema/Collections/IndexHashMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.ObjectModel;
3+
using static RelogicLabs.JSchema.Utilities.CommonUtilities;
34

45
namespace RelogicLabs.JSchema.Collections;
56

@@ -12,7 +13,7 @@ internal sealed class IndexHashMap<TK, TV> : IIndexMap<TK, TV>
1213
public IndexHashMap(IEnumerable<TV> source)
1314
{
1415
_list = source.ToList();
15-
_dictionary = _list.ToDictionary(static e => e.Key, static e => e);
16+
_dictionary = _list.ToDictionary(static e => e.Key, Identity);
1617
}
1718

1819
public IEnumerator<TV> GetEnumerator() => _list.GetEnumerator();

JSchema/RelogicLabs/JSchema/Engine/ScriptTreeHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static GParameter[] ToParameters(IList<ITerminalNode> identifiers, ITermi
9292
{
9393
var group = identifiers.GroupBy(static i => i.GetText())
9494
.FirstOrDefault(static g => g.Count() > 1);
95-
var duplicate = group?.First();
95+
var duplicate = group?.Last();
9696
if(duplicate != null) throw FailOnDuplicateParameterName(duplicate);
9797
var parameters = identifiers.Select(static i => i.GetText()).ToList();
9898
if(ellipsis != null) UpdateLast(parameters, ellipsis.GetText());

JSchema/RelogicLabs/JSchema/JsonAssert.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using RelogicLabs.JsonSchema.Tree;
2-
using RelogicLabs.JsonSchema.Utilities;
3-
using static RelogicLabs.JsonSchema.Message.MessageFormatter;
4-
using static RelogicLabs.JsonSchema.Tree.TreeType;
1+
using RelogicLabs.JSchema.Tree;
2+
using RelogicLabs.JSchema.Utilities;
3+
using static RelogicLabs.JSchema.Message.MessageFormatter;
4+
using static RelogicLabs.JSchema.Tree.TreeType;
55

6-
namespace RelogicLabs.JsonSchema;
6+
namespace RelogicLabs.JSchema;
77

88
/// <summary>
9-
/// Provides assertion functionalities to validate JSON document against a Schema or JSON.
9+
/// Provides assertion functionalities to validate JSON document against a JSchema or JSON document.
1010
/// </summary>
1111
public class JsonAssert
1212
{
@@ -15,18 +15,18 @@ public class JsonAssert
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="JsonAssert"/> class for the
18-
/// specified Schema string.
18+
/// specified JSchema string.
1919
/// </summary>
20-
/// <param name="schema">A Schema string for validation or conformation.</param>
20+
/// <param name="schema">A JSchema string for validation or conformation.</param>
2121
public JsonAssert(string schema) : this(schema, SCHEMA_TREE) { }
2222

2323
/// <summary>
2424
/// 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.
25+
/// <paramref name="expected"/> string which can be either a JSchema or a JSON representation.
2626
/// </summary>
27-
/// <param name="expected">An expected Schema or JSON string for validation or conformation.</param>
27+
/// <param name="expected">An expected JSchema or JSON string for validation or conformation.</param>
2828
/// <param name="type">The type of string provided by <paramref name="expected"/>, indicating
29-
/// whether it represents a Schema or JSON. Use <see cref="TreeType.SCHEMA_TREE"/> for Schema
29+
/// whether it represents a JSchema or JSON. Use <see cref="TreeType.SCHEMA_TREE"/> for JSchema
3030
/// and <see cref="TreeType.JSON_TREE"/> for JSON.</param>
3131
public JsonAssert(string expected, TreeType type)
3232
{
@@ -43,24 +43,24 @@ public JsonAssert(string expected, TreeType type)
4343
}
4444

4545
/// <summary>
46-
/// Tests whether the input JSON string conforms to the expected Schema or JSON
46+
/// Tests whether the input JSON string conforms to the expected JSchema or JSON
4747
/// specified in the <see cref="JsonAssert"/> constructor.
4848
/// </summary>
4949
/// <param name="json">The actual JSON to conform or validate.</param>
5050
public void IsValid(string json)
5151
{
5252
Runtime.Clear();
5353
JsonTree jsonTree = new(Runtime, json);
54-
DebugUtilities.Print(ExpectedTree, jsonTree);
54+
LogHelper.Debug(ExpectedTree, jsonTree);
5555
if(!ExpectedTree.Match(jsonTree))
5656
throw new InvalidOperationException("Invalid runtime state");
5757
}
5858

5959
/// <summary>
60-
/// Tests whether the specified JSON string conforms to the given Schema string
61-
/// and throws an exception if the JSON string does not conform to the Schema.
60+
/// Tests whether the specified JSON string conforms to the given JSchema string
61+
/// and throws an exception if the JSON string does not conform to the JSchema.
6262
/// </summary>
63-
/// <param name="schema">The expected Schema to conform or validate.</param>
63+
/// <param name="schema">The expected JSchema to conform or validate.</param>
6464
/// <param name="json">The actual JSON to conform or validate.</param>
6565
public static void IsValid(string schema, string json)
6666
=> new JsonAssert(schema).IsValid(json);

JSchema/RelogicLabs/JSchema/JsonSchema.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using RelogicLabs.JsonSchema.Tree;
2-
using RelogicLabs.JsonSchema.Utilities;
3-
using static RelogicLabs.JsonSchema.Message.MessageFormatter;
1+
using RelogicLabs.JSchema.Tree;
2+
using RelogicLabs.JSchema.Utilities;
3+
using static RelogicLabs.JSchema.Message.MessageFormatter;
44

5-
namespace RelogicLabs.JsonSchema;
5+
namespace RelogicLabs.JSchema;
66

77
/// <summary>
8-
/// Provides Schema validation functionalities for JSON document.
8+
/// Provides JSchema validation functionalities for JSON document.
99
/// </summary>
1010
public class JsonSchema
1111
{
@@ -15,9 +15,9 @@ public class JsonSchema
1515

1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="JsonSchema"/> class for the
18-
/// specified Schema string.
18+
/// specified JSchema string.
1919
/// </summary>
20-
/// <param name="schema">A Schema string for validation or conformation.</param>
20+
/// <param name="schema">A JSchema string for validation or conformation.</param>
2121
public JsonSchema(string schema)
2222
{
2323
Runtime = new(SchemaValidation, false);
@@ -26,37 +26,42 @@ public JsonSchema(string schema)
2626
}
2727

2828
/// <summary>
29-
/// Indicates whether the input JSON string conforms to the Schema specified
29+
/// Indicates whether the input JSON string conforms to the JSchema specified
3030
/// in the <see cref="JsonSchema"/> constructor.
3131
/// </summary>
32-
/// <param name="json">The JSON string to conform or validate with Schema.</param>
33-
/// <returns><c>true</c> if the JSON string conforms to the Schema; otherwise,
32+
/// <param name="json">The JSON string to conform or validate with JSchema.</param>
33+
/// <returns><c>true</c> if the JSON string conforms to the JSchema; otherwise,
3434
/// <c>false</c>.</returns>
3535
public bool IsValid(string json)
3636
{
3737
Runtime.Clear();
3838
JsonTree jsonTree = new(Runtime, json);
39-
DebugUtilities.Print(SchemaTree, jsonTree);
39+
LogHelper.Debug(SchemaTree, jsonTree);
4040
var result = SchemaTree.Match(jsonTree);
4141
return result;
4242
}
4343

4444
/// <summary>
45-
/// Writes error messages that occur during Schema validation process, to the
45+
/// Writes error messages that occur during JSchema validation process, to the
4646
/// standard error stream.
4747
/// </summary>
4848
public void WriteError()
4949
{
50+
if(Exceptions.Count == 0)
51+
{
52+
Console.WriteLine("No error has occurred");
53+
return;
54+
}
5055
foreach(var exception in Exceptions)
5156
Console.Error.WriteLine(exception.Message);
5257
}
5358

5459
/// <summary>
55-
/// Indicates whether the input JSON string conforms to the given Schema string.
60+
/// Indicates whether the input JSON string conforms to the given JSchema string.
5661
/// </summary>
57-
/// <param name="schema">The Schema string to conform or validate.</param>
62+
/// <param name="schema">The JSchema string to conform or validate.</param>
5863
/// <param name="json">The JSON string to conform or validate.</param>
59-
/// <returns><c>true</c> if the JSON string conforms to the Schema; otherwise,
64+
/// <returns><c>true</c> if the JSON string conforms to the JSchema; otherwise,
6065
/// <c>false</c>.</returns>
6166
public static bool IsValid(string schema, string json)
6267
{

JSchema/RelogicLabs/JSchema/Message/ContextDetail.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using RelogicLabs.JsonSchema.Tree;
2-
using RelogicLabs.JsonSchema.Types;
1+
using RelogicLabs.JSchema.Tree;
2+
using RelogicLabs.JSchema.Nodes;
33

4-
namespace RelogicLabs.JsonSchema.Message;
4+
namespace RelogicLabs.JSchema.Message;
55

66
public abstract class ContextDetail
77
{
@@ -28,7 +28,7 @@ internal static string GetTypeName(JNode node)
2828

2929
internal static string GetTypeName(Type type)
3030
{
31-
var jsonType = JsonType.From(type);
32-
return jsonType != null ? jsonType.Name : type.Name;
31+
var t = JsonType.GetType(type);
32+
return t?.Name ?? type.Name;
3333
}
3434
}

0 commit comments

Comments
 (0)