MCPcopy Create free account
hub / github.com/apache/fory / Emit

Method Emit

csharp/src/Fory.Generator/ForyModelGenerator.cs:115–198  ·  view source on GitHub ↗
(SourceProductionContext context, ImmutableArray<TypeModel?> maybeModels)

Source from the content-addressed store, hash-verified

113 }
114
115 private static void Emit(SourceProductionContext context, ImmutableArray<TypeModel?> maybeModels)
116 {
117 if (maybeModels.IsDefaultOrEmpty)
118 {
119 return;
120 }
121
122 Dictionary<string, TypeModel> models = new(StringComparer.Ordinal);
123 foreach (TypeModel? maybeModel in maybeModels)
124 {
125 if (maybeModel is null)
126 {
127 continue;
128 }
129
130 if (!maybeModel.Diagnostics.IsDefaultOrEmpty)
131 {
132 foreach (Diagnostic diagnostic in maybeModel.Diagnostics)
133 {
134 context.ReportDiagnostic(diagnostic);
135 }
136
137 continue;
138 }
139
140 models[maybeModel.TypeName] = maybeModel;
141 }
142
143 if (models.Count == 0)
144 {
145 return;
146 }
147
148 StringBuilder sb = new();
149 sb.AppendLine("// <auto-generated/>");
150 sb.AppendLine("#nullable enable");
151 sb.AppendLine("namespace Apache.Fory.Generated;");
152 sb.AppendLine();
153
154 foreach (KeyValuePair<string, TypeModel> entry in models.OrderBy(kv => kv.Key, StringComparer.Ordinal))
155 {
156 TypeModel model = entry.Value;
157 if (model.Kind == DeclKind.Struct || model.Kind == DeclKind.Class)
158 {
159 EmitObjectSerializer(sb, model);
160 sb.AppendLine();
161 }
162 else if (model.Kind == DeclKind.Union)
163 {
164 EmitUnionSerializer(sb, model);
165 sb.AppendLine();
166 }
167 }
168
169 sb.AppendLine("internal static class __ForyGeneratedModuleInitializer");
170 sb.AppendLine("{");
171 sb.AppendLine(" [global::System.Runtime.CompilerServices.ModuleInitializer]");
172 sb.AppendLine(" internal static void Register()");

Callers

nothing calls this directly

Calls 2

FromMethod · 0.80
ToStringMethod · 0.45

Tested by

no test coverage detected