Example #1
0
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            if (transformations.KeysToRemove != null)
            {
                foreach (var key in transformations.KeysToRemove)
                {
                    if (String.IsNullOrWhiteSpace(key))
                    {
                        throw new ArgumentNullException("Transformation key is empty.", nameof(transformations));
                    }

                    source.RemoveKey(TransformationKey.Split(key));
                }
            }

            foreach (var transformation in transformations)
            {
                var transformationKey = new TransformationKey(transformation.Key);

                switch (transformationKey.Type)
                {
                case TransformationKeyType.Remove:
                    source.RemoveKey(transformationKey.Path);
                    break;

                case TransformationKeyType.AddToArray:
                    source.AddElementToArray(transformationKey.Path, transformation.Value);
                    break;

                case TransformationKeyType.Replace:
                    source.ReplaceKey(transformationKey.Path, transformation.Value);
                    break;

                default:
                    throw new InvalidOperationException($"Action is not defined for {nameof(TransformationKeyType)}.{transformationKey.Type}");
                }
            }

            return(source.ToString());
        }
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            if (transformations.KeysToRemove != null)
            {
                foreach (var key in transformations.KeysToRemove)
                {
                    if (String.IsNullOrWhiteSpace(key))
                    {
                        throw new ArgumentNullException("Transformation key is empty.", nameof(transformations));
                    }

                    source.RemoveKey(SplitKey(key));
                }
            }

            foreach (var transformation in transformations)
            {
                if (String.IsNullOrWhiteSpace(transformation.Key))
                {
                    throw new ArgumentException("Transformation key is empty.", nameof(transformations));
                }

                if (transformation.Key.StartsWith("#"))
                {
                    source.RemoveKey(SplitKey(transformation.Key.TrimStart('#')));
                }
                else if (RemoveEndingRegex.Replace(transformation.Key, String.Empty).EndsWith("[]"))
                {
                    source.AddElementToArray(SplitKey(transformation.Key), transformation.Value);
                }
                else
                {
                    source.ReplaceKey(SplitKey(transformation.Key), transformation.Value);
                }
            }

            return(source.ToString());
        }