### Define Avro Enum and Record Schemas Source: https://context7.com/alexrosenfeld10/avro-sourcegen-csharp/llms.txt Examples of defining Avro enum types and record types with cross-file references using JSON schema definitions. ```json { "name": "PlanetEnum", "namespace": "Space.Models", "type": "enum", "symbols": ["Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] } ``` ```json { "name": "SolarSystem", "namespace": "Space.Bodies", "type": "record", "fields": [ { "name": "Name", "type": "string" }, { "name": "Planets", "type": { "type": "array", "items": "Space.Models.PlanetEnum" } } ] } ``` ```json { "name": "SpaceShip", "namespace": "Space.Travel", "type": "record", "fields": [ { "name": "Name", "type": "string" }, { "name": "HomePlanet", "type": "Space.Models.PlanetEnum" } ] } ``` -------------------------------- ### Consume Generated C# Models Source: https://context7.com/alexrosenfeld10/avro-sourcegen-csharp/llms.txt Demonstrates how to instantiate and use the strongly-typed C# classes generated from Avro schemas within a .NET application. ```csharp using Space.Bodies; using Space.Models; using Space.Travel; PlanetEnum homePlanet = PlanetEnum.Earth; var rocketMcRocketFace = new SpaceShip { Name = "Rocket McRocketFace", HomePlanet = PlanetEnum.Earth }; Console.WriteLine($"Ship: {rocketMcRocketFace.Name}, Home: {rocketMcRocketFace.HomePlanet}"); var ourSolarSystem = new SolarSystem { Name = "Our Solar System", Planets = new List { PlanetEnum.Earth, PlanetEnum.Jupiter } }; Console.WriteLine($"System: {ourSolarSystem.Name}, Planet count: {ourSolarSystem.Planets.Count}"); ``` -------------------------------- ### Configure .NET Project for Source Generation Source: https://context7.com/alexrosenfeld10/avro-sourcegen-csharp/llms.txt Configures the .csproj file to include the Avro source generator as an analyzer and process schema files as AdditionalFiles. ```xml false Analyzer ``` -------------------------------- ### AvroUtils: Generate C# Source Code from Avro Schemas Source: https://context7.com/alexrosenfeld10/avro-sourcegen-csharp/llms.txt Provides functionality to generate C# source code from Avro schema definitions. It parses Avro JSON schema files, utilizes Apache Avro's CodeGen for type generation, and handles cross-file type resolution using SchemaNames. The generated code is written to a temporary directory and then read back. ```csharp using System.Collections.Immutable; using Avro; namespace AvroSourceGenerator; public static class AvroUtils { // Shared schema names registry for cross-file type resolution private static readonly SchemaNames SchemaNames = new(); public static IEnumerable<(string name, string code)> GenerateSourceCode( ImmutableArray<(string schemaName, string schemaContents)> schemaNamesAndContents) { var codeGen = new CodeGen(); // Parse each schema, using shared SchemaNames for type resolution foreach (var (_, schemaContents) in schemaNamesAndContents) { var schema = Schema.Parse(schemaContents, SchemaNames); codeGen.AddSchema(schema); } // Generate C# code for all schemas codeGen.GenerateCode(); // Write generated types to temp directory and read back var tempDir = Path.Join(Path.GetTempPath(), $"avro_generated_{Guid.NewGuid():N}"); Directory.CreateDirectory(tempDir); codeGen.WriteTypes(tempDir, true); var generatedSourceCode = (from file in Directory.GetFiles(tempDir) let sourceCode = File.ReadAllText(file) select (Path.GetFileNameWithoutExtension(file), sourceCode)) .ToArray(); Directory.Delete(tempDir, true); return generatedSourceCode; } } // Example usage in unit tests: var schemaNamesAndContents = new[] { ("planet-enum", File.ReadAllText("./planet-enum.json")), ("solar-system-model", File.ReadAllText("./solar-system-model.json")) }.ToImmutableArray(); var generatedSources = AvroUtils.GenerateSourceCode(schemaNamesAndContents); // Returns tuples like: ("PlanetEnum", "public enum PlanetEnum { ... }"), ("SolarSystem", "public class SolarSystem { ... }") ``` -------------------------------- ### AvroGenerator: C# Incremental Source Generator for Avro Schemas Source: https://context7.com/alexrosenfeld10/avro-sourcegen-csharp/llms.txt Implements Roslyn's IIncrementalGenerator to process specified Avro schema files during compilation. It filters additional text files, extracts schema names and content, and registers a source output to generate C# code using AvroUtils. This enables compile-time generation of strongly-typed C# classes from Avro schemas. ```csharp using Microsoft.CodeAnalysis; namespace AvroSourceGenerator; [Generator] public class AvroGenerator : IIncrementalGenerator { // Define which Avro schemas to process private readonly IEnumerable _avroSchemasToGenerate = new HashSet { "planet-enum", "solar-system-model", "space-ship-model" }; public void Initialize(IncrementalGeneratorInitializationContext initContext) { // Filter additional text files to only process specified schemas var schemaFiles = initContext .AdditionalTextsProvider .Where(file => _avroSchemasToGenerate.Contains(Path.GetFileNameWithoutExtension(file.Path))); // Extract file name and content pairs var schemaFileNameAndContents = schemaFiles.Select( static (file, cancellationToken) => (name: Path.GetFileNameWithoutExtension(file.Path), content: file.GetText(cancellationToken)!.ToString())) .Collect(); // Register source output to generate C# code from schemas initContext.RegisterSourceOutput(schemaFileNameAndContents, static (spc, namesAndContents) => { var generatedSources = AvroUtils.GenerateSourceCode(namesAndContents); foreach (var (name, code) in generatedSources) { spc.AddSource($"AvroGenerated_{name}", code); } }); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.