Skip to content

Commit

Permalink
Code changes to generate resources/subresources (#1769)
Browse files Browse the repository at this point in the history
Code changes to generate resources/subresources in ruby based on the standard definitions defined in MSRestAzure
  • Loading branch information
sarangan12 authored Feb 2, 2017
1 parent 5788a5f commit 1fae9ec
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 7 deletions.
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 };
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
@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

0 comments on commit 1fae9ec

Please sign in to comment.