diff --git a/src/generator/AutoRest.Ruby.Azure/CodeGeneratorRba.cs b/src/generator/AutoRest.Ruby.Azure/CodeGeneratorRba.cs index 9b1505e0fa..ef56a9aa85 100644 --- a/src/generator/AutoRest.Ruby.Azure/CodeGeneratorRba.cs +++ b/src/generator/AutoRest.Ruby.Azure/CodeGeneratorRba.cs @@ -61,20 +61,22 @@ public override async Task Generate(CodeModel cm) foreach (CompositeTypeRba model in codeModel.ModelTypes) { if ((model.Extensions.ContainsKey(AzureExtensions.ExternalExtension) && - (bool)model.Extensions[AzureExtensions.ExternalExtension]) - || model.Name == "Resource" || model.Name == "SubResource") + (bool)model.Extensions[AzureExtensions.ExternalExtension])) { continue; } - if( codeModel.pageModels.Any( each => each.Name.EqualsIgnoreCase(model.Name ) ) ) + if (codeModel.pageModels.Any(each => each.Name.EqualsIgnoreCase(model.Name))) { // Skip, handled in the .pageModels section below. continue; } - var modelTemplate = new ModelTemplate { Model = model }; - await Write(modelTemplate, Path.Combine(GeneratorSettingsRb.Instance.modelsPath, CodeNamer.UnderscoreCase(model.Name) + ImplementationFileExtension)); + var modelTemplate = new AzureModelTemplate { Model = model }; + if (!CompositeTypeRba.resourceOrSubResourceRegEx.IsMatch(model.Name) || !CompositeTypeRba.IsResourceModelMatchingStandardDefinition(model)) + { + await Write(modelTemplate, Path.Combine(GeneratorSettingsRb.Instance.modelsPath, CodeNamer.UnderscoreCase(model.Name) + ImplementationFileExtension)); + } } // Paged Models foreach (var pageModel in codeModel.pageModels) diff --git a/src/generator/AutoRest.Ruby.Azure/Model/CompositeTypeRba.cs b/src/generator/AutoRest.Ruby.Azure/Model/CompositeTypeRba.cs index 6b49fd0b5e..24d26ddbe2 100644 --- a/src/generator/AutoRest.Ruby.Azure/Model/CompositeTypeRba.cs +++ b/src/generator/AutoRest.Ruby.Azure/Model/CompositeTypeRba.cs @@ -1,9 +1,13 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See License.txt in the project root for license information. +using System; +using System.Linq; using System.Collections.Generic; using AutoRest.Extensions.Azure; using AutoRest.Ruby.Model; +using AutoRest.Core.Model; +using System.Text.RegularExpressions; namespace AutoRest.Ruby.Azure.Model { @@ -12,6 +16,10 @@ namespace AutoRest.Ruby.Azure.Model /// public class CompositeTypeRba : CompositeTypeRb { + public static readonly Regex resourceOrSubResourceRegEx = new Regex(@"^(RESOURCE|SUBRESOURCE)$", RegexOptions.IgnoreCase); + private static readonly Regex subResourceRegEx = new Regex(@"^(ID)$", RegexOptions.IgnoreCase); + private static readonly Regex resourceRegEx = new Regex(@"^(ID|NAME|TYPE|LOCATION|TAGS)$", RegexOptions.IgnoreCase); + /// /// Initializes a new instance of the AzureModelTemplateModel class. /// @@ -42,15 +50,65 @@ public override string GetBaseTypeName() if (this.BaseModelType.Extensions.ContainsKey(AzureExtensions.ExternalExtension) || this.BaseModelType.Extensions.ContainsKey(AzureExtensions.AzureResourceExtension)) { - typeName = "MsRestAzure::" + typeName; + if (!resourceOrSubResourceRegEx.IsMatch(typeName) || !IsResourceModelMatchingStandardDefinition(this)) + { + typeName = "MsRestAzure::" + typeName; + } } return " < " + typeName; } + else if (resourceOrSubResourceRegEx.IsMatch(this.Name)) + { + return " < " + "MsRestAzure::" + this.Name; + } return string.Empty; } + /// + /// Checks if the provided definition of models 'Resource'/'SubResource' matches the standard definition, + /// as defined in MsRestAzure. For other models, it returns false. + /// + /// to be validated + /// + public static bool IsResourceModelMatchingStandardDefinition(CompositeType model) + { + string modelName = model.Name.ToString(); + if (modelName.Equals("SubResource", StringComparison.InvariantCultureIgnoreCase) && + model.Properties.All(property => subResourceRegEx.IsMatch(property.Name.ToString()))) + { + return true; + } + + if(modelName.Equals("Resource", StringComparison.InvariantCultureIgnoreCase) && + model.Properties.All(property => resourceRegEx.IsMatch(property.Name.ToString()))) + { + return true; + } + + return false; + } + + /// + /// Determines if the accessor needs to be generated. For Resource/SubResource models, accessors are generated only + /// for properties that are not in the standard definition, as defined in MsRestAzure. + /// + /// + /// + /// + public static bool NeedsAccessor(CompositeType model, string propertyName) + { + string modelName = model.Name.ToString(); + if((modelName.Equals("SubResource", StringComparison.InvariantCultureIgnoreCase) && subResourceRegEx.IsMatch(propertyName)) || + (modelName.Equals("Resource", StringComparison.InvariantCultureIgnoreCase) && resourceRegEx.IsMatch(propertyName))) + { + return false; + } + + return true; + } + /// /// Gets the list of modules/classes which need to be included. /// diff --git a/src/generator/AutoRest.Ruby.Azure/Model/RequirementsRba.cs b/src/generator/AutoRest.Ruby.Azure/Model/RequirementsRba.cs index f01be6ef22..159bed280e 100644 --- a/src/generator/AutoRest.Ruby.Azure/Model/RequirementsRba.cs +++ b/src/generator/AutoRest.Ruby.Azure/Model/RequirementsRba.cs @@ -22,7 +22,7 @@ protected override bool ExcludeModel(CompositeType model) { return (model.Extensions.ContainsKey(AzureExtensions.ExternalExtension) && (bool) model.Extensions[AzureExtensions.ExternalExtension]) - || model.Name == "Resource" || model.Name == "SubResource"; + ||CompositeTypeRba.IsResourceModelMatchingStandardDefinition(model); } /// diff --git a/src/generator/AutoRest.Ruby.Azure/Templates/AzureModelTemplate.cshtml b/src/generator/AutoRest.Ruby.Azure/Templates/AzureModelTemplate.cshtml new file mode 100644 index 0000000000..263626e01a --- /dev/null +++ b/src/generator/AutoRest.Ruby.Azure/Templates/AzureModelTemplate.cshtml @@ -0,0 +1,68 @@ +@using System +@using System.Linq +@using AutoRest.Core.Utilities +@using AutoRest.Ruby +@using AutoRest.Ruby.Model +@using AutoRest.Ruby.Azure.Model +@inherits AutoRest.Core.Template +# encoding: utf-8 +@Header("# ") +@EmptyLine +module @(Settings.Namespace) + module Models + # + @WrapComment("# ", Model.BuildSummaryAndDescriptionString()) + # + class @Model.Name@(Model.GetBaseTypeName()) + @if (Model.Includes.Any()) + { + @EmptyLine + foreach (var include in Model.Includes) + { + @:include @include + } + @EmptyLine + } + + @if (Model.BaseIsPolymorphic && Model.BaseModelType == null) + { + @:@@@@discriminatorMap = Hash.new + foreach (var derivedType in Model.DerivedTypes) + { + @:@@@@discriminatorMap["@derivedType.SerializedName"] = "@derivedType.Name" + } + } + + @if (Model.BaseIsPolymorphic) + { + @EmptyLine + @:def initialize + @: @@@Model.PolymorphicDiscriminatorProperty.Name = "@Model.SerializedName" + @:end + @EmptyLine + @:attr_accessor :@Model.PolymorphicDiscriminatorProperty.Name + @EmptyLine + } + + @foreach (var property in Model.PropertyTemplateModels.Where(each => !each.IsPolymorphicDiscriminator)) + { + @if (CompositeTypeRba.NeedsAccessor(Model, property.Name)) + { + @:@WrapComment("# ", string.Format("@return {0}{1}", property.ModelType.GetYardDocumentation(), CompositeTypeRb.GetPropertyDocumentationString(property))) + @:attr_accessor :@property.Name + @EmptyLine + @: + } + } + + @EmptyLine + # + @WrapComment("# ", string.Format("Mapper for {0} class as Ruby Hash.", Model.Name)) + # This will be used for serialization/deserialization. + # + def self.mapper() + @(Model.ConstructModelMapper()) + end + end + end +end