/**
         * Do a Shift style insert :
         *  1) if there is no data "there", then just set it
         *  2) if there is already a list "there", just add the data to the list
         *  3) if there something other than a list there, grab it and stuff it and the data into a list
         *     and overwrite what is there with a list.
         */
        public override JToken HandleFinalSet(ITraversalStep traversalStep, JToken tree, string key, JToken data)
        {
            var optSub = traversalStep.Get(tree, key);

            if (optSub == null || optSub.Type == JTokenType.Null)
            {
                // nothing is here so just set the data
                traversalStep.OverwriteSet(tree, key, data);
            }
            else if (optSub is JArray arr)
            {
                // there is a list here, so we just add to it
                arr.Add(data);
            }
            else
            {
                // take whatever is there and make it the first element in an Array
                var temp = new JArray();
                temp.Add(optSub);
                temp.Add(data);

                traversalStep.OverwriteSet(tree, key, temp);
            }

            return(data);
        }
Example #2
0
        public Traversr(string humanPath)
        {
            string intermediatePath = humanPath.Replace("[", ".[");

            // given this replace and split strategy, we can end up with double dots, "..", which will generate an empty path element.
            // so remove any ".."  ;)
            intermediatePath = intermediatePath.Replace("..", ".");

            if (intermediatePath[0] == '.')
            {
                // if the path started with an array, aka "[0].tuna", remove the leading .
                intermediatePath = intermediatePath.Substring(1);
            }

            string[] paths = intermediatePath.Split('.');

            ITraversalStep rooty = null;

            for (int index = paths.Length - 1; index >= 0; index--)
            {
                rooty = MakePathElement(paths[index], rooty);
            }
            _traversaLength = paths.Length;
            _root           = rooty;
        }
Example #3
0
        /// <summary>
        /// Adds a traversal step to the IQuery.TraversalSteps Dictionary{int stepIndex, ITraversalStep step}.
        /// </summary>
        /// <param name="step">Step to add to the query.</param>
        /// <returns><see cref="ITraversalOperations"/> for chaining.</returns>
        protected ITraversalOperations AddTraversalStep(ITraversalStep step)
        {
            // Index the step.
            this.OwnerQuery.TraversalSteps[this.OwnerQuery.TraversalSteps.Count.ToString()] = step;

            // Return this ITraversalOperations for chaining.
            return(this);
        }
Example #4
0
        /**
         * Constructor where we provide a known good set of pathElement Strings in a list.
         * Aka, no need to extract it from a "Human Readable" form.
         */
        public Traversr(List <string> paths)
        {
            ITraversalStep rooty = null;

            for (int index = paths.Count - 1; index >= 0; index--)
            {
                rooty = MakePathElement(paths[index], rooty);
            }
            _traversaLength = paths.Count;
            _root           = rooty;
        }
        /**
         * Only make a new instance of a container object for SET, if there is nothing "there".
         */
        public override JToken HandleIntermediateGet(ITraversalStep traversalStep, JToken tree, string key, TraversalStepOperation op)
        {
            var sub = traversalStep.Get(tree, key);

            if ((sub == null || sub.Type == JTokenType.Null) && op == TraversalStepOperation.SET)
            {
                // get our child to make the container object, so it will be happy with it
                sub = traversalStep.GetChild().NewContainer();
                traversalStep.OverwriteSet(tree, key, sub);
            }

            return(sub);
        }
Example #6
0
 private ITraversalStep MakePathElement(string path, ITraversalStep child)
 {
     if ("[]" == path)
     {
         return(new AutoExpandArrayTraversalStep(this, child));
     }
     else if (path.StartsWith("[") && path.EndsWith("]"))
     {
         return(new ArrayTraversalStep(this, child));
     }
     else
     {
         return(new MapTraversalStep(this, child));
     }
 }
Example #7
0
 public MapTraversalStep(Traversr traversr, ITraversalStep child) :
     base(traversr, child)
 {
 }
Example #8
0
 public ArrayTraversalStep(Traversr traversr, ITraversalStep child) :
     base(traversr, child)
 {
 }
Example #9
0
 /**
  * Allow subclasses to control how gets are handled for intermediate traversals.
  *
  * Example: we are a MapTraversal and out key is "foo".
  *   We simply  do a 'tree.get( "foo" )'.  However, if we get a null back, or we get back
  *   a data type incompatible with our child Traversal, what do we do?
  *
  * Overwrite or just return?
  */
 public abstract JToken HandleIntermediateGet(ITraversalStep traversalStep, JToken tree, string key, TraversalStepOperation op);
Example #10
0
        // TODO extract these methods to an interface, and then sublasses of Traverser like ShiftrTraversr can do the
        //  Swing style "I implement the interface and pass myself down" trick.
        //  Means we can still can have a ShiftrTraversr, but less of a an explicit dependency inversion going
        //   on between the Traversr and its Traversals.

        /**
         * Allow subclasses to control how "sets" are done, if/once the traversal has made it to the the last element.
         *
         * Overwrite existing data?   List-ize existing data with new data?
         *
         * @return the data object if the set was successful, or null if not
         */
        public abstract JToken HandleFinalSet(ITraversalStep traversalStep, JToken tree, string key, JToken data);
 public BaseTraversalStep(Traversr traversr, ITraversalStep child)
 {
     _traversr = traversr;
     _child    = child;
 }
 public override JToken HandleFinalSet(ITraversalStep traversalStep, JToken tree, string key, JToken data)
 {
     return(traversalStep.OverwriteSet(tree, key, data));
 }
 /*--------------------------------------------------------------------------------------------*/
 internal FabResponse <T> Execute <T>(ITraversalStep <T> pStep,
                                      SessionType pSessionType) where T : FabObject
 {
     return(new FabricRequest <FabResponse <T> >("GET", vUri).Send(vContext, pSessionType));
 }