Skip to content

Commit

Permalink
Create file directory if it does not exist before doing File.WriteAll…
Browse files Browse the repository at this point in the history
…Text in JsonConfigStore
  • Loading branch information
youri authored and aloneguid committed Nov 4, 2022
1 parent 4256aa9 commit adf833f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
21 changes: 21 additions & 0 deletions src/Config.Net.Tests/Stores/JsonFileCOnfigStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using Config.Net.Stores;
using Xunit;
using YamlDotNet.Core.Tokens;

namespace Config.Net.Tests.Stores
{
Expand Down Expand Up @@ -50,6 +51,26 @@ public void Write_new_value_hierarchically()
Assert.Equal("111", _store.Read(key));
}

[Fact]
public void TestCreatingFileInMissingFolder()
{
_path = Path.Combine("C:\\temp", "TestData", "sample.json");

if (Directory.Exists(_path))
{
Directory.Delete(_path);
}

string key = "One.Two.Three";

_store = new JsonConfigStore(_path, true);
_store.Write(key, "111");

Assert.Equal("111", _store.Read(key));

Assert.True(File.Exists(_path));
}

public void Dispose()
{
_store.Dispose();
Expand Down
23 changes: 18 additions & 5 deletions src/Config.Net/Stores/JsonConfigStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Text.Json;
using System.Text.Json.Nodes;
using Config.Net.Core;
Expand All @@ -26,7 +27,7 @@ public JsonConfigStore(string name, bool isFilePath)
{
if (name == null) throw new ArgumentNullException(nameof(name));

if(isFilePath)
if (isFilePath)
{
_pathName = Path.GetFullPath(name); // Allow relative path to JSON file
_j = ReadJsonFile(_pathName);
Expand Down Expand Up @@ -59,15 +60,15 @@ public void Dispose()
if (parts.Length == 0) return null;

JsonNode? node = _j;
foreach(string rawPart in parts)
foreach (string rawPart in parts)
{
bool isIndex = OptionPath.TryStripIndex(rawPart, out string? part, out int partIndex);
if (part == null) return null;

node = node![part];
if (node == null) return null;

if(isIndex)
if (isIndex)
{
if (!(node is JsonArray ja)) return null;

Expand Down Expand Up @@ -113,7 +114,7 @@ public void Write(string key, string? value)
}
else
{
if(nextNode == null)
if (nextNode == null)
{
//create missing node
nextNode = new JsonObject();
Expand All @@ -130,12 +131,24 @@ public void Write(string key, string? value)
parent![lastPart!] = JsonValue.Create(value);

string js = _j.ToJsonString(new JsonSerializerOptions { WriteIndented = true });

FileInfo file = new FileInfo(_pathName);

if (file is not null)
{
if (file.Directory is not null)
{
file.Directory.Create();
}
}

File.WriteAllText(_pathName, js);

}

private static JsonNode? ReadJsonFile(string fileName)
{
if(File.Exists(fileName))
if (File.Exists(fileName))
{
string json = File.ReadAllText(fileName);
return ReadJsonString(json);
Expand Down

0 comments on commit adf833f

Please sign in to comment.