Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/PaulBraetz/AttributeFactoryGenerator
Generate factories for creating attribute instances from `AttributeData` instances.
https://github.com/PaulBraetz/AttributeFactoryGenerator
Last synced: about 1 month ago
JSON representation
Generate factories for creating attribute instances from `AttributeData` instances.
- Host: GitHub
- URL: https://github.com/PaulBraetz/AttributeFactoryGenerator
- Owner: PaulBraetz
- License: gpl-3.0
- Created: 2023-11-09T22:04:19.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-11-22T15:23:50.000Z (about 1 year ago)
- Last Synced: 2024-05-12T13:32:18.796Z (7 months ago)
- Language: C#
- Size: 39.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- RSCG_Examples - AttributeFactoryGenerator
- csharp-source-generators - AttributeFactoryGenerator - ![stars](https://img.shields.io/github/stars/PaulBraetz/AttributeFactoryGenerator?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/PaulBraetz/AttributeFactoryGenerator?style=flat-square&cacheSeconds=86400) - Generate factories to easily parse actual attribute instances from symbol data. (Meta - libs and generators for other generators / Other)
README
# RhoMicro.AttributeFactoryGenerator
Are you creating source generators for C#? Are you using attributes to allow your consumers to instruct your generator? Are you dissatisfied with the amount of boilerplate you have to write in order to extract those instructions from the roslyn api?
Then this project could be of use to you!
## Key Features & Limitations
- Generate Factory for parsing attribute instance from `AttributeData`
- Generate helper functions for retrieving instances of your attribute from an `IEnumerable`
- Parse type properties as `ITypeSymbol`s- The attribute type (or semantically equivalent type) has to be available to the generator
## How to use
Install the generator through [nuget](https://www.nuget.org/packages/RhoMicro.AttributeFactoryGenerator)
In the assembly through which I distribute the attributes, I declare one:
```cs
[AttributeUsage(AttributeTargets.Class)]
public partial class TestGeneratorTargetAttribute : Attribute
{
public TestGeneratorTargetAttribute(String name, Int32[] ages)
{
Name = name;
Ages = ages;
}
public TestGeneratorTargetAttribute(Type type) => Type = type;public String Name { get; }
public Int32[] Ages { get; }
public Type? Type { get; set; }
}
```Then, in the generator assembly, I add a link to the source file above, as well as a second partial declaration like so:
```cs
[GenerateFactory]
partial class TestGeneratorTargetAttribute
{
[ExcludeFromFactory]
private TestGeneratorTargetAttribute(System.Object typeSymbolContainer) =>
_typeSymbolContainer = typeSymbolContainer;
}
```
The constructor above is required in order to construct an instance when the consumer made use of a constructor taking at least one parameter of type `Type`.For every constructor that takes at least one parameter of type `Type`, an equivalent factory constructor is required. These are expected to take an instance of `Object` instead of the type and assign it to the generated helper field.
This way, a generated helper property of type `ITypeSymbol` may be used to retrieve the type used by the consumer in their `typeof` expression.
Use the generated factory and helper methods like so:
```cs
ImmutableArray attributes =
symbol.GetAttributes();IEnumerable allParsed =
attributes.OfTestGeneratorTargetAttribute();TestGeneratorTargetAttribute singleParsed =
TestGeneratorTargetAttribute.TryCreate(attributes[0], out var a) ?
a :
null;singleParsed =
symbol.TryGetFirstTestGeneratorTargetAttribute(out var a) ?
a :
null;
```
The generated extension method `OfTestGeneratorTargetAttribute` will return all instances of `TestGeneratorTargetAttribute` found in the symbols list of attributes.The generated extension method `TryGetFirstTestGeneratorTargetAttribute` attempts to retrieve the first instance of `TestGeneratorTargetAttribute` found on the symbol.