Exemple #1
0
        public void Typed_PartialOverlay()
        {
            var defaultOptions = new Options()
            {
                LastName  = "Smith",
                FirstName = "Fred",
                Age       = 22,
                Location  = new Location()
                {
                    Lat = 1.2312312F, Long = 3.234234F
                }
            };

            var overlay = new Options()
            {
                LastName = "Grant"
            };

            var result = ObjectPath.Merge(defaultOptions, overlay);

            Assert.AreEqual(result.LastName, overlay.LastName);
            Assert.AreEqual(result.FirstName, defaultOptions.FirstName);
            Assert.AreEqual(result.Age, defaultOptions.Age);
            Assert.AreEqual(result.Bool, defaultOptions.Bool);
            Assert.AreEqual(result.Location.Lat, defaultOptions.Location.Lat);
            Assert.AreEqual(result.Location.Long, defaultOptions.Location.Long);
        }
        public override async Task <DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (options is CancellationToken)
            {
                throw new ArgumentException($"{nameof(options)} cannot be a cancellation token");
            }

            object originalOptions = dc.GetState().GetValue <object>(ThisPath.OPTIONS);

            if (options == null)
            {
                options = originalOptions;
            }
            else if (originalOptions != null)
            {
                options = ObjectPath.Merge(options, originalOptions);
            }

            var targetDialogId = dc.Parent.ActiveDialog.Id;

            var repeatedIds = dc.GetState().GetValue <List <string> >(TurnPath.REPEATEDIDS, () => new List <string>());

            if (repeatedIds.Contains(targetDialogId))
            {
                throw new ArgumentException($"Recursive loop detected, {targetDialogId} cannot be repeated twice in one turn.");
            }

            repeatedIds.Add(targetDialogId);
            dc.GetState().SetValue(TurnPath.REPEATEDIDS, repeatedIds);

            var turnResult = await dc.Parent.ReplaceDialogAsync(dc.Parent.ActiveDialog.Id, options, cancellationToken).ConfigureAwait(false);

            turnResult.ParentEnded = true;
            return(turnResult);
        }
Exemple #3
0
        /// <summary>
        /// BindOptions - evaluate expressions in options.
        /// </summary>
        /// <param name="dc">dialog context.</param>
        /// <param name="options">options to bind.</param>
        /// <returns>merged options with expressions bound to values.</returns>
        protected virtual object BindOptions(DialogContext dc, object options)
        {
            // binding options are static definition of options with overlay of passed in options);
            var bindingOptions = (JObject)ObjectPath.Merge(this.Options.GetValue(dc.State), options ?? new JObject());
            var boundOptions   = new JObject();

            foreach (var binding in bindingOptions)
            {
                JToken value = null;

                // evaluate the value
                var(val, error) = new ValueExpression(binding.Value).TryGetValue(dc.State);

                if (error != null)
                {
                    throw new Exception(error);
                }

                if (val != null)
                {
                    value = JToken.FromObject(val).DeepClone();
                }

                value = value?.ReplaceJTokenRecursively(dc.State);

                // and store in options as the result
                ObjectPath.SetPathValue(boundOptions, binding.Key, value);
            }

            return(boundOptions);
        }
        /// <summary>
        /// BindOptions - evaluate expressions in options.
        /// </summary>
        /// <param name="dc">dialog context.</param>
        /// <param name="options">options to bind.</param>
        /// <returns>merged options with expressions bound to values.</returns>
        protected virtual object BindOptions(DialogContext dc, object options)
        {
            // binding options are static definition of options with overlay of passed in options);
            var bindingOptions = (JObject)ObjectPath.Merge(this.Options.GetValue(dc.State), options ?? new JObject());
            var boundOptions   = new JObject();

            foreach (var binding in bindingOptions)
            {
                // evaluate the value
                var value = new ValueExpression(binding.Value).EvaluateExpression(dc.State);

                // and store in options as the result
                ObjectPath.SetPathValue(boundOptions, binding.Key, value);
            }

            return(boundOptions);
        }
Exemple #5
0
        protected object BindOptions(DialogContext dc, object options)
        {
            // binding options are static definition of options with overlay of passed in options);
            var bindingOptions = (JObject)ObjectPath.Merge(Options, options ?? new JObject());
            var boundOptions   = new JObject();

            foreach (var binding in bindingOptions)
            {
                // evalute the value
                var(result, error) = new ExpressionEngine().Parse(binding.Value.ToString()).TryEvaluate(dc.GetState());

                if (error != null)
                {
                    throw new Exception(error);
                }

                // and store in options as the result
                boundOptions[binding.Key] = JToken.FromObject(result);
            }

            return(boundOptions);
        }
Exemple #6
0
        protected object BindOptions(DialogContext dc, object options)
        {
            // binding options are static definition of options with overlay of passed in options);
            var bindingOptions = (JObject)ObjectPath.Merge(this.Options.GetValue(dc.State), options ?? new JObject());
            var boundOptions   = new JObject();

            foreach (var binding in bindingOptions)
            {
                // evalute the value
                var(value, error) = new ValueExpression(binding.Value).TryGetValue(dc.State);

                if (error != null)
                {
                    throw new Exception(error);
                }

                // and store in options as the result
                ObjectPath.SetPathValue(boundOptions, binding.Key, value);
            }

            return(boundOptions);
        }