-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed ci asd
- Loading branch information
Showing
13 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
_src/Implementation/Configuration/Factories/JsonConfigurationQueryFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
98
_src/Implementation/Configuration/JsonConfigurationQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
_src/Infrastructure/Configuration/Factories/IConfigurationQueryFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,8 @@ | |
|
||
|
||
|
||
|
||
|
||
|
||
|
||
</Project> |
66 changes: 66 additions & 0 deletions
66
_src/_Tests/ImplementationTest/Configuration/JsonConfigurationQueryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
_src/_Tests/ImplementationTest/Configuration/Model/Admin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"server": { | ||
"db": { | ||
"connection": "test:string" | ||
}, | ||
"admin": { | ||
"name": "Bela", | ||
"role": "Heathcliff" | ||
} | ||
} | ||
} |