Skip to content

Commit

Permalink
Added JsonCOnfiguration query
Browse files Browse the repository at this point in the history
fixed ci

asd
  • Loading branch information
joshika39 committed Oct 4, 2023
1 parent 4eaf9cf commit 8f837da
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/modules-cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI-CD
on:
push:
tags:
- "*v[0-9]+.[0-9]+.[0-9]+-preview[0-9][0-9][0-9][0-9]"
- "*v*"

jobs:
deploy:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Infrastructure.Configuration;
using Infrastructure.Configuration.Factories;

namespace Implementation.Configuration.Factories
{
internal class JsonConfigurationQueryFactory : IConfigurationQueryFactory
{
public IConfigurationQuery CreateConfigurationQuery(string filePath)
{
return new JsonConfigurationQuery(filePath);
}
}
}
98 changes: 98 additions & 0 deletions _src/Implementation/Configuration/JsonConfigurationQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Infrastructure.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Implementation.Configuration
{
internal class JsonConfigurationQuery : IConfigurationQuery
{
private readonly string _filePath;

public JsonConfigurationQuery(string filePath)
{
_filePath = filePath ?? throw new ArgumentNullException(nameof(filePath));
}

#region Get
public async Task<string?> GetStringAttributeAsync(string path)
{
return (await GetAttributeAsync(path)).Value<string>();
}

public async Task<int> GetIntAttributeAsync(string path)
{
return (await GetAttributeAsync(path)).Value<int>();
}

public async Task<bool> GetBoolAttributeAsync(string path)
{
return (await GetAttributeAsync(path)).Value<bool>();
}

public async Task<T?> GetObjectAsync<T>(string path)
{
var token = await GetAttributeAsync(path);
return JsonConvert.DeserializeObject<T>(token?.ToString());
}
#endregion

public async Task SetAttributeAsync(string path, string value)
{
await SetObjectAsync(path, value);
}

public async Task SetAttributeAsync(string path, int value)
{
await SetObjectAsync(path, value);
}

public async Task SetAttributeAsync(string path, bool value)
{
await SetObjectAsync(path, value);
}

public async Task SetObjectAsync<T>(string path, T value)
{
var jsonObject = JObject.Parse(await File.ReadAllTextAsync(_filePath));
JToken? token = jsonObject;
var split = path.Trim('.').Split('.');
for (var index = 0; index < split.Length; index++)
{
var segment = split[index];
if (token is not JObject jObject)
{
break;
}
if (index == split.Length - 1)
{
jObject[segment] = JToken.FromObject(value);
break;
}
token = jObject.TryGetValue(segment, out var nextToken) ? nextToken : null;
if (token == null)
{
break;
}
}
await File.WriteAllTextAsync(_filePath, jsonObject.ToString(Formatting.Indented));
}

private async Task<JToken?> GetAttributeAsync(string path)
{
JToken? token = JObject.Parse(await File.ReadAllTextAsync(_filePath));
foreach (var segment in path.Trim('.').Split('.'))
{
if (token is not JObject jObject)
{
return default;
}

token = jObject.TryGetValue(segment, out var nextToken) ? nextToken : null;
}
return token;
}
}
}
7 changes: 7 additions & 0 deletions _src/Implementation/Implementation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
<Platforms>AnyCPU</Platforms>
<AssemblyName>joshika39.$(MSBuildProjectName)</AssemblyName>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand All @@ -36,6 +37,12 @@
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>







