public static FunctionWithManyArguments Create(VariableSource varSource) { return(new ConcreteHiOrderFunction( varSource, varSource.Type.FunTypeSpecification.Output, varSource.Type.FunTypeSpecification.Inputs)); }
protected UserVariableProviderBase(VariableSource source, string inputContextKey, ILogger logger) { Source = source; _inputContextKey = inputContextKey; _logger = logger; _propertyCacheDictionary = new ConcurrentDictionary <string, PropertyInfo>(); }
public void ResolvePriority(VariableSource expected, VariableSource competitor) { const string Key = "{839BA97E-9271-4D25-9453-434E269F8BDB}"; const string ExpectedValue = "expected"; const string CompetitorValue = "competitor"; // default Assert.IsNull(_sut.GetValue(Key)); // competitor, expected _sut.SetValue(competitor, Key, CompetitorValue); _sut.SetValue(expected, Key, ExpectedValue); Assert.AreEqual(ExpectedValue, _sut.GetValue(Key)); // try to remove by competitor _sut.SetValue(competitor, Key, null); Assert.AreEqual(ExpectedValue, _sut.GetValue(Key)); // try to remove by expected _sut.SetValue(expected, Key, null); Assert.IsNull(_sut.GetValue(Key)); // expected, competitor _sut.SetValue(expected, Key, ExpectedValue); _sut.SetValue(competitor, Key, CompetitorValue); Assert.AreEqual(ExpectedValue, _sut.GetValue(Key)); }
public void GetVariableByIdentifierWorksCorrectly() { var addressDict = new Dictionary <string, string> { { "street", "MyStreet" }, { "number", "1" }, { "app", "2" } }; var data = new Dictionary <string, object> { { "name", "MyName" }, { "address", addressDict }, { "phones", new List <string> { "123", "12345" } } }; var source = new VariableSource(data); Assert.AreEqual("MyName", source.GetVariable <string>("name")); Assert.AreEqual("MyStreet", source.GetVariable <string>("address.street")); Assert.AreEqual("12345", source.GetVariable <string>("phones.[1]")); Assert.Throws <VariableNotFoundException>((() => source.GetVariable <string>("address.streeets"))); Assert.Throws <IncorrectIdentifierException>((() => source.GetVariable <string>("name.street"))); Assert.Throws <IncorrectVariableTypeException>((() => source.GetVariable <int>("name"))); }
/// <summary> /// /// </summary> /// <param name="keyword"></param> public KeywordVariable(string keyword) { this.Keyword = keyword; string key = keyword.Replace("@", "").Trim(); int pointLocation = key.IndexOf('.'); if (pointLocation == -1) { _source = VariableSource.UnKnow; _keyname = key; } else { string sourceKey = key.Substring(0, pointLocation).ToLower(); if (Array.IndexOf(sourceArray, sourceKey) >= 0) { _source = (VariableSource)Enum.Parse(typeof(VariableSource), sourceKey, true); _keyname = key.Substring(pointLocation + 1); } else { _source = VariableSource.UnKnow; _keyname = key; } } }
protected override string ProcessControl(string variableIdentifier, IVariableSource variableSource, ContentControl contentControl, List <string> otherParameters) { try { var variable = variableSource.GetVariable(variableIdentifier); if (variable == null) { return(null); } //If the variable is not of a complex type or if the content control does not support nested controls, //(ie, is not a RichText control), then just return the string representation of the variable if (contentControl.Type != OpenXmlExtensions.ContentControlType.RichText || !(variable is Dictionary <string, object> innerData)) { return(variable.ToString()); } //If the variable is complex (dictionary) type and the control is rich text, we need to do //recursive replacement. For that we will add it to the queue var innerVariableSource = new VariableSource(innerData); Enqueue(new ControlReplacementExecutionData { Controls = contentControl.DescendingControls, VariableSource = innerVariableSource }); return(null); } catch (VariableNotFoundException) { return(null); } }
public void ReplacedConditionalDropdownContentControls() { var filePath = this.CurrentFolder() + "Doc.docx"; using var doc = new TemplateDocument(filePath); var json = File.ReadAllText(this.CurrentFolder() + "data.json"); var src = new VariableSource(); src.LoadDataFromJson(json); var replacer = new ConditionalDropdownControlReplacer(); replacer.ReplaceAll(doc, src); var c1 = doc.WordprocessingDocument.FindContentControl("conditional_isValid"); var c2 = doc.WordprocessingDocument.FindContentControl("conditional_isInvalid"); var c3 = doc.WordprocessingDocument.FindContentControl("conditional_isInvalid_or_isValid"); Assert.NotNull(c1); Assert.NotNull(c2); Assert.NotNull(c3); Assert.AreEqual("THIS IS VALID", c1.GetTextElement().Text); Assert.AreEqual("THIS IS VALID", c2.GetTextElement().Text); Assert.AreEqual("THIS IS VALID", c3.GetTextElement().Text); doc.WordprocessingDocument.AssertValid(); doc.SaveAs(this.CurrentFolder() + "result.docx"); doc.Close(); }
public void FindsContentControlAndReplacesSingulars() { var filePath = this.CurrentFolder() + "SingularsTestDoc.docx"; using var doc = new TemplateDocument(filePath); var json = File.ReadAllText(this.CurrentFolder() + "TemplatingsTestSingularsData.json"); var src = new VariableSource(); src.LoadDataFromJson(json); var singularReplacer = new SingularDropdownControlReplacer(); singularReplacer.ReplaceAll(doc, src); var c1 = doc.WordprocessingDocument.FindContentControl(singularReplacer.TagName + "_sellers"); var c2 = doc.WordprocessingDocument.FindContentControl(singularReplacer.TagName + "_buyers"); Assert.NotNull(c1); Assert.NotNull(c2); Assert.AreEqual("sellers are", c1.GetTextElement().Text); Assert.AreEqual("buyer", c2.GetTextElement().Text); doc.WordprocessingDocument.AssertValid(); doc.SaveAs(this.CurrentFolder() + "result.docx"); doc.Close(); }
public void DeclareVariable(string name, IMember value, VariableSource source, Location location, bool overwrite = true) { var member = GetInScope(name); if (member != null && !overwrite) { return; } if (source == VariableSource.Import && value is IVariable v) { CurrentScope.LinkVariable(name, v, location); return; } if (member != null) { if (!value.IsUnknown()) { CurrentScope.DeclareVariable(name, value, source, location); } } else { CurrentScope.DeclareVariable(name, value, source, location); } }
public void TestVariableControls() { using var doc = GetDoc; var data = GetData; var src = new VariableSource(data); // Substitute path for testing src.Data["picture1"] = this.CurrentFolder() + "/" + src.Data["picture1"]; if (src.Data["nested"] is IDictionary nested) { nested["picture1"] = this.CurrentFolder() + "/" + src.Data["picture1"]; } var replacer = new PictureControlReplacer(); replacer.ReplaceAll(doc, src); doc.SaveAs(this.CurrentFolder() + "result.docx"); Assert.AreEqual("DocumentFormat.OpenXml.Wordprocessing.SdtBlock", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "picture1").GetType() .ToString()); doc.WordprocessingDocument.AssertValid(); }
private IEnumerable <IVariableSource> allVariableSources(VariableSource variableSource) { foreach (var source in _method.Sources) { yield return(source); } foreach (var source in _class.Rules.Sources) { yield return(source); } yield return(_singletons); yield return(ServiceProviderVariableSource.Instance); //yield return new NoArgConcreteCreator(); if (variableSource == VariableSource.All) { yield return(_services); } }
public Variable(string name, IMember value, VariableSource source, Location location) : base(PythonMemberType.Variable, location) { Name = name ?? throw new ArgumentNullException(nameof(name)); Value = value is IVariable v ? v.Value : value; Source = source; }
public void DeclareVariable(string name, IMember value, VariableSource source, Location location, bool overwrite = true) { if (source == VariableSource.Import) { // Duplicate declaration so if variable gets overwritten it can still be retrieved. Consider: // from X import A // class A(A): ... CurrentScope.DeclareImported(name, value, location); } var member = GetInScope(name); if (member != null && !overwrite) { return; } if (source == VariableSource.Import && value is IVariable v) { CurrentScope.LinkVariable(name, v, location); return; } if (member != null) { if (!value.IsUnknown()) { CurrentScope.DeclareVariable(name, value, source, location); } } else { CurrentScope.DeclareVariable(name, value, source, location); } }
public Variable(string name, IMember value, VariableSource source, LocationInfo location = null) { Name = name; Value = value; Source = source; Location = location ?? (value is ILocatedMember lm ? lm.Location : LocationInfo.Empty); }
protected internal override Node VisitVariableSource(VariableSource node) { node = this.ValidateChildren(node); var variableType = this.Scope.GetVariableType(node.Variable); if (variableType == null) { this.AddError(node, $"Undeclared variable '{node.Variable}'."); } else if (!variableType.Interfaces.Contains(typeof(IDataSource))) { this.AddError(node, $"Variable {node.Variable} is not a data source."); } if (this.Scope.GetSource(node.Alias) != null) { this.AddError(node, $"There is already a source with alias {node.Alias}."); } else { this.Scope.AddSource(node.Alias, node); } this.Data.SetType(node, variableType); this.Data.SetScope(node, NodeScope.Constant); return(node); }
public void ReplacedConditionalDropdownContentControls() { var filePath = this.CurrentFolder() + "Doc.docx"; using var doc = new TemplateDocument(filePath); var json = File.ReadAllText(this.CurrentFolder() + "data.json"); var src = new VariableSource(); src.LoadDataFromJson(json); var replacer = new ConditionalRemoveControlReplacer(); replacer.ReplaceAll(doc, src); doc.SaveAs(this.CurrentFolder() + "result.docx"); Assert.IsNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled1")); Assert.NotNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled2")); Assert.NotNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled3")); Assert.NotNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled1_or_enabled2")); Assert.NotNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled3_or_enabled2")); Assert.IsNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled1_and_enabled2")); Assert.NotNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled1_not_and_enabled2")); Assert.IsNull(doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_enabled2_and_enabled3_not")); doc.WordprocessingDocument.AssertValid(); doc.Close(); }
public KLVariableDefinition(string name, bool isGlobal, VariableSource source, SourceContract sourceContract) { this.name = name; this.isGlobal = isGlobal; this.source = source; this.sourceContract = sourceContract; }
protected internal override Node VisitVariableSource(VariableSource node) { node = (VariableSource)base.VisitVariableSource(node); this.data.SetFactoryExpression(node, Evaluator.CreateDataSource(Expression.Convert(this.data.ConvertToLinqExpression(new VariableSqlExpression(node.Variable)), typeof(IDataSource)), node.Alias)); return(node); }
public KnownVariable(string n, string t, int l, VariableSource vs, string cn) { name = n; typestring = t; linenr = l; variablesource = vs; conceptname = cn; //Debug.Assert(cn == "" || vs != VariableSource.Concept); }
internal void DeclareVariable(string name, IMember value, VariableSource source, LocationInfo location) { name = !string.IsNullOrWhiteSpace(name) ? name : throw new ArgumentException(nameof(name)); if (_variables.TryGetValue(name, out var existing)) { existing.Assign(value, location); } else { _variables[name] = new Variable(name, value, source, location); } }
public KLVariableDefinition(KLVariableDefinition other) { this.name = other.name; this.defaultValue = other.defaultValue; this.isGlobal = other.isGlobal; this.source = other.source; this.content = other.content; this.min = other.min; this.max = other.max; this.sourceContract = other.sourceContract; }
public void GetValueEnvironmentVariable(VariableSource source, bool isOverridden) { const string Key = "TEMP"; Assert.IsNotNull(_sut.GetValue(Key)); Assert.AreEqual(Environment.GetEnvironmentVariable(Key), _sut.GetValue(Key)); _sut.SetValue(source, Key, "new value"); var expected = isOverridden ? "new value" : Environment.GetEnvironmentVariable(Key); Assert.AreEqual(expected, _sut.GetValue(Key)); }
public IExpressionNode Visit(ArrowAnonymFunctionSyntaxNode arrowAnonymFunNode) { if (arrowAnonymFunNode.Definition == null) { throw ErrorFactory.AnonymousFunDefinitionIsMissing(arrowAnonymFunNode); } if (arrowAnonymFunNode.Body == null) { throw ErrorFactory.AnonymousFunBodyIsMissing(arrowAnonymFunNode); } //Anonym fun arguments list var argumentLexNodes = arrowAnonymFunNode.ArgumentsDefinition; //Prepare local variable scope //Capture all outerscope variables var localVariables = new VariableDictionary(_variables.GetAllSources()); var arguments = new VariableSource[argumentLexNodes.Length]; var argIndex = 0; foreach (var arg in argumentLexNodes) { //Convert argument node var varNode = FunArgumentExpressionNode.CreateWith(arg); var source = VariableSource.CreateWithStrictTypeLabel(varNode.Name, varNode.Type, arg.Interval, isOutput: false); //collect argument arguments[argIndex] = source; argIndex++; //add argument to local scope if (!localVariables.TryAdd(source)) { //Check for duplicated arg-names //If outer-scope contains the conflict variable name if (_variables.GetSourceOrNull(varNode.Name) != null) { throw ErrorFactory.AnonymousFunctionArgumentConflictsWithOuterScope(varNode.Name, arrowAnonymFunNode.Interval); } else //else it is duplicated arg name { throw ErrorFactory.AnonymousFunctionArgumentDuplicates(varNode, arrowAnonymFunNode.Definition); } } } var body = arrowAnonymFunNode.Body; return(BuildAnonymousFunction(arrowAnonymFunNode.Interval, body, localVariables, arguments)); }
public Variable TryFindVariable(Type type, VariableSource source) { if (_variables.ContainsKey(type)) { return(_variables[type]); } var variable = findVariable(type, source); if (variable != null) { _variables.Add(type, variable); } return(variable); }
public void DeclareVariable(string name, IMember value, VariableSource source, LocationInfo location, bool overwrite = false) { var member = GetInScope(name); if (member != null) { if (!value.IsUnknown()) { CurrentScope.DeclareVariable(name, value, source, location); } } else { CurrentScope.DeclareVariable(name, value, source, location); } }
public IExpressionNode Visit(SuperAnonymFunctionSyntaxNode arrowAnonymFunNode) { var outputTypeFunDefinition = arrowAnonymFunNode.OutputType.FunTypeSpecification; if (outputTypeFunDefinition == null) { throw new ImpossibleException("Fun definition expected"); } string[] argNames = null; if (outputTypeFunDefinition.Inputs.Length == 1) { argNames = new[] { "it" } } ; else { argNames = new string[outputTypeFunDefinition.Inputs.Length]; for (int i = 0; i < outputTypeFunDefinition.Inputs.Length; i++) { argNames[i] = $"it{i + 1}"; } } //Prepare local variable scope //Capture all outerscope variables var localVariables = new VariableDictionary(_variables.GetAllSources()); var arguments = new VariableSource[argNames.Length]; for (var i = 0; i < argNames.Length; i++) { var arg = argNames[i]; var type = outputTypeFunDefinition.Inputs[i]; var source = VariableSource.CreateWithoutStrictTypeLabel(arg, type, false); //collect argument arguments[i] = source; //add argument to local scope //if argument with it* name already exist - replace it localVariables.AddOrReplace(source); } var body = arrowAnonymFunNode.Body; return(BuildAnonymousFunction(arrowAnonymFunNode.Interval, body, localVariables, arguments)); }
public void TestEngine() { using var doc = new TemplateDocument(this.CurrentFolder() + "Doc.docx"); var json = File.ReadAllText(this.CurrentFolder() + "data.json"); var src = new VariableSource(json); var engine = new DefaultOpenXmlTemplateEngine(); engine.ReplaceAll(doc, src); doc.SaveAs(this.CurrentFolder() + "result.docx"); var wd = doc.WordprocessingDocument; string GetText(string tagName, int elementIndex) => wd.FindContentControls(tagName).ElementAtOrDefault(elementIndex).GetTextElement().Text; Assert.AreEqual("Antoniantaz", GetText("variable_name", 0)); Assert.AreEqual("1", GetText("variable_index", 0)); Assert.AreEqual("2", GetText("variable_index", 1)); Assert.AreEqual("1", GetText("variable_index", 2)); Assert.AreEqual("2", GetText("variable_index", 3)); Assert.AreEqual(4, wd.FindContentControls("repeating_streets").Count()); Assert.AreEqual(2, wd.FindContentControls("variable_city").Count()); Assert.AreEqual(9, wd.FindContentControls("variable_name").Count()); Assert.AreEqual("Antoniantaz", GetText("variable_name", 1)); Assert.AreEqual("Antoniantaz", GetText("variable_name", 2)); Assert.AreEqual("Novigrad", GetText("variable_name", 3)); Assert.AreEqual("First street", GetText("variable_name", 4)); Assert.AreEqual("Second Street", GetText("variable_name", 5)); Assert.AreEqual("Khan", GetText("variable_name", 6)); Assert.AreEqual("Novigrad", GetText("variable_city.name", 0)); doc.WordprocessingDocument.AssertValid(); doc.Close(); }
public void TestVariableControls() { using var doc = GetDoc; var data = GetData; var src = new VariableSource(); src.LoadDataFromJson(data); var replacer = new VariableControlReplacer(); replacer.ReplaceAll(doc, src); doc.SaveAs(this.CurrentFolder() + "result.docx"); foreach (var namecc in doc.WordprocessingDocument.FindContentControls(replacer.TagName + "_" + "name")) { Assert.AreEqual("Antonio Conte", namecc.GetTextElement().Text); } Assert.AreEqual("Elm street", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "address.street").GetTextElement().Text); Assert.AreEqual("23", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "address.number").GetTextElement().Text); Assert.AreEqual("Novakovo", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "address.city.name").GetTextElement().Text); Assert.AreEqual("Plovdiv", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "address.city.province").GetTextElement().Text); var cc = doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "paragraph"); Assert.AreEqual(2, cc.Descendants <Break>().Count()); //Nested Assert.AreEqual("Elm street", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "street").GetTextElement().Text); Assert.AreEqual("Novakovo", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "name_city").GetTextElement().Text); Assert.AreEqual("Plovdiv", doc.WordprocessingDocument.FindContentControl(replacer.TagName + "_" + "province").GetTextElement().Text); doc.WordprocessingDocument.AssertValid(); }
private Variable findVariable(Type type, VariableSource variableSource) { var argument = _method.Arguments.Concat(_method.DerivedVariables).FirstOrDefault(x => x.VariableType == type); if (argument != null) { return(argument); } var created = _method.Frames.SelectMany(x => x.Creates).FirstOrDefault(x => x.VariableType == type); if (created != null) { return(created); } var source = allVariableSources(variableSource).FirstOrDefault(x => x.Matches(type)); return(source?.Create(type)); }
private IEnumerable <IVariableSource> allVariableSources(VariableSource variableSource) { foreach (var source in _method.Sources) { yield return(source); } foreach (var source in _type.Rules.Sources) { yield return(source); } // To get injected fields yield return(_type); if (variableSource == VariableSource.All && _services != null) { yield return(_services); } }
public BlockVariable(string name, VariableType type, VariableSource source) { Name = name; Type = type; Source = source; }