protected override UpdatedItem DoUpdate(JObject target, ISourceMatch source) { var propertyName = this.GetPropertyName(source); if (!this.ShouldMap(target.ContainsKey(propertyName))) { return(UpdatedItem.Notupdated); } if (this.IsArray) { var targetProperty = target.Property(propertyName); if (targetProperty == null || !(targetProperty.Value is JArray targetArray)) { targetArray = new JArray(); target[propertyName] = targetArray; } var targetElement = source.Element.DeepClone(); targetArray.Add(targetElement); return(targetElement.AsUpdated()); } return(Merging.MergeToProperty(target, propertyName, source.Element).AsUpdated()); }
public static void MergeObjects(JObject target, ISourceMatch source) { if (!(source.Element is JObject sourceObj)) { throw new ApplicationException("Only an object can be merged with an object."); } MergeObjects(target, sourceObj); }
public string Get(ISourceMatch source) { var builder = new StringBuilder(); foreach (var token in this.tokens) { builder.Append( token.Type == TokenType.Variable ? source.VariableValues[token.Value] : token.Value); } return(builder.ToString()); }
public bool Update(JObject target, ISourceMatch source) { var updated = this.DoUpdate(target, source); if (updated.WasUpdated && this.propertySetters != null && updated.Item is JObject updatedObj) { foreach (var setter in this.propertySetters) { setter.Set(updatedObj, source); } } return(updated.WasUpdated); }
protected override UpdatedItem DoUpdate(JObject target, ISourceMatch source) { var propertyName = this.GetPropertyName(source); var targetProperty = target.Property(propertyName); if (!this.ShouldMap(targetProperty != null)) { return(UpdatedItem.Notupdated); } if (!this.IsArray && targetProperty != null && targetProperty.Value is JObject targetObj) { return(this.next.Update(targetObj, source) ? targetObj.AsUpdated() : UpdatedItem.Notupdated); } // If the target does not have an object to update then create it // and try to map to it, if that succeeds then assign to the target // either as the object or into the array (if this is an array) targetObj = new JObject(); if (this.next.Update(targetObj, source)) { if (!this.IsArray) { target[propertyName] = targetObj; } else { if (targetProperty == null || !(targetProperty.Value is JArray targetArr)) { targetArr = new JArray(); target[propertyName] = targetArr; } targetArr.Add(targetObj); } return(targetObj.AsUpdated()); } return(UpdatedItem.Notupdated); }
public string Get(ISourceMatch source) => this.name;
protected string GetPropertyName(ISourceMatch source) => this.propertyName.Get(source);
protected abstract UpdatedItem DoUpdate(JObject target, ISourceMatch source);