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

Updated GetNextIdValue to get actual next Id #13

Open
wants to merge 1 commit 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
17 changes: 10 additions & 7 deletions JsonFlatFileDataStore.Test/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ public void GetNextIdValue_IntegerId()
var nextId = collection.GetNextIdValue();
Assert.Equal(4, nextId);

collection.InsertOne(new { id = nextId + 1 });
collection.InsertOne(new { id = nextId });

nextId = collection.GetNextIdValue();
Assert.Equal(5, nextId);
Assert.Equal(6, nextId);
}

[Fact]
Expand Down Expand Up @@ -124,7 +125,7 @@ public void GetNextIdValue_StringType_AnonymousType()
collection.InsertOne(new { myId = "hello2" });

nextId = collection.GetNextIdValue();
Assert.Equal("hello3", nextId);
Assert.Equal("hello5", nextId);

var same = collection.AsQueryable().Where(e => e.myId == "hello2");
Assert.Equal(2, same.Count());
Expand All @@ -140,25 +141,27 @@ public void GetNextIdValue_StringType_JToken()
var collection = store.GetCollection("collectionWithStringId");

// Insert seed value with upsert
collection.ReplaceOne(e => e, JToken.Parse("{ 'myId': 'test1' }"), true);
collection.ReplaceOne(e => true, JToken.Parse("{ 'myId': 'test1' }"), true);
collection.InsertOne(JToken.Parse("{ 'myId': 'test3' }"));
collection.InsertOne(JToken.Parse("{ 'myId': 'test2' }"));

var nextId = collection.GetNextIdValue();
Assert.Equal("test2", nextId);
Assert.Equal("test4", nextId);

var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong2' }");
var nextUpdate = JToken.Parse("{ 'myId': 'somethingWrong4' }");
collection.InsertOne(nextUpdate);
Assert.Equal(nextId, nextUpdate["myId"]);

nextId = collection.GetNextIdValue();
Assert.Equal("test3", nextId);
Assert.Equal("test5", nextId);

nextUpdate = JToken.Parse("{ 'xxx': 111 }");
collection.InsertOne(nextUpdate);
Assert.Equal(nextId, nextUpdate["myId"]);
Assert.Equal(111, nextUpdate["xxx"]);

nextId = collection.GetNextIdValue();
Assert.Equal("test4", nextId);
Assert.Equal("test6", nextId);
}

[Fact]
Expand Down
29 changes: 19 additions & 10 deletions JsonFlatFileDataStore/DocumentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,20 +372,29 @@ private dynamic GetNextIdValue(List<T> data)
if (!data.Any())
return 0;

var lastItem = data.Last();
var expando = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(lastItem), new ExpandoObjectConverter());
// Problem here is if we have typed data with upper camel case properties but lower camel case in JSON, so need to use OrdinalIgnoreCase string comparer
var expandoAsIgnoreCase = new Dictionary<string, dynamic>(expando, StringComparer.OrdinalIgnoreCase);
var idValues = data.Select(item =>
{
var expando = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(item), new ExpandoObjectConverter());
// Problem here is if we have typed data with upper camel case properties but lower camel case in JSON, so need to use OrdinalIgnoreCase string comparer
var expandoIgnoreCase = new Dictionary<string, dynamic>(expando, StringComparer.OrdinalIgnoreCase);

if (!expandoIgnoreCase.ContainsKey(_idField))
{
return null;
}

return expandoIgnoreCase[_idField];
});

if (!expandoAsIgnoreCase.ContainsKey(_idField))
return null;
var maxIdValue = idValues.Max();

dynamic keyValue = expandoAsIgnoreCase[_idField];
if (maxIdValue == null)
return 0;

if (keyValue is Int64)
return (int)keyValue + 1;
if (maxIdValue is Int64)
return (int)maxIdValue + 1;

return ParseNextIntegertToKeyValue(keyValue.ToString());
return ParseNextIntegertToKeyValue(maxIdValue.ToString());
}

private string ParseNextIntegertToKeyValue(string input)
Expand Down