using System.Linq; using Microsoft.CodeAnalysis; namespace Amazon.Lambda.Annotations.SourceGenerator.Extensions { public static class SymbolHasAttributeExtension { /// /// Returns true if symbol has an given attribute. /// /// Symbol exposed by the compiler. /// Source generator context. /// Fully qualified type name. /// public static bool HasAttribute(this ISymbol symbol, GeneratorExecutionContext context, string fullyQualifiedName) { return symbol.GetAttributes() .Any(att => { if (att.AttributeClass == null) { return false; } return att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(fullyQualifiedName), SymbolEqualityComparer.Default); }); } /// /// Returns attribute data if symbol has an given attribute. /// If there symbol doesn't have the attribute, returns null. /// /// Symbol exposed by the compiler. /// Source generator context. /// Fully qualified type name. /// for the given attribute. public static AttributeData GetAttributeData(this ISymbol symbol, GeneratorExecutionContext context, string fullyQualifiedName) { return symbol.GetAttributes() .FirstOrDefault(att => { if (att.AttributeClass == null) { return false; } return att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(fullyQualifiedName), SymbolEqualityComparer.Default); }); } } }