Skip to content
Draft
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
6 changes: 6 additions & 0 deletions Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,15 @@ public static SwitchStatementSyntax WithDefault(this SwitchStatementSyntax @swit
public static SimpleNameSyntax MaybeWithPragmaDisableObsoleteWarning(this SimpleNameSyntax syntax, bool obsolete) =>
obsolete ? syntax.WithPragmaWarning(PragmaWarnings.Obsolete) : syntax;

public static PropertyDeclarationSyntax MaybeWithPragmaDisableObsoleteWarning(this PropertyDeclarationSyntax syntax, bool obsolete) =>
obsolete ? syntax.WithPragmaWarning(PragmaWarnings.Obsolete) : syntax;

public static SimpleNameSyntax WithPragmaWarning(this SimpleNameSyntax syntax, string errorCode) =>
syntax.WithIdentifier(syntax.Identifier.WithPragmaWarning(errorCode));

public static PropertyDeclarationSyntax WithPragmaWarning(this PropertyDeclarationSyntax syntax, string errorCode) =>
syntax.WithIdentifier(syntax.Identifier.WithPragmaWarning(errorCode));

public static SyntaxToken WithPragmaWarning(this SyntaxToken token, string errorCode) =>
token.WithAdditionalAnnotations(new SyntaxAnnotation(PragmaWarnings.AnnotationKind, errorCode));

Expand Down
2 changes: 2 additions & 0 deletions Google.Api.Generator/Generation/MethodDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public Paginated(ServiceDetails svc, MethodDescriptor desc,
SyncGrpcType = Typ.Generic(typeof(GrpcPagedEnumerable<,,>), RequestTyp, ResponseTyp, ResourceTyp);
AsyncGrpcType = Typ.Generic(typeof(GrpcPagedAsyncEnumerable<,,>), RequestTyp, ResponseTyp, ResourceTyp);
ResourcesFieldName = responseResourceField.CSharpPropertyName();
ResponseResourceFieldIsDeprecated = responseResourceField.IsDeprecated();
PageSizeFieldNumber = pageSizeFieldNumber;
PageTokenFieldNumber = pageTokenFieldNumber;
}
Expand All @@ -96,6 +97,7 @@ public Paginated(ServiceDetails svc, MethodDescriptor desc,
public Typ SyncGrpcType { get; }
public Typ AsyncGrpcType { get; }
public string ResourcesFieldName { get; }
public bool ResponseResourceFieldIsDeprecated { get; }
public int PageSizeFieldNumber { get; }
public int PageTokenFieldNumber { get; }
}
Expand Down
2 changes: 1 addition & 1 deletion Google.Api.Generator/Generation/ServiceCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private static IEnumerable<MemberDeclarationSyntax> PaginatedPartialClasses(Sour
{
var propertyName = method.ResourcesFieldName;
var genericGetEnumerator = Method(Public, ctx.Type(Typ.Generic(typeof(IEnumerator<>), method.ResourceTyp)), "GetEnumerator")()
.WithBody(Property(Public, ctx.TypeDontCare, propertyName).Call(nameof(IEnumerable<int>.GetEnumerator))())
.WithBody(Property(Public, ctx.TypeDontCare, propertyName).MaybeWithPragmaDisableObsoleteWarning(method.ResponseResourceFieldIsDeprecated).Call(nameof(IEnumerable<int>.GetEnumerator))())
Comment on lines 72 to +74

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The statement for genericGetEnumerator is extremely long (exceeding 180 characters) and hard to read. Breaking it down by introducing a local variable for the property expression significantly improves readability and maintainability.

                        var propertyName = method.ResourcesFieldName;
                        var property = Property(Public, ctx.TypeDontCare, propertyName)
                            .MaybeWithPragmaDisableObsoleteWarning(method.ResponseResourceFieldIsDeprecated);
                        var genericGetEnumerator = Method(Public, ctx.Type(Typ.Generic(typeof(IEnumerator<>), method.ResourceTyp)), "GetEnumerator")()
                            .WithBody(property.Call(nameof(IEnumerable<int>.GetEnumerator))())

.WithXmlDoc(XmlDoc.Summary("Returns an enumerator that iterates through the resources in this response."));
var getEnumerator = Method(None, ctx.Type<IEnumerator>(), "GetEnumerator")()
.WithExplicitInterfaceSpecifier(ctx.Type<IEnumerable>())
Expand Down
Loading