Skip to content

Commit 3e62efc

Browse files
authored
fix: use configured case with single item property names (#89)
1 parent 9db4d5d commit 3e62efc

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### [Unreleased]
44
* FIXED: Duplicate collection data to JSON on save when configured case was not used with collection name
5+
* FIXED: Duplicate single item data to JSON on save when configured case was not used with item name
56

67
### [2.4.1] - 2023-01-15
78
* FIXED: Ignore OperationCanceledException during dispose

JsonFlatFileDataStore.Test/DataStoreTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,22 @@ public void File_Has_Correct_PropertyNames_TypedCollection()
604604
var assertCollection = store.GetCollection<Employee>();
605605
Assert.Equal(3, assertCollection.Count);
606606
}
607+
608+
[Fact]
609+
public void File_Has_Correct_PropertyNames_Single_Item()
610+
{
611+
var path = UTHelpers.GetFullFilePath($"CreateNewFile_{DateTime.UtcNow.Ticks}");
612+
613+
var store = new DataStore(path);
614+
615+
store.ReplaceItem("TestOkIsThis1", 1, true);
616+
store.ReplaceItem("TestOkIsThis2", 2, true);
617+
store.ReplaceItem("TestOkIsThis2", 3, true);
618+
619+
var content = UTHelpers.GetFileContent(path);
620+
var propCount = Regex.Matches(content, "testOkIsThis2").Count;
621+
Assert.Equal(1, propCount);
622+
}
607623

608624
public class Employee
609625
{

JsonFlatFileDataStore/DataStore.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ public T GetItem<T>(string key)
132132
// This might be a bad idea especially if the file is in use, as this can take a long time
133133
_jsonData = GetJsonObjectFromFile();
134134
}
135+
136+
var convertedKey = _convertPathToCorrectCamelCase(key);
135137

136-
var token = _jsonData[key];
138+
var token = _jsonData[convertedKey];
137139

138140
if (token == null)
139141
{
@@ -156,7 +158,9 @@ public dynamic GetItem(string key)
156158
_jsonData = GetJsonObjectFromFile();
157159
}
158160

159-
var token = _jsonData[key];
161+
var convertedKey = _convertPathToCorrectCamelCase(key);
162+
163+
var token = _jsonData[convertedKey];
160164

161165
if (token == null)
162166
return null;
@@ -170,12 +174,14 @@ public dynamic GetItem(string key)
170174

171175
private Task<bool> Insert<T>(string key, T item, bool isAsync = false)
172176
{
177+
var convertedKey = _convertPathToCorrectCamelCase(key);
178+
173179
(bool, JObject) UpdateAction()
174180
{
175-
if (_jsonData[key] != null)
181+
if (_jsonData[convertedKey] != null)
176182
return (false, _jsonData);
177183

178-
_jsonData[key] = JToken.FromObject(item);
184+
_jsonData[convertedKey] = JToken.FromObject(item);
179185
return (true, _jsonData);
180186
}
181187

@@ -188,12 +194,14 @@ private Task<bool> Insert<T>(string key, T item, bool isAsync = false)
188194

189195
private Task<bool> Replace<T>(string key, T item, bool upsert = false, bool isAsync = false)
190196
{
197+
var convertedKey = _convertPathToCorrectCamelCase(key);
198+
191199
(bool, JObject) UpdateAction()
192200
{
193-
if (_jsonData[key] == null && upsert == false)
201+
if (_jsonData[convertedKey] == null && upsert == false)
194202
return (false, _jsonData);
195203

196-
_jsonData[key] = JToken.FromObject(item);
204+
_jsonData[convertedKey] = JToken.FromObject(item);
197205
return (true, _jsonData);
198206
}
199207

@@ -206,21 +214,23 @@ private Task<bool> Replace<T>(string key, T item, bool upsert = false, bool isAs
206214

207215
private Task<bool> Update(string key, dynamic item, bool isAsync = false)
208216
{
217+
var convertedKey = _convertPathToCorrectCamelCase(key);
218+
209219
(bool, JObject) UpdateAction()
210220
{
211-
if (_jsonData[key] == null)
221+
if (_jsonData[convertedKey] == null)
212222
return (false, _jsonData);
213223

214-
var toUpdate = SingleDynamicItemReadConverter(_jsonData[key]);
224+
var toUpdate = SingleDynamicItemReadConverter(_jsonData[convertedKey]);
215225

216226
if (ObjectExtensions.IsReferenceType(item) && ObjectExtensions.IsReferenceType(toUpdate))
217227
{
218228
ObjectExtensions.CopyProperties(item, toUpdate);
219-
_jsonData[key] = JToken.FromObject(toUpdate);
229+
_jsonData[convertedKey] = JToken.FromObject(toUpdate);
220230
}
221231
else
222232
{
223-
_jsonData[key] = JToken.FromObject(item);
233+
_jsonData[convertedKey] = JToken.FromObject(item);
224234
}
225235

226236
return (true, _jsonData);
@@ -235,9 +245,11 @@ private Task<bool> Update(string key, dynamic item, bool isAsync = false)
235245

236246
private Task<bool> Delete(string key, bool isAsync = false)
237247
{
248+
var convertedKey = _convertPathToCorrectCamelCase(key);
249+
238250
(bool, JObject) UpdateAction()
239251
{
240-
var result = _jsonData.Remove(key);
252+
var result = _jsonData.Remove(convertedKey);
241253
return (result, _jsonData);
242254
}
243255

0 commit comments

Comments
 (0)