Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code changes to generate resources/subresources #1769

Merged
merged 7 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/generator/AutoRest.Ruby.Azure/CodeGeneratorRba.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into AzureModelTemplate the only difference I saw with Model template is an if statement asking if an accessor should be generated and avoid generating it if the model is Resource or Subsresource, is this correct? If so, could we be making the distinction here and producing the template with the accessor (AzureModelTemplate) if the model matches a resource or subsresource, and produce a Model template in the rest of the cases?
or is there another way where we could avoid repeating the code between ModelTemplate and AzureModelTemplate and add a condition to ModelTemplate that can produce the difference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, since we have to create AzureModelTemplate anyway, it is good to keep the Azure specific model generation in one place than two. I am not convinced that we need to continue to use 2 file. No changes here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the idea that @veronicagg has but looks like you guys already discussed and reached to mutual conclusion

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)
Expand Down
60 changes: 59 additions & 1 deletion src/generator/AutoRest.Ruby.Azure/Model/CompositeTypeRba.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -12,6 +16,10 @@ namespace AutoRest.Ruby.Azure.Model
/// </summary>
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);

/// <summary>
/// Initializes a new instance of the AzureModelTemplateModel class.
/// </summary>
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Checks if the provided definition of models 'Resource'/'SubResource' matches the standard definition,
/// as defined in MsRestAzure. For other models, it returns false.
/// </summary>
/// <param name="model">to be validated</param>
/// <returns></returns>
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;
}

/// <summary>
/// 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.
/// </summary>
/// <param name="model"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
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;
}

/// <summary>
/// Gets the list of modules/classes which need to be included.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/generator/AutoRest.Ruby.Azure/Model/RequirementsRba.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
@using System
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entire file seems like exact copy of ModelTemplate from Ruby generator except line number 49. I'd definitely recommend not repeating as long as possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into other options. For now, since we are using cshtml files, we do not have anyother option. No changes here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay then

@using System.Linq
@using AutoRest.Core.Utilities
@using AutoRest.Ruby
@using AutoRest.Ruby.Model
@using AutoRest.Ruby.Azure.Model
@inherits AutoRest.Core.Template<AutoRest.Ruby.Model.CompositeTypeRb>
# 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