public void Parse_ReplacesWildcards()
        {
            const string input  = "insert into #0 (name) values ('#1')";
            var          record = GetRecord("Person", "Jens");
            var          code   = _templateEvaluator.Evaluate(input, record);

            Assert.That(code, Is.EqualTo("insert into Person (name) values ('Jens')"));
        }
Esempio n. 2
0
        public Dictionary <String, String> EvaluateJobDefaultsRun(
            TemplateToken token,
            DictionaryContextData contextData)
        {
            var result = default(Dictionary <String, String>);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.JobDefaultsRun, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = new Dictionary <String, String>(StringComparer.OrdinalIgnoreCase);
                    var mapping = token.AssertMapping("defaults run");
                    foreach (var pair in mapping)
                    {
                        // Literal key
                        var key = pair.Key.AssertString("defaults run key");

                        // Literal value
                        var value = pair.Value.AssertString("defaults run value");
                        result[key.Value] = value.Value;
                    }
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 3
0
        public JobContainer EvaluateJobContainer(
            TemplateToken token,
            DictionaryContextData contextData)
        {
            var result = default(JobContainer);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.Container, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToJobContainer(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 4
0
        public Int32 EvaluateStepTimeout(
            TemplateToken token,
            DictionaryContextData contextData)
        {
            var result = default(Int32?);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.NumberStepsContext, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToStepTimeout(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? 0);
        }
Esempio n. 5
0
        public Dictionary <String, String> EvaluateStepEnvironment(
            TemplateToken token,
            DictionaryContextData contextData,
            StringComparer keyComparer)
        {
            var result = default(Dictionary <String, String>);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StepEnv, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToStepEnvironment(context, token, keyComparer);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? new Dictionary <String, String>(keyComparer));
        }
Esempio n. 6
0
        public DictionaryContextData EvaluateStepScopeOutputs(
            TemplateToken token,
            DictionaryContextData contextData)
        {
            var result = default(DictionaryContextData);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StepsScopeOutputs, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = token.ToContextData().AssertDictionary("steps scope outputs");
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? new DictionaryContextData());
        }
Esempio n. 7
0
        public string EvaluateDefaultInput(
            IExecutionContext executionContext,
            string inputName,
            TemplateToken token)
        {
            string result = "";

            if (token != null)
            {
                var context = CreateContext(executionContext);
                try
                {
                    var evaluateResult = TemplateEvaluator.Evaluate(context, "input-default-context", token, 0, null, omitHeader: true);
                    context.Errors.Check();

                    Trace.Info($"Input '{inputName}': default value evaluate result: {StringUtil.ConvertToJson(evaluateResult)}");

                    // String
                    result = evaluateResult.AssertString($"default value for input '{inputName}'").Value;
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    Trace.Error(ex);
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 8
0
        public DictionaryContextData EvaluateCompositeOutputs(
            IExecutionContext executionContext,
            TemplateToken token,
            IDictionary <string, PipelineContextData> extraExpressionValues)
        {
            var result = default(DictionaryContextData);

            if (token != null)
            {
                var context = CreateContext(executionContext, extraExpressionValues);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, "outputs", token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = token.ToContextData().AssertDictionary("composite outputs");
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? new DictionaryContextData());
        }
Esempio n. 9
0
        public Dictionary <String, String> EvaluateStepInputs(
            TemplateToken token,
            DictionaryContextData contextData,
            IList <IFunctionInfo> expressionFunctions)
        {
            var result = default(Dictionary <String, String>);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData, expressionFunctions);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StepWith, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToStepInputs(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? new Dictionary <String, String>(StringComparer.OrdinalIgnoreCase));
        }
Esempio n. 10
0
        public String EvaluateStepDisplayName(
            TemplateToken token,
            DictionaryContextData contextData,
            IList <IFunctionInfo> expressionFunctions)
        {
            var result = default(String);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData, expressionFunctions);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StringStepsContext, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToStepDisplayName(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 11
0
        /// <summary>
        /// Evaluates a given expression as if it were the value of a variable.
        /// </summary>
        /// <param name="expressionOrVariableOrText">The value or expression to evaluate.</param>
        /// <param name="error">Any parsing errors silently found.</param>
        /// <param name="haltOnError">Stop parsing if an error is found.</param>
        /// <returns>The result of the expression.</returns>
        public string Evaluate(string expressionOrVariableOrText, out string error, bool haltOnError = true)
        {
            error = null;
            if (expressionOrVariableOrText == null)
            {
                return(null);
            }

            if (CanEvaluationBeSkippedForExpression(expressionOrVariableOrText))
            {
                return(expressionOrVariableOrText);
            }

            Template template;

            if (!TemplateParser.TryParseTemplate(expressionOrVariableOrText, out template, out error, haltOnError))
            {
                return(expressionOrVariableOrText);
            }

            using (var writer = new StringWriter())
            {
                string[] missingTokens;
                TemplateEvaluator.Evaluate(template, Binding, writer, out missingTokens);
                if (missingTokens.Any())
                {
                    var tokenList = string.Join(", ", missingTokens.Select(token => "'" + token + "'"));
                    error = string.Format("The following tokens were unable to be evaluated: {0}", tokenList);
                }


                return(writer.ToString());
            }
        }
Esempio n. 12
0
        public IList <KeyValuePair <String, JobContainer> > EvaluateJobServiceContainers(
            TemplateToken token,
            DictionaryContextData contextData,
            IList <IFunctionInfo> expressionFunctions)
        {
            var result = default(List <KeyValuePair <String, JobContainer> >);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData, expressionFunctions);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.Services, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToJobServiceContainers(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 13
0
        public TemplateToken EvaluateEnvironmentUrl(
            TemplateToken token,
            DictionaryContextData contextData,
            IList <IFunctionInfo> expressionFunctions)
        {
            var result = default(TemplateToken);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData, expressionFunctions);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, TemplateConstants.StringRunnerContextNoSecrets, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = token.AssertString("environment.url");
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
        public List <ActionStep> LoadCompositeSteps(
            TemplateToken token
            )
        {
            var result = default(List <ActionStep>);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(null, null, setMissingContext: false);
                // TODO: we might want to to have a bool to prevent it from filling in with missing context w/ dummy variables
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StepsInTemplate, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToSteps(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }
            return(result);
        }
Esempio n. 15
0
        public Boolean EvaluateStepIf(
            TemplateToken token,
            DictionaryContextData contextData,
            IList <IFunctionInfo> expressionFunctions,
            IEnumerable <KeyValuePair <String, Object> > expressionState)
        {
            var result = default(Boolean?);

            if (token != null && token.Type != TokenType.Null)
            {
                var context = CreateContext(contextData, expressionFunctions, expressionState);
                try
                {
                    token = TemplateEvaluator.Evaluate(context, PipelineTemplateConstants.StepIfResult, token, 0, null, omitHeader: true);
                    context.Errors.Check();
                    result = PipelineTemplateConverter.ConvertToIfResult(context, token);
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result ?? throw new InvalidOperationException("Step if cannot be null"));
        }
Esempio n. 16
0
        public void Evaluate_template_that_has_no_expressions()
        {
            TemplateEvaluator templateEvaluator = new TemplateEvaluator();

            string[] actualEvaluations = templateEvaluator.Evaluate(StringConstants.TemplateWithNoExpressions);

            Assert.Equal(StringConstants.TemplateWithNoExpressions, actualEvaluations[0]);
        }
Esempio n. 17
0
        public Dictionary <string, string> EvaluateContainerEnvironment(
            IExecutionContext executionContext,
            MappingToken token,
            IDictionary <string, PipelineContextData> extraExpressionValues)
        {
            var result = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            if (token != null)
            {
                var context = CreateContext(executionContext, extraExpressionValues);
                try
                {
                    var evaluateResult = TemplateEvaluator.Evaluate(context, "container-runs-env", token, 0, null, omitHeader: true);
                    context.Errors.Check();

                    Trace.Info($"Environments evaluate result: {StringUtil.ConvertToJson(evaluateResult)}");

                    // Mapping
                    var mapping = evaluateResult.AssertMapping("container env");

                    foreach (var pair in mapping)
                    {
                        // Literal key
                        var key = pair.Key.AssertString("container env key");

                        // Literal value
                        var value = pair.Value.AssertString("container env value");
                        result[key.Value] = value.Value;

                        Trace.Info($"Add env {key} = {value}");
                    }
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    Trace.Error(ex);
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }
Esempio n. 18
0
        public List <string> EvaluateContainerArguments(
            IExecutionContext executionContext,
            SequenceToken token,
            IDictionary <string, PipelineContextData> extraExpressionValues)
        {
            var result = new List <string>();

            if (token != null)
            {
                var context = CreateContext(executionContext, extraExpressionValues);
                try
                {
                    var evaluateResult = TemplateEvaluator.Evaluate(context, "container-runs-args", token, 0, null, omitHeader: true);
                    context.Errors.Check();

                    Trace.Info($"Arguments evaluate result: {StringUtil.ConvertToJson(evaluateResult)}");

                    // Sequence
                    var args = evaluateResult.AssertSequence("container args");

                    foreach (var arg in args)
                    {
                        var str = arg.AssertString("container arg").Value;
                        result.Add(str);
                        Trace.Info($"Add argument {str}");
                    }
                }
                catch (Exception ex) when(!(ex is TemplateValidationException))
                {
                    Trace.Error(ex);
                    context.Errors.Add(ex);
                }

                context.Errors.Check();
            }

            return(result);
        }