Skip to content

Commit

Permalink
Resolves ttu#19
Browse files Browse the repository at this point in the history
  • Loading branch information
colbytimm committed Feb 21, 2022
1 parent a55acde commit 9d04821
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
35 changes: 35 additions & 0 deletions JsonFlatFileDataStore.Test/DataStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,41 @@ public async Task Insert_CorrectIdWithDynamic_With_InitialId()
UTHelpers.Down(pathToJson);
}

[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 async Task Readme_Example3()
{
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; }
}
}
35 changes: 35 additions & 0 deletions JsonFlatFileDataStore/DataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,41 @@ 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();
}

var temp = _jsonData.ToObject<T>();

return temp;
}

private dynamic SingleDynamicItemReadConverter(JToken e)
{
switch (e)
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

0 comments on commit 9d04821

Please sign in to comment.