Merged in bugfix/json-merge-arrays (pull request #4)

Bugfix/json merge arrays
This commit is contained in:
Heath Volmer
2019-02-12 20:06:14 +00:00
5 changed files with 70 additions and 22 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -75,23 +75,23 @@ namespace PepperDash.Core.Config
// Put together top-level objects // Put together top-level objects
if (system["info"] != null) if (system["info"] != null)
merged.Add("info", Merge(template["info"], system["info"])); merged.Add("info", Merge(template["info"], system["info"], "infO"));
else else
merged.Add("info", template["info"]); merged.Add("info", template["info"]);
merged.Add("devices", MergeArraysOnTopLevelProperty(template["devices"] as JArray, merged.Add("devices", MergeArraysOnTopLevelProperty(template["devices"] as JArray,
system["devices"] as JArray, "uid")); system["devices"] as JArray, "uid", "devices"));
if (system["rooms"] == null) if (system["rooms"] == null)
merged.Add("rooms", template["rooms"]); merged.Add("rooms", template["rooms"]);
else else
merged.Add("rooms", MergeArraysOnTopLevelProperty(template["rooms"] as JArray, merged.Add("rooms", MergeArraysOnTopLevelProperty(template["rooms"] as JArray,
system["rooms"] as JArray, "key")); system["rooms"] as JArray, "key", "rooms"));
if (system["sourceLists"] == null) if (system["sourceLists"] == null)
merged.Add("sourceLists", template["sourceLists"]); merged.Add("sourceLists", template["sourceLists"]);
else else
merged.Add("sourceLists", Merge(template["sourceLists"], system["sourceLists"])); merged.Add("sourceLists", Merge(template["sourceLists"], system["sourceLists"], "sourceLists"));
// Template tie lines take precedence. Config tool doesn't do them at system // Template tie lines take precedence. Config tool doesn't do them at system
// level anyway... // level anyway...
@@ -103,7 +103,7 @@ namespace PepperDash.Core.Config
merged.Add("tieLines", new JArray()); merged.Add("tieLines", new JArray());
if (system["global"] != null) if (system["global"] != null)
merged.Add("global", Merge(template["global"], system["global"])); merged.Add("global", Merge(template["global"], system["global"], "global"));
else else
merged.Add("global", template["global"]); merged.Add("global", template["global"]);
@@ -116,7 +116,7 @@ namespace PepperDash.Core.Config
/// given by propertyName. Returns a merge of them. Items in the delta array that do not have /// given by propertyName. Returns a merge of them. Items in the delta array that do not have
/// a matched item in base array will not be merged. /// a matched item in base array will not be merged.
/// </summary> /// </summary>
static JArray MergeArraysOnTopLevelProperty(JArray a1, JArray a2, string propertyName) static JArray MergeArraysOnTopLevelProperty(JArray a1, JArray a2, string propertyName, string path)
{ {
var result = new JArray(); var result = new JArray();
if (a2 == null) if (a2 == null)
@@ -130,7 +130,7 @@ namespace PepperDash.Core.Config
var a2Match = a2.FirstOrDefault(t => t[propertyName].Equals(a1Dev[propertyName]));// t.Value<int>("uid") == tmplDev.Value<int>("uid")); var a2Match = a2.FirstOrDefault(t => t[propertyName].Equals(a1Dev[propertyName]));// t.Value<int>("uid") == tmplDev.Value<int>("uid"));
if (a2Match != null) if (a2Match != null)
{ {
var mergedItem = Merge(a1Dev, a2Match);// Merge(JObject.FromObject(a1Dev), JObject.FromObject(a2Match)); var mergedItem = Merge(a1Dev, a2Match, string.Format("{0}[{1}].", path, i));// Merge(JObject.FromObject(a1Dev), JObject.FromObject(a2Match));
result.Add(mergedItem); result.Add(mergedItem);
} }
else else
@@ -144,31 +144,57 @@ namespace PepperDash.Core.Config
/// <summary> /// <summary>
/// Helper for using with JTokens. Converts to JObject /// Helper for using with JTokens. Converts to JObject
/// </summary> /// </summary>
static JObject Merge(JToken t1, JToken t2) static JObject Merge(JToken t1, JToken t2, string path)
{ {
return Merge(JObject.FromObject(t1), JObject.FromObject(t2)); return Merge(JObject.FromObject(t1), JObject.FromObject(t2), path);
} }
/// <summary> /// <summary>
/// Merge b ONTO a /// Merge o2 onto o1
/// </summary> /// </summary>
/// <param name="a"></param> /// <param name="a"></param>
/// <param name="b"></param> /// <param name="b"></param>
static JObject Merge(JObject o1, JObject o2) static JObject Merge(JObject o1, JObject o2, string path)
{ {
foreach (var o2Prop in o2) foreach (var o2Prop in o2)
{ {
var o1Value = o1[o2Prop.Key]; var propKey = o2Prop.Key;
var o1Value = o1[propKey];
var o2Value = o2[propKey];
// if the property doesn't exist on o1, then add it.
if (o1Value == null) if (o1Value == null)
o1.Add(o2Prop.Key, o2Prop.Value);
else
{ {
JToken replacement = null; o1.Add(propKey, o2Value);
if (o2Prop.Value.HasValues && o1Value.HasValues) // Drill down }
replacement = Merge(JObject.FromObject(o1Value), JObject.FromObject(o2Prop.Value)); // otherwise merge them
else else
replacement = o2Prop.Value; {
o1[o2Prop.Key].Replace(replacement); // Drill down
var propPath = String.Format("{0}.{1}", path, propKey);
try
{
if (o1Value is JArray)
{
if (o2Value is JArray)
{
o1Value.Replace(MergeArraysOnTopLevelProperty(o1Value as JArray, o2Value as JArray, "key", propPath));
}
}
else if (o2Prop.Value.HasValues && o1Value.HasValues)
{
o1Value.Replace(Merge(JObject.FromObject(o1Value), JObject.FromObject(o2Value), propPath));
}
else
{
o1Value.Replace(o2Prop.Value);
}
}
catch (Exception e)
{
Debug.Console(1, Debug.ErrorLogLevel.Warning, "Cannot merge items at path {0}: \r{1}", propPath, e);
}
} }
} }
return o1; return o1;

View File

@@ -57,6 +57,13 @@ namespace PepperDash.Core.JsonToSimpl
State = state; State = state;
Type = type; Type = type;
} }
public BoolChangeEventArgs(bool state, ushort type, ushort index)
{
State = state;
Type = type;
Index = index;
}
} }
//**************************************************************************************************// //**************************************************************************************************//
@@ -75,6 +82,13 @@ namespace PepperDash.Core.JsonToSimpl
IntValue = intValue; IntValue = intValue;
Type = type; Type = type;
} }
public UshrtChangeEventArgs(ushort intValue, ushort type, ushort index)
{
IntValue = intValue;
Type = type;
Index = index;
}
} }
//**************************************************************************************************// //**************************************************************************************************//
@@ -93,5 +107,13 @@ namespace PepperDash.Core.JsonToSimpl
StringValue = stringValue; StringValue = stringValue;
Type = type; Type = type;
} }
public StringChangeEventArgs(string stringValue, ushort type, ushort index)
{
StringValue = stringValue;
Type = type;
Index = index;
}
} }
} }

View File

@@ -3,5 +3,5 @@
[assembly: AssemblyTitle("Pepperdash_Core")] [assembly: AssemblyTitle("Pepperdash_Core")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Pepperdash_Core")] [assembly: AssemblyProduct("Pepperdash_Core")]
[assembly: AssemblyCopyright("Copyright © PepperDash 2016")] [assembly: AssemblyCopyright("Copyright © PepperDash 2019")]
[assembly: AssemblyVersion("1.0.11.*")] [assembly: AssemblyVersion("1.0.13.*")]