Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get root #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions JsonFlatFileDataStore.Test/SingleItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,41 @@ public void GetItem_Nullable_NotFound()
UTHelpers.Down(newFilePath);
}

[Fact]
public void GetRoot_Dynamic()
{
var pathToJson = UTHelpers.Up();

var store = new DataStore(pathToJson);

// Get root
var root = store.GetRoot();

Assert.Equal(1, root.user[0].id);
Assert.Equal(2.1, root.myValue);
Assert.Equal("Carrillo", root.family[0].parents[0].name);

UTHelpers.Down(pathToJson);
}

[Fact]
public void GetRoot_Typed()
{
var pathToJson = UTHelpers.Up();

// Open database (create new if file doesn't exist)
var store = new DataStore(pathToJson);

// Get root
var root = store.GetRoot<Store>();

Assert.Equal(1, root.User.First().Id);
Assert.Equal(2.1, root.MyValue);
Assert.Equal("Carrillo", root.Family.First().Parents.First().Name);

UTHelpers.Down(pathToJson);
}

[Fact]
public void NotFound_Exception()
{
Expand Down
10 changes: 10 additions & 0 deletions JsonFlatFileDataStore.Test/TestModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,14 @@ public class World

public float CameraRotationY { get; set; }
}

public class Store
{
public List<User> User { get; set; }
public List<Movie> Movies { get; set; }
public List<Family> Family { get; set; }
public List<World> Worlds { get; set; }
public Double MyValue { get; set; }
public List<Double> MyValues { get; set; }
}
}
33 changes: 33 additions & 0 deletions JsonFlatFileDataStore/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,39 @@ private async Task<bool> InnerCommit(bool isOperationAsync, CommitAction commitA
return actionSuccess;
}

public dynamic GetRoot()
{
if (_reloadBeforeGetCollection)
{
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
}

if (_jsonData == null)
return null;

return SingleDynamicItemReadConverter(_jsonData);
}

public T GetRoot<T>()
{
if (_reloadBeforeGetCollection)
{
_jsonData = JObject.Parse(ReadJsonFromFile(_filePath));
}

if (_jsonData == null)
{
if (Nullable.GetUnderlyingType(typeof(T)) != null)
{
return default(T);
}

throw new KeyNotFoundException();
}

return _jsonData.ToObject<T>();
}

private dynamic SingleDynamicItemReadConverter(JToken e)
{
switch (e)
Expand Down
13 changes: 13 additions & 0 deletions JsonFlatFileDataStore/IDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ public interface IDataStore : IDisposable
/// <param name="key">Item key</param>
/// <returns>true if items found for deletion</returns>
Task<bool> DeleteItemAsync(string key);

/// <summary>
/// Get root item
/// </summary>
/// <returns>Dynamic item</returns>
dynamic GetRoot();

/// <summary>
/// Get root item
/// </summary>
/// <typeparam name="T">Item type</typeparam>
/// <returns>Typed item</returns>
T GetRoot<T>();
}

public enum ValueType
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ var counter = store.GetItem<int>("counter");
var user = store.GetItem("myUser");
```

`GetRoot` will return the dynamic root object and `GetRoot<T>` will return the root object of type T.

```csharp
var store = new DataStore(pathToJson);
// Dynamic data
var settingsDynamic = store.GetRoot();
// Typed data
var settings = store.GetRoot<Settings>("myUser");
```

Typed data will throw `KeyNotFoundException` if key is not found. Dynamic data and nullable types will return null.

```csharp
Expand Down