private static string ValidateEnumType(this EnumType enumType, IChild scope, string valueReference, bool isRequired) { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var builder = new IndentedStringBuilder(" "); var allowedValues = scope.GetUniqueName("allowedValues"); builder.AppendLine("if ({0}) {{", valueReference) .Indent() .AppendLine("var {0} = {1};", allowedValues, enumType.GetEnumValuesArray()) .AppendLine("if (!{0}.some( function(item) {{ return item === {1}; }})) {{", allowedValues, valueReference) .Indent() .AppendLine("throw new Error({0} + ' is not a valid value. The valid values are: ' + {1});", valueReference, allowedValues) .Outdent() .AppendLine("}"); if (isRequired) { var escapedValueReference = valueReference.EscapeSingleQuotes(); builder.Outdent().AppendLine("} else {") .Indent() .AppendLine("throw new Error('{0} cannot be null or undefined.');", escapedValueReference) .Outdent() .AppendLine("}"); } else { builder.Outdent().AppendLine("}"); } return(builder.ToString()); }
private static string ValidateSequenceType(this SequenceType sequence, IChild scope, string valueReference, bool isRequired, string modelReference = "client.models") { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var builder = new IndentedStringBuilder(" "); var escapedValueReference = valueReference.EscapeSingleQuotes(); var indexVar = scope.GetUniqueName("i"); var innerValidation = sequence.ElementType.ValidateType(scope, valueReference + "[" + indexVar + "]", false, modelReference); if (!string.IsNullOrEmpty(innerValidation)) { if (isRequired) { return(builder.AppendLine("if (!util.isArray({0})) {{", valueReference) .Indent() .AppendLine("throw new Error('{0} cannot be null or undefined and it must be of type {1}.');", escapedValueReference, sequence.Name.ToLower()) .Outdent() .AppendLine("}") .AppendLine("for (var {1} = 0; {1} < {0}.length; {1}++) {{", valueReference, indexVar) .Indent() .AppendLine(innerValidation) .Outdent() .AppendLine("}").ToString()); } return(builder.AppendLine("if (util.isArray({0})) {{", valueReference) .Indent() .AppendLine("for (var {1} = 0; {1} < {0}.length; {1}++) {{", valueReference, indexVar) .Indent() .AppendLine(innerValidation) .Outdent() .AppendLine("}") .Outdent() .AppendLine("}").ToString()); } return(null); }
private static string ValidateDictionaryType(this DictionaryType dictionary, IChild scope, string valueReference, bool isRequired, string modelReference = "client.models") { if (scope == null) { throw new ArgumentNullException(nameof(scope)); } var builder = new IndentedStringBuilder(" "); var escapedValueReference = valueReference.EscapeSingleQuotes(); var valueVar = scope.GetUniqueName("valueElement"); var innerValidation = dictionary.ValueType.ValidateType(scope, valueReference + "[" + valueVar + "]", false, modelReference); if (!string.IsNullOrEmpty(innerValidation)) { if (isRequired) { return(builder.AppendLine("if ({0} === null || {0} === undefined || typeof {0} !== 'object') {{", valueReference) .Indent() .AppendLine("throw new Error('{0} cannot be null or undefined and it must be of type {1}.');", escapedValueReference, dictionary.Name.ToLower()) .Outdent() .AppendLine("}") .AppendLine("for(var {0} in {1}) {{", valueVar, valueReference) .Indent() .AppendLine(innerValidation) .Outdent() .AppendLine("}").ToString()); } return(builder.AppendLine("if ({0} && typeof {0} === 'object') {{", valueReference) .Indent() .AppendLine("for(var {0} in {1}) {{", valueVar, valueReference) .Indent() .AppendLine(innerValidation) .Outdent() .AppendLine("}") .Outdent() .AppendLine("}").ToString()); } return(null); }
/// <summary> /// Generates Ruby code in form of string for deserializing object of given type. /// </summary> /// <param name="type">Type of object needs to be deserialized.</param> /// <param name="scope">Current scope.</param> /// <param name="valueReference">Reference to object which needs to be deserialized.</param> /// <returns>Generated Ruby code in form of string.</returns> public static string AzureDeserializeType( this IModelType type, IChild scope, string valueReference) { var composite = type as CompositeType; var sequence = type as SequenceType; var dictionary = type as DictionaryType; var primary = type as PrimaryType; var enumType = type as EnumTypeRb; var builder = new IndentedStringBuilder(" "); if (primary != null) { if (primary.KnownPrimaryType == KnownPrimaryType.Int || primary.KnownPrimaryType == KnownPrimaryType.Long) { return builder.AppendLine("{0} = Integer({0}) unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.Double) { return builder.AppendLine("{0} = Float({0}) unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.ByteArray) { return builder.AppendLine("{0} = Base64.strict_decode64({0}).unpack('C*') unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.Date) { return builder.AppendLine("{0} = MsRest::Serialization.deserialize_date({0}) unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTime) { return builder.AppendLine("{0} = DateTime.parse({0}) unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTimeRfc1123) { return builder.AppendLine("{0} = DateTime.parse({0}) unless {0}.to_s.empty?", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.UnixTime) { return builder.AppendLine("{0} = DateTime.strptime({0}.to_s, '%s') unless {0}.to_s.empty?", valueReference).ToString(); } } else if (enumType != null && !string.IsNullOrEmpty(enumType.Name)) { return builder .AppendLine("if (!{0}.nil? && !{0}.empty?)", valueReference) .AppendLine( " enum_is_valid = {0}.constants.any? {{ |e| {0}.const_get(e).to_s.downcase == {1}.downcase }}", enumType.ModuleName, valueReference) .AppendLine( " warn 'Enum {0} does not contain ' + {1}.downcase + ', but was received from the server.' unless enum_is_valid", enumType.ModuleName, valueReference) .AppendLine("end") .ToString(); } else if (sequence != null) { var elementVar = scope.GetUniqueName("element"); var innerSerialization = sequence.ElementType.AzureDeserializeType(scope, elementVar); if (!string.IsNullOrEmpty(innerSerialization)) { return builder .AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("deserialized_{0} = []", sequence.Name.ToLower()) .AppendLine("{0}.each do |{1}|", valueReference, elementVar) .Indent() .AppendLine(innerSerialization) .AppendLine("deserialized_{0}.push({1})", sequence.Name.ToLower(), elementVar) .Outdent() .AppendLine("end") .AppendLine("{0} = deserialized_{1}", valueReference, sequence.Name.ToLower()) .Outdent() .AppendLine("end") .ToString(); } } else if (dictionary != null) { var valueVar = scope.GetUniqueName("valueElement"); var innerSerialization = dictionary.ValueType.AzureDeserializeType(scope, valueVar); if (!string.IsNullOrEmpty(innerSerialization)) { return builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0}.each do |key, {1}|", valueReference, valueVar) .Indent() .AppendLine(innerSerialization) .AppendLine("{0}[key] = {1}", valueReference, valueVar) .Outdent() .AppendLine("end") .Outdent() .AppendLine("end").ToString(); } } else if (composite != null) { var compositeName = composite.Name; if(compositeName == "Resource" || compositeName == "SubResource") { compositeName = string.Format(CultureInfo.InvariantCulture, "{0}::{1}", "MsRestAzure", compositeName); } return builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0} = {1}.deserialize_object({0})", valueReference, compositeName) .Outdent() .AppendLine("end").ToString(); } return string.Empty; }
/// <summary> /// Generates Ruby code in form of string for serializing object of given type. /// </summary> /// <param name="type">Type of object needs to be serialized.</param> /// <param name="scope">Current scope.</param> /// <param name="valueReference">Reference to object which needs to serialized.</param> /// <returns>Generated Ruby code in form of string.</returns> public static string AzureSerializeType( this IModelType type, IChild scope, string valueReference) { var composite = type as CompositeType; var sequence = type as SequenceType; var dictionary = type as DictionaryType; var primary = type as PrimaryType; var builder = new IndentedStringBuilder(" "); if (primary != null) { if (primary.KnownPrimaryType == KnownPrimaryType.ByteArray) { return builder.AppendLine("{0} = Base64.strict_encode64({0}.pack('c*'))", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTime) { return builder.AppendLine("{0} = {0}.new_offset(0).strftime('%FT%TZ')", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTimeRfc1123) { return builder.AppendLine("{0} = {0}.new_offset(0).strftime('%a, %d %b %Y %H:%M:%S GMT')", valueReference).ToString(); } if (primary.KnownPrimaryType == KnownPrimaryType.UnixTime) { return builder.AppendLine("{0} = {0}.new_offset(0).strftime('%s')", valueReference).ToString(); } } else if (sequence != null) { var elementVar = scope.GetUniqueName("element"); var innerSerialization = sequence.ElementType.AzureSerializeType(scope, elementVar); if (!string.IsNullOrEmpty(innerSerialization)) { return builder .AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("serialized{0} = []", sequence.Name) .AppendLine("{0}.each do |{1}|", valueReference, elementVar) .Indent() .AppendLine(innerSerialization) .AppendLine("serialized{0}.push({1})", sequence.Name.ToPascalCase(), elementVar) .Outdent() .AppendLine("end") .AppendLine("{0} = serialized{1}", valueReference, sequence.Name.ToPascalCase()) .Outdent() .AppendLine("end") .ToString(); } } else if (dictionary != null) { var valueVar = scope.GetUniqueName("valueElement"); var innerSerialization = dictionary.ValueType.AzureSerializeType(scope, valueVar); if (!string.IsNullOrEmpty(innerSerialization)) { return builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0}.each {{ |key, {1}|", valueReference, valueVar) .Indent() .AppendLine(innerSerialization) .AppendLine("{0}[key] = {1}", valueReference, valueVar) .Outdent() .AppendLine("}") .Outdent() .AppendLine("end").ToString(); } } else if (composite != null) { var compositeName = composite.Name; if(compositeName == "Resource" || compositeName == "SubResource") { compositeName = string.Format(CultureInfo.InvariantCulture, "{0}::{1}", "MsRestAzure", compositeName); } return builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0} = {1}.serialize_object({0})", valueReference, compositeName) .Outdent() .AppendLine("end").ToString(); } return string.Empty; }
/// <summary> /// Generate code to perform required validation on a type /// </summary> /// <param name="type">The type to validate</param> /// <param name="scope">A scope provider for generating variable names as necessary</param> /// <param name="valueReference">A reference to the value being validated</param> /// <param name="constraints">Constraints</param> /// <returns>The code to validate the reference of the given type</returns> public static string ValidateType(this IModelType type, IChild scope, string valueReference, Dictionary <Constraint, string> constraints) { if (scope == null) { throw new ArgumentNullException("scope"); } var model = type as CompositeTypeCs; var sequence = type as SequenceTypeCs; var dictionary = type as DictionaryTypeCs; var sb = new IndentedStringBuilder(); if (model != null && model.ShouldValidateChain()) { sb.AppendLine("{0}.Validate();", valueReference); } if (constraints != null && constraints.Any()) { AppendConstraintValidations(valueReference, constraints, sb, type); } if (sequence != null && sequence.ShouldValidateChain()) { var elementVar = scope.GetUniqueName("element"); var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, null); if (!string.IsNullOrEmpty(innerValidation)) { sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference) .AppendLine("{").Indent() .AppendLine(innerValidation).Outdent() .AppendLine("}"); } } else if (dictionary != null && dictionary.ShouldValidateChain()) { var valueVar = scope.GetUniqueName("valueElement"); var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, null); if (!string.IsNullOrEmpty(innerValidation)) { sb.AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference) .AppendLine("{").Indent() .AppendLine(innerValidation).Outdent() .AppendLine("}").Outdent(); } } if (sb.ToString().Trim().Length > 0) { if (type.IsValueType()) { return(sb.ToString()); } else { return(CheckNull(valueReference, sb.ToString())); } } return(null); }
/// <summary> /// Generate code to perform required validation on a type /// </summary> /// <param name="type">The type to validate</param> /// <param name="scope">A scope provider for generating variable names as necessary</param> /// <param name="valueReference">A reference to the value being validated</param> /// <param name="constraints">Constraints</param> /// <returns>The code to validate the reference of the given type</returns> public static string ValidateType(this IModelType type, IChild scope, string valueReference, Dictionary<Constraint, string> constraints) { if (scope == null) { throw new ArgumentNullException("scope"); } var model = type as CompositeTypeCs; var sequence = type as SequenceTypeCs; var dictionary = type as DictionaryTypeCs; var sb = new IndentedStringBuilder(); if (model != null && model.ShouldValidateChain()) { sb.AppendLine("{0}.Validate();", valueReference); } if (constraints != null && constraints.Any()) { AppendConstraintValidations(valueReference, constraints, sb, (type as PrimaryType)?.KnownFormat ?? KnownFormat.none); } if (sequence != null && sequence.ShouldValidateChain()) { var elementVar = scope.GetUniqueName("element"); var innerValidation = sequence.ElementType.ValidateType(scope, elementVar, null); if (!string.IsNullOrEmpty(innerValidation)) { sb.AppendLine("foreach (var {0} in {1})", elementVar, valueReference) .AppendLine("{").Indent() .AppendLine(innerValidation).Outdent() .AppendLine("}"); } } else if (dictionary != null && dictionary.ShouldValidateChain()) { var valueVar = scope.GetUniqueName("valueElement"); var innerValidation = dictionary.ValueType.ValidateType(scope, valueVar, null); if (!string.IsNullOrEmpty(innerValidation)) { sb.AppendLine("foreach (var {0} in {1}.Values)", valueVar, valueReference) .AppendLine("{").Indent() .AppendLine(innerValidation).Outdent() .AppendLine("}").Outdent(); } } if (sb.ToString().Trim().Length > 0) { if (type.IsValueType()) { return sb.ToString(); } else { return CheckNull(valueReference, sb.ToString()); } } return null; }
/// <summary> /// Generates Ruby code in form of string for deserializing object of given type. /// </summary> /// <param name="type">Type of object needs to be deserialized.</param> /// <param name="scope">Current scope.</param> /// <param name="valueReference">Reference to object which needs to be deserialized.</param> /// <returns>Generated Ruby code in form of string.</returns> public static string AzureDeserializeType( this IModelType type, IChild scope, string valueReference) { var composite = type as CompositeType; var sequence = type as SequenceType; var dictionary = type as DictionaryType; var primary = type as PrimaryType; var enumType = type as EnumTypeRb; var builder = new IndentedStringBuilder(" "); if (primary != null) { if (primary.KnownPrimaryType == KnownPrimaryType.Int || primary.KnownPrimaryType == KnownPrimaryType.Long) { return(builder.AppendLine("{0} = Integer({0}) unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.Double) { return(builder.AppendLine("{0} = Float({0}) unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.ByteArray) { return(builder.AppendLine("{0} = Base64.strict_decode64({0}).unpack('C*') unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.Date) { return(builder.AppendLine("{0} = MsRest::Serialization.deserialize_date({0}) unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTime) { return(builder.AppendLine("{0} = DateTime.parse({0}) unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTimeRfc1123) { return(builder.AppendLine("{0} = DateTime.parse({0}) unless {0}.to_s.empty?", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.UnixTime) { return(builder.AppendLine("{0} = DateTime.strptime({0}.to_s, '%s') unless {0}.to_s.empty?", valueReference).ToString()); } } else if (enumType != null && !string.IsNullOrEmpty(enumType.Name)) { return(builder .AppendLine("if (!{0}.nil? && !{0}.empty?)", valueReference) .AppendLine( " enum_is_valid = {0}.constants.any? {{ |e| {0}.const_get(e).to_s.downcase == {1}.downcase }}", enumType.ModuleName, valueReference) .AppendLine( " warn 'Enum {0} does not contain ' + {1}.downcase + ', but was received from the server.' unless enum_is_valid", enumType.ModuleName, valueReference) .AppendLine("end") .ToString()); } else if (sequence != null) { var elementVar = scope.GetUniqueName("element"); var innerSerialization = sequence.ElementType.AzureDeserializeType(scope, elementVar); if (!string.IsNullOrEmpty(innerSerialization)) { return (builder .AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("deserialized_{0} = []", sequence.Name.ToLower()) .AppendLine("{0}.each do |{1}|", valueReference, elementVar) .Indent() .AppendLine(innerSerialization) .AppendLine("deserialized_{0}.push({1})", sequence.Name.ToLower(), elementVar) .Outdent() .AppendLine("end") .AppendLine("{0} = deserialized_{1}", valueReference, sequence.Name.ToLower()) .Outdent() .AppendLine("end") .ToString()); } } else if (dictionary != null) { var valueVar = scope.GetUniqueName("valueElement"); var innerSerialization = dictionary.ValueType.AzureDeserializeType(scope, valueVar); if (!string.IsNullOrEmpty(innerSerialization)) { return(builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0}.each do |key, {1}|", valueReference, valueVar) .Indent() .AppendLine(innerSerialization) .AppendLine("{0}[key] = {1}", valueReference, valueVar) .Outdent() .AppendLine("end") .Outdent() .AppendLine("end").ToString()); } } else if (composite != null) { var compositeName = composite.Name; if (compositeName == "Resource" || compositeName == "SubResource") { compositeName = string.Format(CultureInfo.InvariantCulture, "{0}::{1}", "MsRestAzure", compositeName); } return(builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0} = {1}.deserialize_object({0})", valueReference, compositeName) .Outdent() .AppendLine("end").ToString()); } return(string.Empty); }
/// <summary> /// Generates Ruby code in form of string for serializing object of given type. /// </summary> /// <param name="type">Type of object needs to be serialized.</param> /// <param name="scope">Current scope.</param> /// <param name="valueReference">Reference to object which needs to serialized.</param> /// <returns>Generated Ruby code in form of string.</returns> public static string AzureSerializeType( this IModelType type, IChild scope, string valueReference) { var composite = type as CompositeType; var sequence = type as SequenceType; var dictionary = type as DictionaryType; var primary = type as PrimaryType; var builder = new IndentedStringBuilder(" "); if (primary != null) { if (primary.KnownPrimaryType == KnownPrimaryType.ByteArray) { return(builder.AppendLine("{0} = Base64.strict_encode64({0}.pack('c*'))", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTime) { return(builder.AppendLine("{0} = {0}.new_offset(0).strftime('%FT%TZ')", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.DateTimeRfc1123) { return(builder.AppendLine("{0} = {0}.new_offset(0).strftime('%a, %d %b %Y %H:%M:%S GMT')", valueReference).ToString()); } if (primary.KnownPrimaryType == KnownPrimaryType.UnixTime) { return(builder.AppendLine("{0} = {0}.new_offset(0).strftime('%s')", valueReference).ToString()); } } else if (sequence != null) { var elementVar = scope.GetUniqueName("element"); var innerSerialization = sequence.ElementType.AzureSerializeType(scope, elementVar); if (!string.IsNullOrEmpty(innerSerialization)) { return (builder .AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("serialized{0} = []", sequence.Name) .AppendLine("{0}.each do |{1}|", valueReference, elementVar) .Indent() .AppendLine(innerSerialization) .AppendLine("serialized{0}.push({1})", sequence.Name.ToPascalCase(), elementVar) .Outdent() .AppendLine("end") .AppendLine("{0} = serialized{1}", valueReference, sequence.Name.ToPascalCase()) .Outdent() .AppendLine("end") .ToString()); } } else if (dictionary != null) { var valueVar = scope.GetUniqueName("valueElement"); var innerSerialization = dictionary.ValueType.AzureSerializeType(scope, valueVar); if (!string.IsNullOrEmpty(innerSerialization)) { return(builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0}.each {{ |key, {1}|", valueReference, valueVar) .Indent() .AppendLine(innerSerialization) .AppendLine("{0}[key] = {1}", valueReference, valueVar) .Outdent() .AppendLine("}") .Outdent() .AppendLine("end").ToString()); } } else if (composite != null) { var compositeName = composite.Name; if (compositeName == "Resource" || compositeName == "SubResource") { compositeName = string.Format(CultureInfo.InvariantCulture, "{0}::{1}", "MsRestAzure", compositeName); } return(builder.AppendLine("unless {0}.nil?", valueReference) .Indent() .AppendLine("{0} = {1}.serialize_object({0})", valueReference, compositeName) .Outdent() .AppendLine("end").ToString()); } return(string.Empty); }