public void Navigate_ContentListField_Null() { var content = new TestPageContent(); var explorer = ContentExplorer.Create(metadataManager, content, "Headers[0]"); Assert.Null(explorer); }
public void Navigate_ContentField() { var content = new TestPageContent { Header = new PageHeaderContent() }; var explorer = ContentExplorer.Create(metadataManager, content, "Header"); Assert.NotNull(explorer); }
private ContentExplorer(object model, ContentMetadataProvider contentMetadata) { Field = null; Model = model; Metadata = contentMetadata; ModelPath = string.Empty; Parent = null; rootExplorer = null; name = contentMetadata.Name; }
private ContentProvider(TEntry page, object model, IServiceProvider services) { if (model == null) { throw new ArgumentNullException(nameof(model)); } Entry = page ?? throw new ArgumentNullException(nameof(page)); Services = services ?? throw new ArgumentNullException(nameof(services)); var contentMetadataManager = services.GetRequiredService <IContentMetadataManager>(); Explorer = ContentExplorer.Create(contentMetadataManager, model); }
public void Create_Root() { var content = new TestPageContent(); var explorer = ContentExplorer.Create(metadataManager, content); Assert.NotNull(explorer); Assert.NotNull(explorer.Metadata); Assert.Null(explorer.Field); Assert.Equal(content, explorer.Model); Assert.Equal(string.Empty, explorer.ModelPath); Assert.Equal(content.Title, explorer.Title); Assert.Null(explorer.Root); Assert.Null(explorer.Parent); Assert.Equal(-1, explorer.Index); Assert.True(explorer.IsRoot); }
public void Create_SpecifyPath() { var content = new TestPageContent { Header = new PageHeaderContent() }; var explorer = ContentExplorer.Create(metadataManager, content, "Header"); Assert.NotNull(explorer); Assert.NotNull(explorer.Metadata); Assert.NotNull(explorer.Field); Assert.Equal(explorer.Model, content.Header); Assert.Equal("Header", explorer.ModelPath); Assert.NotEqual(explorer.Root, explorer); Assert.NotNull(explorer.Parent); Assert.Equal(explorer.Index, -1); Assert.False(explorer.IsRoot); }
private ContentExplorer(ContentExplorer parent, IModelField field, int index, object model, ContentMetadataProvider contentMetadata) { Field = field; Model = model; Metadata = contentMetadata; Parent = parent ?? throw new ArgumentNullException(nameof(parent)); FieldPath = field.Name; if (index >= 0) { FieldPath += "[" + index + "]"; } ModelPath = !parent.IsRoot ? string.Concat(parent.ModelPath, Delimiter, FieldPath) : FieldPath; Index = index; rootExplorer = parent.rootExplorer ?? parent; name = rootExplorer.name + ":" + ModelPath; }
public ContentExplorer Navigate(string modelPath) { if (modelPath == null) { throw new ArgumentNullException(nameof(modelPath)); } ContentExplorer navExplorer = this; while (modelPath != string.Empty) { var fieldName = ExtractFirstFieldName(ref modelPath); if (!VisitField(ref navExplorer, fieldName)) { return(null); } } return(navExplorer); }
private static bool VisitField(ref ContentExplorer parentExplorer, string fieldName) { var charIndex = fieldName.IndexOf(IndexStart); var itemIndex = -1; if (charIndex > 0) { itemIndex = int.Parse(fieldName.Substring(charIndex).Trim(IndexTrimChars)); fieldName = fieldName.Substring(0, charIndex); } var parentModel = parentExplorer.Model; if (!parentExplorer.Metadata.TryGetField(fieldName, out FieldProviderAttribute field)) { throw new InvalidOperationException(string.Format("Не найдено поле {0}.", fieldName)); } if (!(field is IModelField modelField)) { throw new InvalidOperationException($"Поле {fieldName} не является полем модели."); } var fieldValue = field.GetModelValue(parentModel); var contentModel = modelField.Navigate(fieldValue, itemIndex); if (contentModel == null) { parentExplorer = null; return(false); } var contentMetadata = parentExplorer.Metadata.Manager.GetMetadata(contentModel.GetType()); parentExplorer = new ContentExplorer(parentExplorer, modelField, itemIndex, contentModel, contentMetadata); return(true); }
private ContentProvider(ContentProvider <TEntry> parent, ContentExplorer contentExplorer) { Entry = parent.Entry; Services = parent.Services; Explorer = contentExplorer; }