Beispiel #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());
        }
        public IParameterizedObject Create(IParameterDescriptor parameter, IReadonlyContext context)
        {
            var meta  = GetMeta(parameter);
            var value = meta.Constructor();

            foreach (var autoParameter in meta.Parameters)
            {
                autoParameter.SetMemberValue(value, context.TryGet(autoParameter, out var pv) ? pv : autoParameter.DefaultValue);
            }
            return(value);
        }
Beispiel #3
0
            public object Get(IReadonlyContext context)
            {
                var result = context.TryGet(_parameter, out var val) ? val : _parameter.DefaultValue;

                _parameter.IsValidOrThrow(result);
                if (!(_adapter?.IsValid(_parameter.Field, result) ?? true))
                {
                    throw new ArgumentException($"Value of field '{_parameter.Field}' is invalid");
                }
                return(result);
            }
        public static IDictionary <string, string> SerializeArgs(this IEnumerable <IParameterDescriptor> parameters, [CanBeNull] IReadonlyContext context)
        {
            if (context == null)
            {
                return(null);
            }
            var @params = new Dictionary <string, string>();

            foreach (var p in parameters)
            {
                if (context.TryGet(p, out var val))
                {
                    try { @params[p.Key] = p.SerializeParam(val); }
                    catch (Exception e) { Logger.Warn("SerializeArgs", e, "param", p.Key, "value", val); }
                }
            }
            return(@params);
        }