public ITemplateContext <TOutTemplate> Create(string input) { var templateContext = new TemplateContext <TOutTemplate>(); TemplateContainer rootContainer = null; try { rootContainer = JsonConvert.DeserializeObject <TemplateContainer>(input); } catch (JsonSerializationException ex) { templateContext.Errors.Add(new TemplateError(ex.Message)); } catch (JsonReaderException ex) { templateContext.Errors.Add(new TemplateError(ex.Message)); } if (rootContainer != null && IsValid(rootContainer, templateContext.Errors)) { templateContext.Template = BuildCollectionTemplate((JArray)rootContainer.Template, templateContext.Errors); } return(templateContext); }
public void GivenInvalidTemplateTargetType_WhenFactoryCreate_ThenNullReturned_Test() { var templateContainer = new TemplateContainer(); var factory = new IotJsonPathContentTemplateFactory(); var template = factory.Create(templateContainer); Assert.Null(template); }
public void GivenInvalidTemplateTargetType_WhenFactoryCreate_ThenInvalidTemplateExceptionThrown_Test() { var templateContainer = new TemplateContainer(); var factory = new CodeValueFhirTemplateFactory(); var ex = Assert.Throws <InvalidTemplateException>(() => factory.Create(templateContainer)); Assert.NotNull(ex); }
public void GivenInvalidTemplateBody_WhenFactoryCreate_ThenInvalidTemplateExceptionThrown_Test() { var templateContainer = new TemplateContainer { TemplateType = "IotJsonPathContentTemplate", Template = null, }; var factory = new IotJsonPathContentTemplateFactory(); var ex = Assert.Throws <InvalidTemplateException>(() => factory.Create(templateContainer)); Assert.NotNull(ex); }
private bool IsValid(TemplateContainer rootContainer, ICollection <TemplateError> errors) { if (!rootContainer.MatchTemplateName(TargetTemplateTypeName)) { errors.Add(new TemplateError($"Expected {nameof(rootContainer.TemplateType)} value {TargetTemplateTypeName}, actual {rootContainer.TemplateType}.")); return(false); } if (rootContainer.Template?.Type != JTokenType.Array) { errors.Add(new TemplateError($"Expected an array for the template property value for template type {TargetTemplateTypeName}.")); return(false); } return(true); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType != JsonToken.Null) { var lineNumber = 0; var linePosition = 0; if (reader is IJsonLineInfo lineInfoReader && lineInfoReader != null) { if (lineInfoReader.HasLineInfo()) { lineNumber = lineInfoReader.LineNumber; linePosition = lineInfoReader.LinePosition; } } var templateContainerObject = JObject.Load(reader); var innerTemplate = templateContainerObject.GetValue("template", StringComparison.InvariantCultureIgnoreCase); var templateContainer = new TemplateContainer(); serializer.Populate(templateContainerObject.CreateReader(), templateContainer); /** * At this point the TemplateConainer is fully populated but the inner 'Template' contains no line numbers. * Replace the 'Template' property with that of the templateContainerObject, which will contain line * information */ templateContainer.Template = innerTemplate; // Set line number details templateContainer.LineNumber = lineNumber; templateContainer.LinePosition = linePosition; templateContainer.SetLineInfoProperties(templateContainerObject); return(templateContainer); } return(null); }
public TTemplate Evaluate(TemplateContainer request) => throw new InvalidTemplateException($"No match found for template type {request.TemplateType}.");
public TTemplate Evaluate(TemplateContainer request) => throw new InvalidTemplateException($"No match found for template type {request.TemplateType}.", request.GetLineInfoForProperty(nameof(TemplateContainer.Template), true));