openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
codegen/generator/src/Visitors/ModelSerializationVisitor.cs
35lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.ClientModel; |
| 2 | using Microsoft.TypeSpec.Generator.ClientModel.Providers; |
| 3 | using Microsoft.TypeSpec.Generator.Input; |
| 4 | using Microsoft.TypeSpec.Generator.Providers; |
| 5 | using System.Linq; |
| 6 | |
| 7 | namespace OpenAILibraryPlugin.Visitors; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// A visitor to add MRW serialization to models. |
| 11 | /// </summary> |
| 12 | public class ModelSerializationVisitor : ScmLibraryVisitor |
| 13 | { |
| 14 | protected override ModelProvider? PreVisitModel(InputModelType model, ModelProvider? type) |
| 15 | { |
| 16 | if (type is null || model.Usage.HasFlag(InputModelTypeUsage.Json)) |
| 17 | { |
| 18 | return base.PreVisitModel(model, type); |
| 19 | } |
| 20 | |
| 21 | foreach (var provider in type.SerializationProviders) |
| 22 | { |
| 23 | if (provider is MrwSerializationTypeDefinition) |
| 24 | { |
| 25 | return base.PreVisitModel(model, type); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | var serializations = type.SerializationProviders.ToList(); |
| 30 | serializations.Add(new MrwSerializationTypeDefinition(model, type)); |
| 31 | type.Update(serializations: serializations); |
| 32 | |
| 33 | return base.PreVisitModel(model, type); |
| 34 | } |
| 35 | } |
| 36 | |