-
Notifications
You must be signed in to change notification settings - Fork 176
Refactor and fixes in source input model #3445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ArcturusZhang
merged 7 commits into
Azure:feature/v3
from
ArcturusZhang:refactor-and-fixes-in-source-input-model
May 31, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
891eb40
refactor
ArcturusZhang 3176fb8
fix the issue when a member has both attributes
ArcturusZhang 5530f10
add back the codegen suppress attribute
ArcturusZhang 60ba859
more refactor
ArcturusZhang 6675e1e
remove the null check and make it non-nullable
ArcturusZhang 2f491a7
rename two methods
ArcturusZhang 464d97d
resolve comments
ArcturusZhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/AutoRest.CSharp/Common/Input/Source/CodeGenAttributes.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Immutable; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Linq; | ||
| using Azure.Core; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace AutoRest.CSharp.Input.Source | ||
| { | ||
| public class CodeGenAttributes | ||
| { | ||
| public CodeGenAttributes(Compilation compilation) | ||
| { | ||
| CodeGenSuppressAttribute = GetSymbol(compilation, typeof(CodeGenSuppressAttribute)); | ||
| CodeGenMemberAttribute = GetSymbol(compilation, typeof(CodeGenMemberAttribute)); | ||
| CodeGenTypeAttribute = GetSymbol(compilation, typeof(CodeGenTypeAttribute)); | ||
| CodeGenModelAttribute = GetSymbol(compilation, typeof(CodeGenModelAttribute)); | ||
| CodeGenClientAttribute = GetSymbol(compilation, typeof(CodeGenClientAttribute)); | ||
| CodeGenMemberSerializationAttribute = GetSymbol(compilation, typeof(CodeGenMemberSerializationAttribute)); | ||
| CodeGenMemberSerializationHooksAttribute = GetSymbol(compilation, typeof(CodeGenMemberSerializationHooksAttribute)); | ||
| } | ||
|
|
||
| public INamedTypeSymbol CodeGenSuppressAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenMemberAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenTypeAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenModelAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenClientAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenMemberSerializationAttribute { get; } | ||
|
|
||
| public INamedTypeSymbol CodeGenMemberSerializationHooksAttribute { get; } | ||
|
|
||
| private static INamedTypeSymbol GetSymbol(Compilation compilation, Type type) => compilation.GetTypeByMetadataName(type.FullName!) ?? throw new InvalidOperationException($"cannot load symbol of attribute {type}"); | ||
|
|
||
| private static bool CheckAttribute(AttributeData attributeData, INamedTypeSymbol codeGenAttribute) | ||
| => SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, codeGenAttribute); | ||
|
|
||
| public bool TryGetCodeGenMemberAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string name) | ||
| { | ||
| name = null; | ||
| if (!CheckAttribute(attributeData, CodeGenMemberAttribute)) | ||
| return false; | ||
|
|
||
| name = attributeData.ConstructorArguments.FirstOrDefault().Value as string; | ||
| return name != null; | ||
| } | ||
|
|
||
| public bool TryGetCodeGenMemberSerializationAttributeValue(AttributeData attributeData, [MaybeNullWhen(false)] out string[] propertyNames) | ||
| { | ||
| propertyNames = null; | ||
| if (!CheckAttribute(attributeData, CodeGenMemberSerializationAttribute)) | ||
| return false; | ||
|
|
||
| if (attributeData.ConstructorArguments.Length > 0) | ||
| { | ||
| propertyNames = ToStringArray(attributeData.ConstructorArguments[0].Values); | ||
| } | ||
|
|
||
| return propertyNames != null; | ||
| } | ||
|
|
||
| public bool TryGetCodeGenMemberSerializationHooksAttributeValue(AttributeData attributeData, out (string? SerializationHook, string? DeserializationHook) hooks) | ||
| { | ||
| hooks = default; | ||
| if (!CheckAttribute(attributeData, CodeGenMemberSerializationHooksAttribute)) | ||
| return false; | ||
|
|
||
| string? serializationHook = null; | ||
| string? deserializationHook = null; | ||
|
|
||
| var arguments = attributeData.ConstructorArguments; | ||
| serializationHook = arguments[0].Value as string; | ||
| deserializationHook = arguments[1].Value as string; | ||
|
|
||
| hooks = (serializationHook, deserializationHook); | ||
| return serializationHook != null || deserializationHook != null; | ||
| } | ||
|
|
||
| public bool TryGetCodeGenModelAttributeValue(AttributeData attributeData, out string[]? usage, out string[]? formats) | ||
lirenhe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| usage = null; | ||
| formats = null; | ||
| if (!CheckAttribute(attributeData, CodeGenModelAttribute)) | ||
| return false; | ||
| foreach (var namedArgument in attributeData.NamedArguments) | ||
| { | ||
| switch (namedArgument.Key) | ||
| { | ||
| case nameof(Azure.Core.CodeGenModelAttribute.Usage): | ||
| usage = ToStringArray(namedArgument.Value.Values); | ||
| break; | ||
| case nameof(Azure.Core.CodeGenModelAttribute.Formats): | ||
| formats = ToStringArray(namedArgument.Value.Values); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return usage != null || formats != null; | ||
| } | ||
|
|
||
| private static string[]? ToStringArray(ImmutableArray<TypedConstant> values) | ||
| { | ||
| if (values.IsDefaultOrEmpty) | ||
| { | ||
| return null; | ||
lirenhe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return values | ||
| .Select(v => (string?)v.Value) | ||
| .OfType<string>() | ||
| .ToArray(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.