/// <summary> /// Substitutes the property tokens in the supplied string. /// </summary> /// <param name="input">The input string.</param> /// <param name="parameters">The parameters.</param> /// <param name="required">A value indicating whether the parameter is required.</param> /// <returns>The substituted string</returns> internal static string SubstituteTokensInString(string input, IDictionary <string, string> parameters, bool required) { string output = input; MatchCollection paramsMatches = ParameterRegex.Matches(output); while (paramsMatches.Count > 0) { Match paramsMatch = paramsMatches[paramsMatches.Count - 1]; string parameterValue; if (!parameters.TryGetValue(paramsMatch.Groups[1].Value, out parameterValue)) { if (required) { throw new TemplateParserException($"Value for parameter '{paramsMatch.Groups[1].Value}' has not been provided."); } parameterValue = string.Empty; } if (MacroUtilities.IsNested(output, paramsMatch)) { parameterValue = "'" + parameterValue + "'"; } output = output.Replace(paramsMatch.Groups[0].Value, parameterValue); paramsMatches = ParameterRegex.Matches(output); } return(output); }
public void ShouldExpandUnquotedSymbol() { Parser parser = new Parser("(unquote x)"); object list = parser.Compile(); ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("x", "y"); object result = MacroUtilities.Expand(list, environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(string)); Assert.AreEqual("y", result); }
public void ShouldExpandSimpleList() { Parser parser = new Parser("(1 2 3)"); object array = parser.Compile(); object result = MacroUtilities.Expand(array, null); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(List)); List resultList = (List)result; Assert.AreEqual(1, resultList.First); Assert.AreEqual(2, resultList.Next.First); Assert.AreEqual(3, resultList.Next.Next.First); Assert.IsNull(resultList.Next.Next.Next); }
public void ShouldExpandImplicitUnquotedSymbolInList() { Parser parser = new Parser("(1 ~x 3)"); object list = parser.Compile(); ValueEnvironment environment = new ValueEnvironment(); environment.SetValue("x", 2); object result = MacroUtilities.Expand(list, environment); Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(List)); List resultList = (List)result; Assert.AreEqual(1, resultList.First); Assert.AreEqual(2, resultList.Next.First); Assert.AreEqual(3, resultList.Next.Next.First); Assert.IsNull(resultList.Next.Next.Next); }
/// <summary> /// Substitutes the property tokens in the supplied string. /// </summary> /// <param name="runtimeContext">The runtime context.</param> /// <param name="taskContext">The task execution context.</param> /// <param name="input">The input string.</param> /// <returns>The substituted string</returns> public async Task <string> SubstituteTokensInString(RuntimeContext runtimeContext, TaskContext taskContext, string input) { return(await Task.Run(() => { string output = input; if (taskContext.ResourcesProperties != null) { MatchCollection resourceMatches = ResourcePropertyRegex.Matches(output); while (resourceMatches.Count > 0) { Match resourceMatch = resourceMatches[resourceMatches.Count - 1]; string resourceId = resourceMatch.Groups[1].Value; string property = resourceMatch.Groups[2].Value; JObject resource; if (!taskContext.ResourcesProperties.TryGetValue(resourceId, out resource)) { throw new TemplateParserException($"Referenced resource '{resourceId}' not found."); } var newValue = resource.SelectToken(property).Value <string>(); if (MacroUtilities.IsNested(output, resourceMatch)) { newValue = "'" + newValue + "'"; } output = output.Replace(resourceMatch.Groups[0].Value, newValue); resourceMatches = ResourcePropertyRegex.Matches(output); } } return output; })); }
public void ShouldExpandSymbol() { Identifier identifier = new Identifier("foo"); Assert.AreEqual(identifier, MacroUtilities.Expand(identifier, null)); }
public void ShouldExpandConstants() { Assert.AreEqual(1, MacroUtilities.Expand(1, null)); Assert.AreEqual("foo", MacroUtilities.Expand("foo", null)); }
public override object Execute(object argument, ValueEnvironment environment) { return(MacroUtilities.Expand(argument, environment)); }