/// <summary> /// Merges the specified object with this instance producing new one. /// </summary> /// <param name="other">The object to merge into this instance.</param> internal HoconObject MergeImmutable(HoconObject other) { var thisItems = new Dictionary <string, HoconValue>(Items); var otherItems = other.Items; foreach (var otherItem in otherItems) { //if other key was present in this object and if we have a value, //just ignore the other value, unless it is an object if (thisItems.TryGetValue(otherItem.Key, out var thisItem)) { //if both values are objects, merge them if (thisItem.IsObject() && otherItem.Value.IsObject()) { var mergedObject = thisItem.GetObject().MergeImmutable(otherItem.Value.GetObject()); var mergedValue = new HoconValue(); mergedValue.AppendValue(mergedObject); thisItems[otherItem.Key] = mergedValue; } } else { //other key was not present in this object, just copy it over thisItems.Add(otherItem.Key, new HoconValue(otherItem.Value.Values)); } } return(new HoconObject { Items = thisItems }); }
public void Merge(HoconObject other) { var thisItems = Items; var otherItems = other.Items; foreach (var otherItem in otherItems) { if (thisItems.ContainsKey(otherItem.Key)) { //other key was present in this object. //if we have a value, just ignore the other value, unless it is an object var thisItem = thisItems[otherItem.Key]; //if both values are objects, merge them if (thisItem.IsObject() && otherItem.Value.IsObject()) { thisItem.GetObject().Merge(otherItem.Value.GetObject()); } } else { //other key was not present in this object, just copy it over Items.Add(otherItem.Key, otherItem.Value); } } }
/// <summary> /// Wraps this <see cref="HoconValue"/> into a new <see cref="Config"/> object at the specified key. /// </summary> /// <param name="key">The key designated to be the new root element.</param> /// <returns>A <see cref="Config"/> with the given key as the root element.</returns> public Config AtKey(string key) { var o = new HoconObject(); o.GetOrCreateKey(key); o.Items[key] = this; var r = new HoconValue(); r.Values.Add(o); return new Config(new HoconRoot(r)); }
/// <summary> /// Wraps this <see cref="HoconValue"/> into a new <see cref="Config"/> object at the specified key. /// </summary> public Config AtKey(string key) { var o = new HoconObject(); o.GetOrCreateKey(key); o.Items[key] = this; var r = new HoconValue(); r.Values.Add(o); return(new Config(new HoconRoot(r))); }
private void ParseObject(HoconValue owner, bool root, string currentPath) { if (owner.IsObject()) { //the value of this KVP is already an object } else { //the value of this KVP is not an object, thus, we should add a new owner.NewValue(new HoconObject()); } HoconObject currentObject = owner.GetObject(); while (!_reader.EoF) { Token t = _reader.PullNext(); switch (t.Type) { case TokenType.Include: var included = _includeCallback(t.Value); var substitutions = included.Substitutions; foreach (var substitution in substitutions) { //fixup the substitution, add the current path as a prefix to the substitution path substitution.Path = currentPath + "." + substitution.Path; } _substitutions.AddRange(substitutions); var otherObj = included.Value.GetObject(); owner.GetObject().Merge(otherObj); break; case TokenType.EoF: break; case TokenType.Key: HoconValue value = currentObject.GetOrCreateKey(t.Value); var nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value; ParseKeyContent(value, nextPath); if (!root) { return; } break; case TokenType.ObjectEnd: return; } } }
/// <summary> /// Merges the specified object into this instance. /// </summary> /// <param name="other">The object to merge into this instance.</param> public void Merge(HoconObject other) { var thisItems = Items; var otherItems = other.Items; var modified = new List <KeyValuePair <string, HoconValue> >(); foreach (var otherItem in otherItems) { //if other key was present in this object and if we have a value, //just ignore the other value, unless it is an object if (thisItems.TryGetValue(otherItem.Key, out var thisItem)) { //if both values are objects, merge them if (thisItem.IsObject() && otherItem.Value.IsObject()) { var newObject = thisItem.GetObject().MergeImmutable(otherItem.Value.GetObject()); var value = new HoconValue(); value.Values.Add(newObject); modified.Add(new KeyValuePair <string, HoconValue>(otherItem.Key, value)); } else { modified.Add(new KeyValuePair <string, HoconValue>(otherItem.Key, otherItem.Value)); } } else { //other key was not present in this object, just copy it over modified.Add(new KeyValuePair <string, HoconValue>(otherItem.Key, otherItem.Value)); } } if (modified.Count == 0) { return; } foreach (var kvp in modified) { Items[kvp.Key] = kvp.Value; } }
private void ParseObject(HoconValue owner, bool root) { if (owner.IsObject()) { //the value of this KVP is already an object } else { //the value of this KVP is not an object, thus, we should add a new owner.NewValue(new HoconObject()); } HoconObject currentObject = owner.GetObject(); while (!reader.EoF) { Token t = reader.PullNext(); switch (t.Type) { case TokenType.EoF: break; case TokenType.Key: HoconValue value = currentObject.GetOrCreateKey(t.Value); ParseKeyContent(value); if (!root) { return; } break; case TokenType.ObjectEnd: return; } } }
/// <summary> /// Merges the specified object into this instance. /// </summary> /// <param name="other">The object to merge into this instance.</param> public void Merge(HoconObject other) { var thisItems = Items; var otherItems = other.Items; foreach (var otherItem in otherItems) { if (thisItems.ContainsKey(otherItem.Key)) { //other key was present in this object. //if we have a value, just ignore the other value, unless it is an object var thisItem = thisItems[otherItem.Key]; //if both values are objects, merge them if (thisItem.IsObject() && otherItem.Value.IsObject()) { thisItem.GetObject().Merge(otherItem.Value.GetObject()); } } else { //other key was not present in this object, just copy it over Items.Add(otherItem.Key,otherItem.Value); } } }
private void ParseObject(HoconValue owner, bool root, string currentPath) { try { PushDiagnostics("{"); if (owner.IsObject()) { //the value of this KVP is already an object } else { //the value of this KVP is not an object, thus, we should add a new owner.NewValue(new HoconObject()); } HoconObject currentObject = owner.GetObject(); while (!_reader.EoF) { Token t = _reader.PullNext(); switch (t.Type) { case TokenType.Include: var included = _includeCallback(t.Value); var substitutions = included.Substitutions; foreach (var substitution in substitutions) { //fixup the substitution, add the current path as a prefix to the substitution path substitution.Path = currentPath + "." + substitution.Path; } _substitutions.AddRange(substitutions); var otherObj = included.Value.GetObject(); owner.GetObject().Merge(otherObj); break; case TokenType.EoF: if (!string.IsNullOrEmpty(currentPath)) { throw new HoconParserException(string.Format("Expected end of object but found EoF {0}", GetDiagnosticsStackTrace())); } break; case TokenType.Key: HoconValue value = currentObject.GetOrCreateKey(t.Value); var nextPath = currentPath == "" ? t.Value : currentPath + "." + t.Value; ParseKeyContent(value, nextPath); if (!root) { return; } break; case TokenType.ObjectEnd: return; } } } finally { PopDiagnostics(); } }