<Target DependsOnTargets="ResolveReferences" Name="CopyProjectReferencesToPackage">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
Expand Down
3 changes: 3 additions & 0 deletions _src/Implementation/Module/CoreModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using Implementation.Configuration.Factories;
using Implementation.IO;
using Implementation.IO.Factories;
using Implementation.Logger.Factories;
using Implementation.Navigator.Factories;
using Infrastructure.Configuration.Factories;
using Infrastructure.IO;
using Infrastructure.IO.Factories;
using Infrastructure.Logger;
Expand All @@ -23,6 +25,7 @@ public void LoadModules(IServiceCollection collection)
collection.AddTransient<IIOFactory, IOFactory>();
collection.AddTransient<INavigatorFactory, NavigatorFactory>();
collection.AddTransient<INavigatorElementFactory, NavigatorElementFactory>();
collection.AddTransient<IConfigurationQueryFactory, JsonConfigurationQueryFactory>();
collection.AddScoped<IWriter, Writer>();
collection.AddScoped<IReader, Reader>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Infrastructure.Configuration.Factories
{
public interface IConfigurationQueryFactory
{
IConfigurationQuery CreateConfigurationQuery(string filePath);
}
}
17 changes: 17 additions & 0 deletions _src/Infrastructure/Configuration/IConfigurationQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading.Tasks;

namespace Infrastructure.Configuration
{
public interface IConfigurationQuery
{
Task<string?> GetStringAttributeAsync(string path);
Task<int> GetIntAttributeAsync(string path);
Task<bool> GetBoolAttributeAsync(string path);
Task<T?> GetObjectAsync<T>(string path);

Task SetAttributeAsync(string path, string value);
Task SetAttributeAsync(string path, int value);
Task SetAttributeAsync(string path, bool value);
Task SetObjectAsync<T>(string path, T value);
}
}
4 changes: 4 additions & 0 deletions _src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@







</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.Threading.Tasks;
using Implementation.Configuration;
using ImplementationTest.Configuration.Model;
using Xunit;

namespace ImplementationTest.Configuration
{
public class JsonConfigurationQueryTests
{
[Fact]
public async Task JCQT_0001()
{
var filePath = @".\Resources\JCQT\0001.json";

var jsonService = new JsonConfigurationQuery(filePath);

var test = await jsonService.GetStringAttributeAsync("server.db.connection");
}

[Fact]
public async Task JCQT_0002()
{
var filePath = @".\Resources\JCQT\0001.json";

var jsonService = new JsonConfigurationQuery(filePath);

await jsonService.SetAttributeAsync("server.db.connection", "changed:connection");
}

[Fact]
public async Task JCQT_0003()
{
var filePath = @".\Resources\JCQT\0001.json";

var jsonService = new JsonConfigurationQuery(filePath);

await jsonService.SetAttributeAsync("server.db.port", 1234);
}

[Fact]
public async Task JCQT_0004()
{
var filePath = @".\Resources\JCQT\0001.json";

var jsonService = new JsonConfigurationQuery(filePath);

var admin = await jsonService.GetObjectAsync<Admin>("server.admin");
}

[Fact]
public async Task JCQT_0005()
{
const string FILE_PATH = @".\Resources\JCQT\0001.json";

var jsonService = new JsonConfigurationQuery(FILE_PATH);

var admin = await jsonService.GetObjectAsync<Admin>("server.admin");
admin.Name = "Kayaba Akihiko";

await jsonService.SetObjectAsync("server.admin", admin);

admin = await jsonService.GetObjectAsync<Admin>("server.admin");
Assert.Equal("Kayaba Akihiko", admin.Name);
}
}
}
14 changes: 14 additions & 0 deletions _src/_Tests/ImplementationTest/Configuration/Model/Admin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace ImplementationTest.Configuration.Model
{
public class Admin
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("role")]
public string Role { get; set;}
}
}
8 changes: 8 additions & 0 deletions _src/_Tests/ImplementationTest/Configuration/Model/IAdmin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ImplementationTest.Configuration.Model
{
public interface IAdmin
{
string Name { get; set; }
string Role { get; set; }
}
}
12 changes: 12 additions & 0 deletions _src/_Tests/ImplementationTest/ImplementationTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@
<None Update="Resources\JRT\0021-users.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\JCST\0001.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\JCQST\0001.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\JCQT\0001.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>



</ItemGroup>


Expand Down
11 changes: 11 additions & 0 deletions _src/_Tests/ImplementationTest/Resources/JCQT/0001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"server": {
"db": {
"connection": "test:string"
},
"admin": {
"name": "Bela",
"role": "Heathcliff"
}
}
}

0 comments on commit 8f837da

Please sign in to comment.