Esempio n. 1
0
        public virtual bool FillWithAttributes(IJSONDocument source, IJSONDocument target)
        {
            if (source != null && target != null)
            {
                IJsonValue wrapedValue;
                if (_fieldName.ToString().Equals("_key"))
                {
                    if (source.Contains("!_key"))
                    {
                        target.Key = source.GetString("!_key");
                    }
                    else
                    {
                        target.Add("!_key", source.Key);
                    }
                    return(true);
                }

                if (_fieldName.Evaluate(out wrapedValue, source))
                {
                    IAttributeChain attributor = Attributor.Create(Attributor.Delimit(_fieldName.InString, new[] { '$' }));
                    Attributor.TryUpdate(target, wrapedValue.Value, attributor, true);
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 2
0
        public bool Apply(IJSONDocument document)
        {
            var attribute = _targetField as Attribute;

            if (attribute != null)
            {
                switch (Type)
                {
                case UpdateType.Single:
                    IJsonValue value;
                    if (_evaluators[0].Value.Evaluate(out value, document))
                    {
                        return(Attributor.TryUpdate(document, value, attribute, true));
                    }
                    return(false);

                case UpdateType.Array:
                    Array array;
                    if (Attributor.TryGetArray(document, out array, attribute))
                    {
                        var values = new ClusteredArrayList(array.Length + _evaluators.Length);
                        values.AddRange(array);

                        bool isChangeApplicable = false;
                        foreach (var keyValuePair in _evaluators)
                        {
                            IJsonValue oldValue, newValue;

                            if (keyValuePair.Key.Evaluate(out oldValue, document) &&
                                keyValuePair.Value.Evaluate(out newValue, document))
                            {
                                int index = -1;
                                while ((index = values.IndexOf(oldValue.Value)) >= 0)
                                {
                                    //bugfix: infinite loop for replace 1=1
                                    //New and old values are same and values contain old value
                                    if (oldValue.CompareTo(newValue) == 0)
                                    {
                                        isChangeApplicable = true;
                                        break;
                                    }
                                    values[index]      = newValue.Value;
                                    isChangeApplicable = true;
                                }
                            }
                        }
                        return(isChangeApplicable && Attributor.TrySetArray(document, values.ToArray(), attribute));
                    }
                    return(false);
                }
                return(false);
            }
            return(false);
        }
Esempio n. 3
0
        public bool Apply(IJSONDocument document)
        {
            var attribute = _targetField as Attribute;

            if (attribute != null)
            {
                switch (Type)
                {
                case UpdateType.Single:
                    IJsonValue newValue;
                    if (_evaluator[0].Evaluate(out newValue, document))
                    {
                        return(Attributor.TryUpdate(document, newValue.Value, attribute, true));
                    }
                    return(false);

                case UpdateType.Array:
                    Array array;
                    if (Attributor.TryGetArray(document, out array, attribute))
                    {
                        var values = new ClusteredArrayList(array.Length + _evaluator.Length);
                        values.AddRange(array);

                        bool insertionDone = false;
                        foreach (var evaluable in _evaluator)
                        {
                            if (evaluable.Evaluate(out newValue, document))
                            {
                                if (!values.Contains(newValue.Value))
                                {
                                    values.Add(newValue.Value);
                                    insertionDone = true;
                                }
                            }
                        }
                        return(insertionDone && Attributor.TrySetArray(document, values.ToArray(), attribute));
                    }
                    return(false);
                }
                return(false);
            }
            return(false);
        }