Ejemplo n.º 1
0
        public static string StringInterpolation(string template, IReadonlyContext context)
        {
            dynamic expandoContext = new ExpandoObject();

            foreach (var property in context.Properties)
            {
                if (property is IParameterDescriptor parameter)
                {
                    ((IDictionary <string, object>)expandoContext)[parameter.Key] = context.TryGet(parameter, out var val) ? val : parameter.DefaultValue;
                }
            }

            var stringBuilder = new StringBuilder();
            var offset        = 0;

            void OffsetTo(int newOffset, bool write)
            {
                if (newOffset <= offset)
                {
                    return;
                }
                if (write)
                {
                    stringBuilder.Append(template.Substring(offset, newOffset - offset));
                }
                offset = newOffset;
            }

            for (;;)
            {
                var open = template.IndexOf("{{", offset, StringComparison.Ordinal);
                if (open == -1)
                {
                    break;
                }
                OffsetTo(open, true);
                var close = template.IndexOf("}}", open + 2, StringComparison.Ordinal);
                if (close == -1)
                {
                    OffsetTo(open + 2, true);
                    continue;
                }
                OffsetTo(close + 2, false);
                var script = template.Substring(open + 2, close - open - 2);
                try
                {
                    stringBuilder.Append(DslExpressionFactory.Create(script).Evaluate((IDictionary <string, object>)expandoContext));
                }
                catch (Exception e)
                {
                    Logger.Warn("GetInterpolatedString", e, "template", template, "script", script);
                    throw new UserException($"Failed to evaluate script: '{script}', template: '{template}'", e);
                }
            }
            OffsetTo(template.Length, true);
            return(stringBuilder.ToString());
        }
Ejemplo n.º 2
0
        public static string GetFormattedSessionDescriptor(this SessionConfig.Experiment experimentPart)
        {
            var     sessionName    = experimentPart.SessionDescriptor;
            dynamic expandoContext = new ExpandoObject();

            if (experimentPart.Params.Params != null && App.Instance.Registries.Registry <PluginExperiment>().LookUp(experimentPart.Params.Id, out var exp))
            {
                var context = exp.DeserializeParams(experimentPart.Params.Params);
                foreach (var group in exp.Factory.ParameterGroups)
                {
                    foreach (var parameter in @group.Parameters)
                    {
                        ((IDictionary <string, object>)expandoContext)[parameter.Key] = context.TryGet(parameter, out var val) ? val : parameter.DefaultValue;
                    }
                }
            }
            var stringBuilder = new StringBuilder();
            var offset        = 0;

            void OffsetTo(int newOffset, bool write)
            {
                if (newOffset <= offset)
                {
                    return;
                }
                if (write)
                {
                    stringBuilder.Append(sessionName.Substring(offset, newOffset - offset));
                }
                offset = newOffset;
            }

            while (true)
            {
                var open = sessionName.IndexOf("{{", offset, StringComparison.Ordinal);
                if (open == -1)
                {
                    break;
                }
                OffsetTo(open, true);
                var close = sessionName.IndexOf("}}", open + 2, StringComparison.Ordinal);
                if (close == -1)
                {
                    OffsetTo(open + 2, true);
                    continue;
                }
                OffsetTo(close + 2, false);
                var script = sessionName.Substring(open + 2, close - open - 2);
                try
                {
                    stringBuilder.Append(DslExpressionFactory.Create(script).Evaluate((IDictionary <string, object>)expandoContext));
                }
                catch (Exception e)
                {
                    Logger.Warn("GetFormattedSessionDescriptor", e, "sessionName", sessionName, "script", script);
                    throw new UserException($"Failed to evaluate script: '{script}', session name: '{sessionName}'", e);
                }
            }
            OffsetTo(sessionName.Length, true);
            return(stringBuilder.ToString());
        }