public ProcessResult Process(ElementNode node, ProcessContext context = null, Dictionary <string, object> settings = null) { EnsureArg.IsNotNull(node); EnsureArg.IsNotNull(context?.VisitedNodes); EnsureArg.IsNotNull(settings); var substituteSetting = SubstituteSetting.CreateFromRuleSettings(settings); ElementNode replacementNode; // Get replacementNode for substitution if (ModelInfo.IsPrimitive(node.InstanceType)) { // Handle replaceWith value of string replacementNode = GetPrimitiveNode(substituteSetting.ReplaceWith); } else { // Handle replaceWith value of json object var replacementNodeType = ModelInfo.GetTypeForFhirType(node.InstanceType); if (replacementNodeType == null) { // Shall never throws here throw new Exception($"Node type is invalid at path {node.GetFhirPath()}."); } // Convert null object to empty object var replaceWith = substituteSetting.ReplaceWith ?? "{}"; var replaceElement = _parser.Parse(replaceWith, replacementNodeType).ToTypedElement(); replacementNode = ElementNode.FromElement(replaceElement); } var keepNodes = new HashSet <ElementNode>(); // Retrieve all nodes that have been processed before to keep _ = GenerateKeepNodeSetForSubstitution(node, context.VisitedNodes, keepNodes); var processResult = SubstituteNode(node, replacementNode, context.VisitedNodes, keepNodes); MarkSubstitutedFragmentAsVisited(node, context.VisitedNodes); return(processResult); }
public void GivenASubstituteSetting_WhenCreate_ReplacementValueShouldBeParsedCorrectly(Dictionary <string, object> config, string expectedValue) { var substituteSetting = SubstituteSetting.CreateFromRuleSettings(config); Assert.Equal(expectedValue, substituteSetting.ReplaceWith); }