internal static bool IsValidForNestedStructure(this DictionaryModel model) { DictionaryModel parentModel = model.GetParentModel(); if (parentModel == null) { return(true); } string[] itemKeyParts = model.GetItemKey().Split('.'); string[] parentItemKeyParts = parentModel.GetItemKey().Split('.') ?? new string[0]; bool equal = itemKeyParts.Length - parentItemKeyParts.Length == 1 && !string.IsNullOrWhiteSpace(itemKeyParts.Last()); if (!equal) { return(false); } if (!parentModel.IsValidForNestedStructure()) { return(false); } for (int i = 0; i < parentItemKeyParts.Length; i++) { equal = parentItemKeyParts[i] == itemKeyParts[i]; if (!equal) { break; } } return(equal); }
/// <summary> /// Writes a collection of models /// </summary> /// <param name="models">The models to build</param> private void WriteDictionaries(params DictionaryModel[] models) { if (models == null) { return; } // Iterate through all models for (int i = 0; i < models.Length; i++) { DictionaryModel model = models[i]; // Write a start region directive WriteLine($"#region {model.GetItemKey()}"); // Create a list of action starting the the action to build the model itself List <Action <DictionaryModel> > parentModelActions = new List <Action <DictionaryModel> > { m => WriteContentModel(model) }; if (model.RenderParentModel(_config.UseNestedStructure)) { // Get the parent model DictionaryModel parentModel = model.GetParentModel(); // Loop through all parent models while (parentModel != null) { // Prevent access to modified closure, so creating a local variable // https://www.jetbrains.com/help/resharper/2020.1/AccessToModifiedClosure.html DictionaryModel localParentModel = parentModel; // The last added action is the child action to this action Action <DictionaryModel> childAction = parentModelActions.Last(); // Generate the class name string itemKey = localParentModel.GenerateCodeItemKey(_config.UseNestedStructure); string className = itemKey.ToCodeString(true).ToPascalCase(); // Add the action to the list parentModelActions.Add(m => WritePartialClass($"_{className}", childAction, localParentModel)); // Get the next parent model parentModel = parentModel.GetParentModel(); } } // Get the root action. Action <DictionaryModel> rootAction = parentModelActions.Last(); // Execute the root action, to start building the nested class structure rootAction(model); // Write a end region directive WriteLine("#endregion"); // If not the last model, add an empty line if (i < models.Length - 1) { _builder.AppendLine(); } } }
internal static bool IsRootModel(this DictionaryModel model, bool useNestedStructure) { return(model.GetParentModel() == null || !useNestedStructure || !model.IsValidForNestedStructure()); }