internal static string RegexReplaceOrValue(string input, string replacement, IReplacementValueOptions rv)
        {
            string value = replacement;

            if (rv != null)
            {
                if (rv.HasReplace)
                {
                    if (rv.IsBase64Encode)
                    {
                        replacement = CryptoHelpers.Encode(replacement);
                    }
                    value = Regex.Replace(input, rv.Replace, replacement, RegexOptions.IgnoreCase);
                }
                else if (rv.IsBase64Encode)
                {
                    value = CryptoHelpers.Encode(value);
                }
            }

            return(value);
        }
Esempio n. 2
0
        /// <summary>
        /// Examines (patch[0] is Dictionary<object, object>):
        ///     true:  remove metadata of indexValue (__sli) and continue recursion of objects
        ///     false: Clear source list, put patch values in: [source.Clear(); source.AddRange( patch );]
        /// </summary>
        /// <param name="source">The destination dict into which patch will be merged.</param>
        /// <param name="patch">The dict to merge in.</param>
        /// <param name="dv">Optional DynamicValue for specifying Regex/Crypto options of destinatio value.</param>
        internal static void ApplyPatchValues(List <object> source, List <object> patch, IReplacementValueOptions dv = null)
        {
            if (source == null)
            {
                throw new ArgumentException("Source cannot be null.", "source");
            }
            if (patch == null)
            {
                throw new ArgumentException("Patch cannot be null.", "patch");
            }

            //updated syntax to Pattern Matching: if patch[0] is Dict => patchItem
            if (patch[0] is Dictionary <object, object> patchItem && patchItem.ContainsKey(__sli))
            {
                //get the metadata index value, remove metadata
                int i = int.Parse(patchItem[__sli].ToString());
                patchItem.Remove(__sli);
                //get the value to updated/added at index 'i'
                object patchKey   = patchItem.ElementAt(0).Key;
                object patchValue = patchItem[patchKey];


                if (i >= 0 && i < source.Count)
                {
                    //updated syntax to Pattern Matching: if source[i] is Dict => listItemValue
                    if (source[i] is Dictionary <object, object> listItemValue)
                    {
                        if (patchValue is Dictionary <object, object> )
                        {
                            ApplyPatchValues((Dictionary <object, object>)listItemValue[patchKey], (Dictionary <object, object>)patchValue, dv);
                        }
                        else if (patchValue is List <object> )
                        {
                            if (!listItemValue.ContainsKey(patchKey))
                            {
                                listItemValue.Add(patchKey, new List <object>());
                            }

                            if (listItemValue[patchKey] is List <object> )
                            {
                                ApplyPatchValues((List <object>)listItemValue[patchKey], (List <object>)patchValue, dv);
                            }
                        }
                        else //if( patchValue is 'the value' )
                        {
                            listItemValue[patchKey] = patchValue;
                        }
                    }
                    else if (source[i] is List <object> )
                    {
                        //recurse back in
                        ApplyPatchValues((List <object>)source[i], (List <object>)patchKey, dv);
                    }
                    else
                    {
                        //update the value at index i
                        source[i] = RegexReplaceOrValue(source[i], patchValue, dv);
                    }
                }
                else //i is outside source lbound/ubound
                {
                    if (patchValue is Dictionary <object, object> )
                    {
                        source.Add(new Dictionary <object, object>()
                        {
                            { patchKey, new Dictionary <object, object>() }
                        });
                        ApplyPatchValues((Dictionary <object, object>)((Dictionary <object, object>)source[source.Count - 1])[patchKey],
                                         (Dictionary <object, object>)patchValue, dv);
                    }
                    else if (patchValue is List <object> )
                    {
                        source.Add(new Dictionary <object, object>()
                        {
                            { patchKey, new List <object>() }
                        });
                        ApplyPatchValues((Dictionary <object, object>)source[source.Count - 1], patchItem, dv);
                    }
                    else
                    {
                        //add the value to the collection, where [string.empty] is a placeholder for the non-existent value at index i
                        source.Add(RegexReplaceOrValue(string.Empty, patchValue, dv));
                    }
                }
            }
Esempio n. 3
0
        /// <summary>
        /// Downward recurse *patch* Dicts/Lists to the bottom**, then set value in source.
        /// **Bottom: Each patch[key] must exist as source[key], or all of patch[key] will be added at source[key]
        /// </summary>
        /// <param name="source">The destination dict into which patch will be merged.</param>
        /// <param name="patch">The dict to merge in.</param>
        /// <param name="dv">Optional DynamicValue for specifying Regex/Crypto options of destinatio value.</param>
        internal static void ApplyPatchValues(Dictionary <object, object> source, Dictionary <object, object> patch, IReplacementValueOptions dv = null)
        {
            if (source == null)
            {
                throw new ArgumentException("Source cannot be null.", "source");
            }
            if (patch == null)
            {
                throw new ArgumentException("Patch cannot be null.", "patch");
            }

            foreach (object key in patch.Keys)
            {
                if (source.ContainsKey(key))
                {
                    if (source[key] is Dictionary <object, object> )
                    {
                        ApplyPatchValues((Dictionary <object, object>)source[key], (Dictionary <object, object>)patch[key], dv);
                    }
                    else if (source[key] is List <object> )
                    {
                        ApplyPatchValues((List <object>)source[key], (List <object>)patch[key], dv);
                    }
                    else //( source[key] is string )
                    {
                        if (patch[key] is Dictionary <object, object> )
                        {
                            source[key] = new Dictionary <object, object>();
                            ApplyPatchValues((Dictionary <object, object>)source[key], (Dictionary <object, object>)patch[key], dv);
                        }
                        else if (patch[key] is List <object> )
                        {
                            source[key] = new List <object>();
                            ApplyPatchValues((List <object>)source[key], (List <object>)patch[key], dv);
                        }
                        else
                        {
                            source[key] = RegexReplaceOrValue(source[key], patch[key], dv);
                        }
                    }
                }
                else
                {
                    if (patch[key] is Dictionary <object, object> )
                    {
                        source[key] = new Dictionary <object, object>();
                        ApplyPatchValues((Dictionary <object, object>)source[key], (Dictionary <object, object>)patch[key], dv);
                    }
                    else if (patch[key] is List <object> )
                    {
                        source[key] = new List <object>();
                        ApplyPatchValues((List <object>)source[key], (List <object>)patch[key], dv);
                    }
                    else
                    {
                        source[key] = patch[key];
                    }
                }
            }
        }