Fixed some minor issues. Added nullability for non-required properties/parameters. This is C# 8 style nullable references.

This commit is contained in:
Michael Yanni 2019-10-31 19:09:43 -07:00
Родитель 9b41288586
Коммит 513f39e261
5 изменённых файлов: 26 добавлений и 12 удалений

Просмотреть файл

@ -47,7 +47,7 @@ namespace AutoRest.CSharp.V3.CodeGen
LazyProperty("public", propertySchemaCs!.Type, propertySchemaCs.ConcreteType ?? propertySchemaCs.Type, propertyCs?.Name);
continue;
}
AutoProperty("public", propertySchemaCs?.Type, propertyCs?.Name);
AutoProperty("public", propertySchemaCs?.Type, propertyCs?.Name, propertyCs?.IsNullable);
}
if (propertyInfos.Any(pi => pi.Property.Required ?? false))
@ -62,7 +62,7 @@ namespace AutoRest.CSharp.V3.CodeGen
{
if (propertySchemaCs?.IsLazy ?? false)
{
Line($"{propertyCs?.Name} = new {propertySchemaCs!.ConcreteType ?? propertySchemaCs.Type}({variableName});");
Line($"{propertyCs?.Name} = new {Type(propertySchemaCs!.ConcreteType ?? propertySchemaCs.Type)}({variableName});");
continue;
}
Line($"{propertyCs?.Name} = {variableName};");

Просмотреть файл

@ -83,16 +83,15 @@ namespace AutoRest.CSharp.V3.CodeGen
public void MethodExpression(string? modifiers, string? returnType, string? name, string[]? parameters, string expression) =>
Line($"{MethodDeclaration(modifiers, returnType, name, parameters ?? new string[0])} => {expression};");
public void AutoProperty(string modifiers, CSharpType? type, string? name) =>
Line($"{modifiers} {Pair(type, name)} {{ get; set; }}");
public void EnumValue(string? value, bool includeComma = true) =>
Line($"{value ?? "[NO VALUE]"}{(includeComma ? "," : String.Empty)}");
public void AutoProperty(string modifiers, CSharpType? type, string? name, bool? isNullable) =>
Line($"{modifiers} {Pair(type, name, isNullable)} {{ get; set; }}");
public void LazyProperty(string modifiers, CSharpType? type, CSharpType? concreteType, string? name)
{
var variable = $"_{name.ToVariableName()}";
//Line($"private {Pair(concreteType, variable)};");
_classFields.Add($"private {Pair(concreteType, variable)};");
Line($"{modifiers} {Pair(type, name)} => {Type(typeof(LazyInitializer))}.EnsureInitialized(ref {variable});");
}
@ -136,7 +135,7 @@ namespace AutoRest.CSharp.V3.CodeGen
});
}
public string Type(CSharpType? type)
public string Type(CSharpType? type, bool? isNullable = false)
{
if (_useTypeShortNames)
{
@ -144,14 +143,16 @@ namespace AutoRest.CSharp.V3.CodeGen
_usingNamespaces.Add(type?.SubType1?.KeywordName != null ? null : type?.SubType1?.Namespace);
_usingNamespaces.Add(type?.SubType2?.KeywordName != null ? null : type?.SubType2?.Namespace);
}
return (_useTypeShortNames ? type?.GetComposedName(typesAsKeywords: _useKeywords) : type?.FullName) ?? "[NO TYPE]";
var typeText = _useTypeShortNames ? type?.GetComposedName(typesAsKeywords: _useKeywords) : type?.FullName;
var nullMark = (isNullable ?? false) ? "?" : String.Empty;
return (typeText != null ? typeText + nullMark : null) ?? "[NO TYPE]";
}
public string Type(Type? type) => Type(new CSharpType {FrameworkType = type});
public string Type(Type? type, bool? isNullable = false) => Type(new CSharpType {FrameworkType = type}, isNullable);
public string AttributeType(Type? type) => Type(type).Replace("Attribute", String.Empty);
public static string Pair(string? typeText, string? name) => $"{typeText ?? "[NO TYPE]"} {name ?? "[NO NAME]"}";
public string Pair(CSharpType? type, string? name) => $"{Type(type)} {name ?? "[NO NAME]"}";
public string Pair(Type? type, string? name) => $"{Type(type)} {name ?? "[NO NAME]"}";
public string Pair(CSharpType? type, string? name, bool? isNullable = false) => $"{Type(type, isNullable)} {name ?? "[NO NAME]"}";
public string Pair(Type? type, string? name, bool? isNullable = false) => $"{Type(type, isNullable)} {name ?? "[NO NAME]"}";
}
}

Просмотреть файл

@ -33,6 +33,9 @@ namespace AutoRest.CSharp.V3.Pipeline.Generated
[YamlMember(Alias = "inputType", Order = 6)]
public CSharpType? InputType { get; set; }
[YamlMember(Alias = "isNullable", Order = 7)]
public bool? IsNullable { get; set; }
[YamlIgnore]
public int SchemaOrder { get; set; }
}

Просмотреть файл

@ -26,6 +26,16 @@ namespace AutoRest.CSharp.V3.Plugins
var cs = property.Language.CSharp ??= new CSharpLanguage();
cs.Name = property.Language.Default.Name.ToCleanName();
cs.Description = property.Language.Default.Description;
cs.IsNullable = !(property.Required ?? false);
}
var parameterNodes = codeModel.OperationGroups.SelectMany(og => og.Operations).SelectMany(o => o.Request.Parameters);
foreach (var parameter in parameterNodes)
{
var cs = parameter.Language.CSharp ??= new CSharpLanguage();
cs.Name = parameter.Language.Default.Name.ToVariableName();
cs.Description = parameter.Language.Default.Description;
cs.IsNullable = !(parameter.Required ?? false);
}
var choiceValueNodes = (codeModel.Schemas.Choices?.SelectMany(c => c.Choices) ?? Enumerable.Empty<ChoiceValue>())

Просмотреть файл

@ -24,7 +24,7 @@ namespace AutoRest.CSharp.V3.Utilities
public static string? RemoveMiddleDotCharacters(this string? text) => text?.Replace("·", String.Empty);
[return: NotNullIfNotNull("name")]
public static string? ToCleanName(this string? name) => name?.ToPascalCase().RemoveMiddleDotCharacters();
public static string? ToVariableName(this string? name) => name?.ToCamelCase();
public static string? ToVariableName(this string? name) => name?.ToCamelCase().RemoveMiddleDotCharacters();
//https://stackoverflow.com/a/41176852/294804
public static IEnumerable<string> ToLines(this string value, bool removeEmptyLines = false)