Collects distinct items from a source sequence, ensuring no duplicate export names exist. The type of items being collected The category/type of items being collected (used for error messages) The incremental generator context for reporting diagnostics The source seque
(
string kind,
IncrementalGeneratorInitializationContext context,
IncrementalValuesProvider<T> source,
Func<T, string> toExportName,
Func<T, string> toFullName
| 2038 | /// <param name="toFullName">Function to get the full name of an item (used for error messages)</param> |
| 2039 | /// <returns>An incremental value provider containing the distinct items</returns> |
| 2040 | private static IncrementalValueProvider<EquatableArray<T>> CollectDistinct<T>( |
| 2041 | string kind, |
| 2042 | IncrementalGeneratorInitializationContext context, |
| 2043 | IncrementalValuesProvider<T> source, |
| 2044 | Func<T, string> toExportName, |
| 2045 | Func<T, string> toFullName |
| 2046 | ) |
| 2047 | where T : IEquatable<T> |
| 2048 | { |
| 2049 | var results = source |
| 2050 | .Collect() |
| 2051 | .Select( |
| 2052 | (collected, ct) => |
| 2053 | DiagReporter.With( |
| 2054 | Location.None, |
| 2055 | diag => |
| 2056 | { |
| 2057 | var grouped = collected |
| 2058 | .GroupBy(toExportName) |
| 2059 | // Sort tables and reducers by name to match Rust behaviour. |
| 2060 | // Not really important outside of testing, but for testing |
| 2061 | // it matters because we commit module-bindings |
| 2062 | // so they need to match 1:1 between different langs. |
| 2063 | .OrderBy(g => g.Key); |
| 2064 | |
| 2065 | foreach (var group in grouped.Where(group => group.Count() > 1)) |
| 2066 | { |
| 2067 | diag.Report( |
| 2068 | ErrorDescriptor.DuplicateExport, |
| 2069 | (kind, group.Key, group.Select(toFullName)) |
| 2070 | ); |
| 2071 | } |
| 2072 | |
| 2073 | return new EquatableArray<T>( |
| 2074 | // Only return first item from each group. |
| 2075 | // We already reported duplicates ourselves, and don't want MSBuild to produce lots of duplicate errors too. |
| 2076 | grouped.Select(Enumerable.First).ToImmutableArray() |
| 2077 | ); |
| 2078 | } |
| 2079 | ) |
| 2080 | ); |
| 2081 | |
| 2082 | context.RegisterSourceOutput( |
| 2083 | results, |
| 2084 | (context, results) => |
| 2085 | { |
| 2086 | foreach (var result in results.Diag) |
| 2087 | { |
| 2088 | context.ReportDiagnostic(result); |
| 2089 | } |
| 2090 | } |
| 2091 | ); |
| 2092 | |
| 2093 | return results |
| 2094 | .Select((result, ct) => result.Parsed) |
| 2095 | .WithTrackingName($"SpacetimeDB.{kind}.Collect"); |
| 2096 | } |
| 2097 |