powershell/resources/templates/model.ejs (180 lines of code) (raw):
<%- project.commentHeader %>
namespace <%- project.namespace %>.Models
{
using System.Linq;
<%# ToDo: need to populate effectiveDocumentation in the model-%>
<% if (model.language.default.description) {-%>
/// <summary>
/// <%=project.helper.wrapComments(' ', '/// ', model.language.default.description)%>
/// </summary>
<% }-%>
<% if (model.summary && model.language.default.description) { -%>
/// <remarks>
/// <%=project.helper.wrapComments(' ', '/// ', model.language.default.description)%>
/// </remarks>
<% } -%>
<%# ToDo: support NeedsPolymorphicConverter-%>
<% if (model.discriminatorValue || model.discriminator) {-%>
[Newtonsoft.Json.JsonObject("<%-model.discriminatorValue || project.helper.PascalCase(model.language.default.name)%>")]
<% } -%>
<% if(project.helper.NeedsTransformationConverter(model)) {-%>
[Microsoft.Rest.Serialization.JsonTransformation]
<% }-%>
<%# ToDo: support ObsoleteAttribute-%>
public partial class <%-model.language.default.name%><%- (model.extensions && model.extensions["x-ms-azure-resource"]) ? " : Microsoft.Rest.Azure.IResource": (model.parents && model.parents.immediate.length == 1) ? " : " + model.parents.immediate[0].language.default.name: ""%>
{
/// <summary>
/// Initializes a new instance of the <%= model.language.default.name%> class.
/// </summary>
public <%- model.language.default.name%>()
{
<%#support for object model that contains ContainsConstantProperties-%>
<% if (model.properties || model.parents) {-%>
<%project.helper.GetAllPublicVirtualProperties(model.language.default.virtualProperties).filter(p => p.required && p.property.schema.type === 'object' && project.helper.HasConstantProperty(p.property.schema)).forEach(function(p) {-%>
this.<%-p.name%> = new <%-p.property.schema.language.csharp.fullname%>();
<%});-%>
<%}-%>
CustomInit();
}
<%#ToDo support for constructorParameters in the model, the constructorParameter is mapped to properties for the time being%>
<% if (((model.properties || []).filter(p => !p.isDiscriminator).length > 0 || model.parents) && model.language.default.constructorParametersDeclaration != '') {-%>
/// <summary>
/// Initializes a new instance of the <%=model.language.default.name%> class.
/// </summary>
<% var publicVirtualProperties = (model.extensions && model.extensions["x-ms-azure-resource"]) ? project.helper.GetAllPublicVirtualPropertiesWithoutInherited(model.language.default.virtualProperties) : project.helper.GetAllPublicVirtualProperties(model.language.default.virtualProperties);
publicVirtualProperties.filter(p => !project.helper.IsConstantEnumProperty(p)).forEach(function(p) {-%>
<%
var additionInfo = '';
if (p.property.schema.type == 'sealed-choice' || p.property.schema.type == 'choice')
{
additionInfo = ' Possible values include: ';
choices = p.property.schema.choices.map(c => `'${c.value}'`);
additionInfo = additionInfo + choices.join(', ');
}
%>
/// <param name="<%-project.helper.CamelCase(p.name)%>"><%=project.helper.wrapComments(' ', '/// ', p.property.language.default.description)%>
/// <%=project.helper.wrapComments(' ', '/// ', additionInfo)%></param>
<%});-%>
public <%- model.language.default.name%>(<%-model.language.default.constructorParametersDeclaration%>)
<%# If there is only one direct parent, will implement it as parent%>
<% if (model.language.default.baseConstructorCall) {
publicVirtualProperties = project.helper.GetAllPublicVirtualPropertiesWithoutInherited(model.language.default.virtualProperties);
-%>
: <%-model.language.default.baseConstructorCall%>
<%}-%>
{
<%#ToDo support for InstanceProperties in the model, non const properties, priority 0-%>
<%publicVirtualProperties.filter(p => !project.helper.IsConstantEnumProperty(p)).forEach(function(p) {-%>
this.<%-p.name%> = <%-project.helper.CamelCase(p.name)%>;
<%});-%>
CustomInit();
}
<% };-%>
<%#Below is support for ClassProperties in the model, const properties-%>
<% const constantProperties = (publicVirtualProperties || []).filter(p => p.property.schema.type === 'constant' || project.helper.IsConstantEnumProperty(p));
if (constantProperties.length > 0) {-%>
/// <summary>
/// Static constructor for <%-model.language.default.name%> class.
/// </summary>
static <%-model.language.default.name%>()
{
<% constantProperties.forEach(function(p) {
const quote = p.property.schema.choiceType.type !== 'string' ? '' : '"';
-%>
<%-p.name%> = <%-quote + p.property.schema.choices[0].value + quote%>;
<%});-%>
}
<% } -%>
/// <summary>
/// An initialization method that performs custom operations like setting defaults
/// </summary>
partial void CustomInit();
<%#ToDo support for InstanceProperties in the model, non const properties, priority 0, line 407-%>
<% (publicVirtualProperties || []).filter(p => p.property.schema.type != 'constant' && !project.helper.IsConstantEnumProperty(p)).forEach(function(p){-%>
<%
var additionInfo = '';
if (p.property.schema.type == 'sealed-choice' || p.property.schema.type == 'choice')
{
additionInfo = ' Possible values include: ';
choices = p.property.schema.choices.map(c => `'${c.value}'`);
additionInfo = additionInfo + choices.join(', ');
}
%>
/// <summary>
/// <%=project.helper.wrapComments(' ', '/// ', p.property.language.csharp.formattedPropertySummary)%><%=additionInfo%>
/// </summary>
<% (p.property.schema.language.csharp.jsonConverters || []).forEach(function(ct) {-%>
[Newtonsoft.Json.JsonConverter(typeof(<%-ct%>))]
<% });-%>
<% if (model.extensions && model.extensions['x-ms-parameter-grouping']) {-%>
[Newtonsoft.Json.JsonIgnore]
<%} else if (p.property.language.default.flavor) {-%>
<%
switch(p.property.language.default.flavor) {
case 'additionalProperties':-%>
[Newtonsoft.Json.JsonExtensionData]
<% break;
default:
break;
}-%>
<% } else {-%>
[Newtonsoft.Json.JsonProperty(PropertyName = "<%-p.serializedName%>")]
<% } -%>
<% var type = p.property.schema.language.csharp.fullname-%>
<%type = (!project.helper.IsValueType(p.property.schema.type) && !project.helper.IsEnum(p.property.schema) && !(p.property.schema.choiceType && project.helper.IsValueType(p.property.schema.choiceType.type))) || p.required || p.property.nullable == false ? type : type + '?'-%>
public <%-type %> <%-p.name%> {get; <%- p.readOnly ? "private " : ""-%>set; }
<% }); -%>
<%#Below is support for ClassProperties in the model, const properties -%>
<% (publicVirtualProperties || []).filter(p => p.property.schema.type === 'constant' || project.helper.IsConstantEnumProperty(p)).forEach(function(p){-%>
/// <summary>
/// <%=p.property.language.csharp.formattedPropertySummary%>
/// </summary>
<% (p.property.schema.language.csharp.jsonConverters || []).forEach(function(ct) {-%>
[Newtonsoft.Json.JsonConverter(typeof(<%-ct%>))]
<% });-%>
<% if (model.extensions && model.extensions['x-ms-parameter-grouping']) {-%>
[Newtonsoft.Json.JsonIgnore]
<% } else {-%>
[Newtonsoft.Json.JsonProperty(PropertyName = "<%-p.serializedName%>")]
<% } -%>
<% var type = p.property.schema.type === 'constant' ? p.property.schema.language.csharp.fullname : project.helper.GetCsharpType(p.property.schema.choiceType)-%>
public static <%-type %> <%-p.name%> {get; private set; }
<%});-%>
<%#ToDo support for ShouldValidateChain-%>
<% if (project.helper.ShouldValidateChain(model)) {
var methodQualifier = model.parents && model.parents.immediate.length == 1 && project.helper.ShouldValidateChain(model.parents.immediate[0]) ? "override" : "virtual";
var anythingToValidate = false;-%>
/// <summary>
/// Validate the object.
/// </summary>
/// <exception cref="Microsoft.Rest.ValidationException">
/// Thrown if validation fails
/// </exception>
public <%-methodQualifier%> void Validate()
{
<% if (model.parents && model.parents.immediate.length == 1 && project.helper.ShouldValidateChain(model.parents.immediate[0])) {
anythingToValidate = true;
-%>
base.Validate();
<% } -%>
<%publicVirtualProperties.filter(p=> p.required && !p.readOnly && !project.helper.IsValueType(p.property.schema.type) && !project.helper.IsConstantEnumProperty(p) && !project.helper.IsEnum(p.property.schema)).forEach(function(p) {
anythingToValidate = true;
-%>
if (this.<%-p.name%> == null)
{
throw new Microsoft.Rest.ValidationException(Microsoft.Rest.ValidationRules.CannotBeNull, "<%-p.name%>");
}
<%});-%>
<%publicVirtualProperties.filter(p=> project.helper.HasConstrains(p.property.schema) || !project.helper.IsValueType(p.property.schema.type)).forEach(function(p) {
anythingToValidate = true;
-%>
<%-project.helper.ValidateType(p.property.schema, model, `this.${p.name}`, !project.helper.IsValueType(p.property.schema.type) || !p.required)%>
<% }); -%>
<% if(!anythingToValidate) { -%>
//Nothing to validate
<% } -%>
}
<% } -%>
<%#ToDo support for ShouldGenerateXmlSerialization, guess we do not need to support it-%>
}
}