Beispiel #1
0
        private ImmutableSortedDictionary <PropertyInfo, Type> GetNavigationCandidates(EntityType entityType)
        {
            if (entityType.FindAnnotation(NavigationCandidatesAnnotationName)?.Value
                is ImmutableSortedDictionary <PropertyInfo, Type> navigationCandidates)
            {
                return(navigationCandidates);
            }

            var dictionaryBuilder = ImmutableSortedDictionary.CreateBuilder <PropertyInfo, Type>(PropertyInfoNameComparer.Instance);

            if (entityType.HasClrType())
            {
                foreach (var propertyInfo in entityType.ClrType.GetRuntimeProperties().OrderBy(p => p.Name))
                {
                    var targetType = FindCandidateNavigationPropertyType(propertyInfo);
                    if (targetType != null)
                    {
                        dictionaryBuilder[propertyInfo] = targetType;
                    }
                }
            }
            navigationCandidates = dictionaryBuilder.ToImmutable();
            SetNavigationCandidates(entityType.Builder, navigationCandidates);
            return(navigationCandidates);
        }
Beispiel #2
0
        public static void TestDebuggerAttributes_Null()
        {
            Type proxyType = DebuggerAttributes.GetProxyType(ImmutableSortedDictionary.CreateBuilder <int, string>());
            TargetInvocationException tie = Assert.Throws <TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object)null));

            Assert.IsType <ArgumentNullException>(tie.InnerException);
        }
Beispiel #3
0
        public MemoryChainStateStorage(ChainedHeader chainTip = null, int?unspentTxCount = null, int?totalTxCount = null, int?totalInputCount = null, int?totalOutputCount = null, int?unspentOutputCount = null, ImmutableSortedDictionary <UInt256, ChainedHeader> headers = null, ImmutableSortedDictionary <UInt256, UnspentTx> unspentTransactions = null, ImmutableDictionary <int, BlockSpentTxes> blockSpentTxes = null, ImmutableDictionary <UInt256, IImmutableList <UnmintedTx> > blockUnmintedTxes = null)
        {
            this.chainTip = CommittedRecord <ChainedHeader> .Initial(chainTip);

            this.unspentTxCount = CommittedRecord <int> .Initial(unspentTxCount ?? 0);

            this.unspentOutputCount = CommittedRecord <int> .Initial(unspentOutputCount ?? 0);

            this.totalTxCount = CommittedRecord <int> .Initial(totalTxCount ?? 0);

            this.totalInputCount = CommittedRecord <int> .Initial(totalInputCount ?? 0);

            this.totalOutputCount = CommittedRecord <int> .Initial(totalOutputCount ?? 0);

            this.headers = CommittedRecord <ImmutableSortedDictionary <UInt256, ChainedHeader> .Builder> .Initial(
                headers?.ToBuilder() ?? ImmutableSortedDictionary.CreateBuilder <UInt256, ChainedHeader>());

            this.unspentTransactions = CommittedRecord <ImmutableSortedDictionary <UInt256, UnspentTx> .Builder> .Initial(
                unspentTransactions?.ToBuilder() ?? ImmutableSortedDictionary.CreateBuilder <UInt256, UnspentTx>());

            this.unspentTxOutputs = CommittedRecord <ImmutableSortedDictionary <TxOutputKey, TxOutput> .Builder> .Initial(
                ImmutableSortedDictionary.CreateBuilder <TxOutputKey, TxOutput>());

            this.blockSpentTxes = CommittedRecord <ImmutableDictionary <int, BlockSpentTxes> .Builder> .Initial(
                blockSpentTxes?.ToBuilder() ?? ImmutableDictionary.CreateBuilder <int, BlockSpentTxes>());

            this.blockUnmintedTxes = CommittedRecord <ImmutableDictionary <UInt256, IImmutableList <UnmintedTx> > .Builder> .Initial(
                blockUnmintedTxes?.ToBuilder() ?? ImmutableDictionary.CreateBuilder <UInt256, IImmutableList <UnmintedTx> >());
        }
Beispiel #4
0
            private static ImmutableArray <TExtension> CreateExtensionsForAllLanguages(Extensions <TExtension> extensions, bool includeDuplicates)
            {
                // Get all analyzers in the assembly.
                var map = ImmutableSortedDictionary.CreateBuilder <string, ImmutableArray <TExtension> >(StringComparer.OrdinalIgnoreCase);

                extensions.AddExtensions(map);

                var builder = ImmutableArray.CreateBuilder <TExtension>();

                foreach (var analyzers in map.Values)
                {
                    foreach (var analyzer in analyzers)
                    {
                        builder.Add(analyzer);
                    }
                }

                if (includeDuplicates)
                {
                    return(builder.ToImmutable());
                }
                else
                {
                    return(builder.Distinct(ExtTypeComparer.Instance).ToImmutableArray());
                }
            }
        public static void CollectRoslynAssemblies(
            string roslynBinariesDirectoryPath,
            ref IImmutableDictionary <AssemblyShortName, AssemblyDetails> mainAssemblies,
            out IImmutableDictionary <AssemblyShortName, AssemblyDetails> usedRoslynAssemblies,
            out IImmutableDictionary <AssemblyShortName, string> roslynAssemblyPaths
            )
        {
            var roslynAssemblyPathsBuilder  = ImmutableSortedDictionary.CreateBuilder <AssemblyShortName, string>();
            var usedRoslynAssembliesBuilder = ImmutableSortedDictionary.CreateBuilder <AssemblyShortName, AssemblyDetails>();

            FluentConsole.White.Line($"Scanning {roslynBinariesDirectoryPath}…");
            foreach (var assemblyPath in Directory.EnumerateFiles(roslynBinariesDirectoryPath, "*.dll", SearchOption.AllDirectories))
            {
                FluentConsole.Gray.Line($"  {Path.GetFileName(assemblyPath)}");
                var name = Path.GetFileNameWithoutExtension(assemblyPath);
                // ReSharper disable once AssignNullToNotNullAttribute
                var assemblyFromMain = mainAssemblies.GetValueOrDefault(name);
                if (assemblyFromMain != null)
                {
                    FluentConsole.Gray.Line("    [used by main]");
                    usedRoslynAssembliesBuilder.Add(name, AssemblyDetails.ReadFrom(assemblyPath, readSymbolsIfExist: true));
                    mainAssemblies = mainAssemblies.Remove(name);
                }
                if (roslynAssemblyPathsBuilder.ContainsKey(name))
                {
                    continue;
                }
                roslynAssemblyPathsBuilder.Add(name, assemblyPath);
            }
            usedRoslynAssemblies = usedRoslynAssembliesBuilder.ToImmutable();
            roslynAssemblyPaths  = roslynAssemblyPathsBuilder.ToImmutable();
        }
Beispiel #6
0
        public void CanDeserialiseImmutableSortedDictionary(bool autoCompile)
        {
            var builder = ImmutableSortedDictionary.CreateBuilder <string, string>();

            builder.Add("a", "1");
            builder.Add("b", "2");
            builder.Add("c", "2");
            var testClass = new ImmutableSortedDictionaryTestClass(builder.ToImmutable());
            var model     = RuntimeTypeModel.Create();

            model.AutoCompile = autoCompile;
            model.Add(typeof(ImmutableSortedDictionaryTestClass));

            model.CompileAndVerify();

            ImmutableSortedDictionaryTestClass testClassClone;

            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, testClass);
                ms.Position = 0;
#pragma warning disable CS0618
                testClassClone = (ImmutableSortedDictionaryTestClass)model.Deserialize(ms, null, testClass.GetType());
#pragma warning restore CS0618
            }

            Assert.Equal((IEnumerable <KeyValuePair <string, string> >)testClass.Dictionary, (IEnumerable <KeyValuePair <string, string> >)testClassClone.Dictionary);
        }
Beispiel #7
0
        public static void CollectRoslynReferences(
            ref IImmutableDictionary <AssemblyShortName, AssemblyDetails> usedRoslynAssemblies,
            IImmutableDictionary <AssemblyShortName, string> roslynAssemblyPaths,
            ref IImmutableDictionary <AssemblyShortName, AssemblyDetails> mainAssemblies,
            IImmutableDictionary <AssemblyShortName, IImmutableSet <PackageInfo> > roslynPackageMap,
            out IImmutableDictionary <AssemblyShortName, AssemblyDetails> othersReferencedByRoslyn
            )
        {
            var othersReferencedByRoslynBuilder = ImmutableSortedDictionary.CreateBuilder <AssemblyShortName, AssemblyDetails>();

            FluentConsole.White.Line("Analyzing Roslyn references…");
            var seen  = new HashSet <AssemblyShortName>();
            var queue = new Queue <AssemblyDetails>(usedRoslynAssemblies.Values);

            while (queue.Count > 0)
            {
                var assembly = queue.Dequeue();
                FluentConsole.Gray.Line($"  {assembly.Definition.Name.Name}");
                seen.Add(assembly.Definition.Name.Name);
                foreach (var reference in assembly.Definition.MainModule.AssemblyReferences)
                {
                    if (!seen.Add(reference.Name))
                    {
                        continue;
                    }
                    FluentConsole.Gray.Line($"    {reference.FullName}");
                    mainAssemblies = mainAssemblies.Remove(reference.Name);
                    if (usedRoslynAssemblies.ContainsKey(reference.Name))
                    {
                        FluentConsole.Gray.Line("      [roslyn assembly, already used]");
                        continue;
                    }
                    var roslynAssemblyPath = roslynAssemblyPaths.GetValueOrDefault(reference.Name);
                    if (roslynAssemblyPath != null)
                    {
                        FluentConsole.Gray.Line("      [roslyn assembly, queued]");
                        var roslynAssembly = AssemblyDetails.ReadFrom(roslynAssemblyPath, readSymbols: true);
                        usedRoslynAssemblies = usedRoslynAssemblies.Add(roslynAssembly.Definition.Name.Name, roslynAssembly);
                        queue.Enqueue(roslynAssembly);
                        continue;
                    }
                    if (InGlobalAssemblyCache(reference))
                    {
                        FluentConsole.Gray.Line("      [gac]");
                        continue;
                    }

                    var referencedAssembly = GetAssemblyDetailsFromNuGetCache(reference.Name, roslynPackageMap);
                    if (referencedAssembly == null)
                    {
                        FluentConsole.Gray.Line("      [system?]");
                        continue;
                    }
                    othersReferencedByRoslynBuilder.Add(reference.Name, referencedAssembly);
                    queue.Enqueue(referencedAssembly);
                }
            }
            othersReferencedByRoslyn = othersReferencedByRoslynBuilder.ToImmutable();
        }
Beispiel #8
0
        public void SerializationTests_ImmutableCollections_SortedDictionary()
        {
            var original = ImmutableSortedDictionary.CreateBuilder <string, string>();

            original.Add("a", "b");
            original.Add("c", "d");
            var dict = original.ToImmutable();

            RoundTripCollectionSerializationTest(dict);
        }
Beispiel #9
0
        public void DebuggerAttributesValid()
        {
            DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSortedDictionary.CreateBuilder <string, int>());
            ImmutableSortedDictionary <int, string> .Builder builder = ImmutableSortedDictionary.CreateBuilder <int, string>();
            builder.Add(1, "One");
            builder.Add(2, "Two");
            DebuggerAttributeInfo info         = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(builder);
            PropertyInfo          itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>().State == DebuggerBrowsableState.RootHidden);

            KeyValuePair <int, string>[] items = itemProperty.GetValue(info.Instance) as KeyValuePair <int, string>[];
            Assert.Equal(builder, items);
        }
Beispiel #10
0
        public void CreateBuilder()
        {
            var builder = ImmutableSortedDictionary.CreateBuilder <string, string>();

            Assert.NotNull(builder);

            builder = ImmutableSortedDictionary.CreateBuilder <string, string>(StringComparer.Ordinal);
            Assert.Same(StringComparer.Ordinal, builder.KeyComparer);
            Assert.Same(EqualityComparer <string> .Default, builder.ValueComparer);

            builder = ImmutableSortedDictionary.CreateBuilder <string, string>(StringComparer.Ordinal, StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.Ordinal, builder.KeyComparer);
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.ValueComparer);
        }
        static void Main(string[] args)
        {
            ImmutableSortedDictionary <int, int> isd = ImmutableSortedDictionary.Create <int, int>();

            isd = isd.Add(1, 1);
            isd = isd.Add(2, 2);
            isd = isd.Remove(2);
            ImmutableSortedDictionary <int, int> .Builder isdBuilder = isd.ToBuilder();
            isdBuilder.Add(10, 10); //adds to original SortedDictionary. returns void.

            ImmutableSortedDictionary <int, int> .Builder builder = ImmutableSortedDictionary.CreateBuilder <int, int>();
            builder.Add(3, 3);
            isd = builder.ToImmutable();
        }
Beispiel #12
0
 public void DeepCopyTests_ImmutableCollections()
 {
     {
         var original = ImmutableDictionary.CreateBuilder <string, Dictionary <string, string> >();
         original.Add("a", new Dictionary <string, string>()
         {
             { "0", "1" },
             { "1", "2" }
         });
         var dict = original.ToImmutable();
         var copy = (ImmutableDictionary <string, Dictionary <string, string> >) this.fixture.SerializationManager.DeepCopy(dict);
         Assert.Same(dict, copy);
     }
     {
         var original = ImmutableArray.Create <string>("1", "2", "3");
         var copy     = (ImmutableArray <string>) this.fixture.SerializationManager.DeepCopy(original);
         Assert.Equal(original, copy);
     }
     {
         var original = ImmutableHashSet.Create("1", "2", "3");
         var copy     = (ImmutableHashSet <string>) this.fixture.SerializationManager.DeepCopy(original);
         Assert.Same(original, copy);
     }
     {
         var original = ImmutableList.Create("1", "2", "3");
         var copy     = (ImmutableList <string>) this.fixture.SerializationManager.DeepCopy(original);
         Assert.Same(original, copy);
     }
     {
         var original = ImmutableQueue.Create("1", "2", "3");
         var copy     = (ImmutableQueue <string>) this.fixture.SerializationManager.DeepCopy(original);
         Assert.Same(original, copy);
     }
     {
         var original = ImmutableSortedDictionary.CreateBuilder <string, Dictionary <string, string> >();
         original.Add("a", new Dictionary <string, string>()
         {
             { "0", "1" },
             { "1", "2" }
         });
         var dict = original.ToImmutable();
         var copy = (ImmutableSortedDictionary <string, Dictionary <string, string> >) this.fixture.SerializationManager.DeepCopy(dict);
         Assert.Same(dict, copy);
     }
     {
         var original = ImmutableSortedSet.Create("1", "2", "3");
         var copy     = (ImmutableSortedSet <string>) this.fixture.SerializationManager.DeepCopy(original);
         Assert.Same(original, copy);
     }
 }
Beispiel #13
0
        public static void CollectMainAssemblies(string binariesDirectoryPath, out IImmutableDictionary <AssemblyShortName, AssemblyDetails> mainAssemblies)
        {
            FluentConsole.White.Line($"Scanning {binariesDirectoryPath}…");
            var mainAssembliesBuilder = ImmutableSortedDictionary.CreateBuilder <AssemblyShortName, AssemblyDetails>();

            foreach (var assemblyPath in Directory.EnumerateFiles(binariesDirectoryPath, "*.*"))
            {
                if (!Regex.IsMatch(assemblyPath, @"(?:\.dll|\.exe)$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant))
                {
                    continue;
                }

                FluentConsole.Gray.Line($"  {Path.GetFileName(assemblyPath)}");
                var name = Path.GetFileNameWithoutExtension(assemblyPath);
                // ReSharper disable once AssignNullToNotNullAttribute
                mainAssembliesBuilder.Add(name, AssemblyDetails.ReadFrom(assemblyPath, readSymbols: false));
            }
            mainAssemblies = mainAssembliesBuilder.ToImmutable();
        }
Beispiel #14
0
        public override ImmutableSortedDictionary <TKey, TValue>?Read(
            ref Utf8JsonReader reader,
            Type typeToConvert,
            JsonSerializerOptions options)
        {
            if (reader.TokenType == JsonTokenType.Null)
            {
                return(null);
            }

            if (reader.TokenType != JsonTokenType.StartObject)
            {
                throw new JsonException("Dictionary must be JSON object.");
            }

            var result = ImmutableSortedDictionary.CreateBuilder <TKey, TValue>();

            while (true)
            {
                if (!reader.Read())
                {
                    throw new JsonException("Incomplete JSON object");
                }

                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return(result.ToImmutable());
                }

                var key = _keyParser(reader.GetString());

                if (!reader.Read())
                {
                    throw new JsonException("Incomplete JSON object");
                }

                var value = JsonSerializer.Deserialize <TValue>(ref reader, options);

                result.Add(key, value);
            }
        }
Beispiel #15
0
        public DataModel(string sourceFilePath, DataModelMetadata metaData, IEnumerable <DataModelType> types)
        {
            this.SourceFilePath = sourceFilePath;
            this.MetaData       = metaData;

            _g4Lookup = ImmutableSortedDictionary.CreateRange(
                StringComparer.Ordinal, types.Select(type => Pair.Make(type.G4DeclaredName, type)));
            ImmutableSortedDictionary <string, DataModelType> .Builder csTypes = ImmutableSortedDictionary.CreateBuilder <string, DataModelType>(StringComparer.Ordinal);
            foreach (DataModelType type in _g4Lookup.Values)
            {
                string key = type.CSharpName;
                if (csTypes.ContainsKey(key))
                {
                    csTypes[key] = null;
                }
                else
                {
                    csTypes.Add(key, type);
                }
            }
        }
Beispiel #16
0
        private static ImmutableSortedDictionary <string, HoistAction> GetViableHoistEdgeList(DataModel source)
        {
            ImmutableSortedDictionary <string, HoistAction> .Builder hoistMap = ImmutableSortedDictionary.CreateBuilder <string, HoistAction>();
            foreach (DataModelType candidate in source.Types)
            {
                if (candidate.HasBase)
                {
                    // Can't be hoist because this would break inheritance chains
                    continue;
                }

                if (candidate.Members.Length != 1)
                {
                    // Can't be hoist because the member can't replace the type
                    continue;
                }

                DataModelMember toBe = candidate.Members[0];
                hoistMap.Add(candidate.G4DeclaredName, new HoistAction(toBe.DeclaredName, toBe.Rank));
            }

            return(hoistMap.ToImmutable());
        }
Beispiel #17
0
        public static IEnumerable <TextMatch> StandardColumnMatchWithQuery(DbConnection conn, TableDefinition tableDef,
                                                                           string query, string substring)
        {
            using (DbCommand command = conn.CreateCommand())
            {
                command.CommandText = query;
                using (DbDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var pKeyBuilder = ImmutableSortedDictionary.CreateBuilder <string, string>();
                        foreach (string pKeyColName in tableDef.PrimaryKeyColumnNames)
                        {
                            string pKeyColVal = reader.GetValue(reader.GetOrdinal(pKeyColName)).ToString();
                            pKeyBuilder[pKeyColName] = pKeyColVal;
                        }
                        ImmutableSortedDictionary <string, string> pKey = pKeyBuilder.ToImmutable();

                        foreach (string textColName in tableDef.TextColumnNames)
                        {
                            int ordinal = reader.GetOrdinal(textColName);
                            if (reader.IsDBNull(ordinal))
                            {
                                continue;
                            }

                            string textColVal = reader.GetString(ordinal);
                            if (textColVal.Contains(substring))
                            {
                                yield return(new TextMatch(pKey, textColName, textColVal));
                            }
                        }
                    }
                }
            }
        }
Beispiel #18
0
 protected override ImmutableSortedDictionary <TKey, TValue> .Builder Create(int count, MessagePackSerializerOptions options)
 {
     return(ImmutableSortedDictionary.CreateBuilder <TKey, TValue>());
 }
        internal static ReleaseTrackingData ReadReleaseTrackingData(
            string path,
            SourceText sourceText,
            Action <string, Version, string, SourceText, TextLine> onDuplicateEntryInRelease,
            Action <TextLine, InvalidEntryKind, string, SourceText> onInvalidEntry,
            bool isShippedFile)
        {
            var releaseTrackingDataByRulesBuilder = new Dictionary <string, ReleaseTrackingDataForRuleBuilder>();
            var currentVersion = UnshippedVersion;
            ReleaseTrackingHeaderKind?   expectedHeaderKind   = isShippedFile ? ReleaseTrackingHeaderKind.ReleaseHeader : ReleaseTrackingHeaderKind.TableHeaderTitle;
            ReleaseTrackingRuleEntryKind?currentRuleEntryKind = null;

            using var versionsBuilder = PooledHashSet <Version> .GetInstance();

            foreach (TextLine line in sourceText.Lines)
            {
                string lineText = line.ToString().Trim();
                if (string.IsNullOrWhiteSpace(lineText) || lineText.StartsWith(";", StringComparison.Ordinal))
                {
                    // Skip blank and comment lines.
                    continue;
                }

                // Parse release header if applicable.
                switch (expectedHeaderKind)
                {
                case ReleaseTrackingHeaderKind.ReleaseHeader:
                    // Parse new release, if any.
                    if (lineText.StartsWith(ReleasePrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        // Expect new table after this line.
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderTitle;

                        // Parse the release version.
                        string versionString = lineText.Substring(ReleasePrefix.Length).Trim();
                        if (!Version.TryParse(versionString, out var version))
                        {
                            OnInvalidEntry(line, InvalidEntryKind.Header);
                            return(ReleaseTrackingData.Default);
                        }
                        else
                        {
                            currentVersion = version;
                            versionsBuilder.Add(version);
                        }

                        continue;
                    }

                    OnInvalidEntry(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderTitle:
                    if (lineText.StartsWith(TableTitleNewRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.New;
                    }
                    else if (lineText.StartsWith(TableTitleRemovedRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Removed;
                    }
                    else if (lineText.StartsWith(TableTitleChangedRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Changed;
                    }
                    else
                    {
                        OnInvalidEntry(line, InvalidEntryKind.Header);
                        return(ReleaseTrackingData.Default);
                    }

                    continue;

                case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1:
                    if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine1, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2;
                        continue;
                    }

                    OnInvalidEntry(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2:
                    expectedHeaderKind = null;
                    if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine2, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    OnInvalidEntry(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1:
                    if (lineText.StartsWith(TableHeaderChangedRulesLine1, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2;
                        continue;
                    }

                    OnInvalidEntry(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2:
                    expectedHeaderKind = null;
                    if (lineText.StartsWith(TableHeaderChangedRulesLine2, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    OnInvalidEntry(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                default:
                    // We might be starting a new release or table.
                    if (lineText.StartsWith("## ", StringComparison.OrdinalIgnoreCase))
                    {
                        goto case ReleaseTrackingHeaderKind.ReleaseHeader;
                    }
                    else if (lineText.StartsWith("### ", StringComparison.OrdinalIgnoreCase))
                    {
                        goto case ReleaseTrackingHeaderKind.TableHeaderTitle;
                    }

                    break;
                }

                RoslynDebug.Assert(currentRuleEntryKind != null);

                var parts = lineText.Split('|').Select(s => s.Trim()).ToArray();
                if (IsInvalidEntry(parts, currentRuleEntryKind.Value))
                {
                    // Report invalid entry, but continue parsing remaining entries.
                    OnInvalidEntry(line, InvalidEntryKind.Other);
                    continue;
                }

                //  New or Removed rule entry:
                //      "Rule ID | Category | Severity | Notes"
                //      "   0    |     1    |    2     |        3           "
                //
                //  Changed rule entry:
                //      "Rule ID | New Category | New Severity | Old Category | Old Severity | Notes"
                //      "   0    |     1        |     2        |     3        |     4        |        5           "

                string ruleId = parts[0];

                InvalidEntryKind?invalidEntryKind = TryParseFields(parts, categoryIndex: 1, severityIndex: 2,
                                                                   out var category, out var defaultSeverity, out var enabledByDefault);
                if (invalidEntryKind.HasValue)
                {
                    OnInvalidEntry(line, invalidEntryKind.Value);
                }

                ReleaseTrackingLine releaseTrackingLine;
                if (currentRuleEntryKind.Value == ReleaseTrackingRuleEntryKind.Changed)
                {
                    invalidEntryKind = TryParseFields(parts, categoryIndex: 3, severityIndex: 4,
                                                      out var oldCategory, out var oldDefaultSeverity, out var oldEnabledByDefault);
                    if (invalidEntryKind.HasValue)
                    {
                        OnInvalidEntry(line, invalidEntryKind.Value);
                    }

                    // Verify at least one field is changed for the entry:
                    if (string.Equals(category, oldCategory, StringComparison.OrdinalIgnoreCase) &&
                        defaultSeverity == oldDefaultSeverity &&
                        enabledByDefault == oldEnabledByDefault)
                    {
                        OnInvalidEntry(line, InvalidEntryKind.Other);
                        return(ReleaseTrackingData.Default);
                    }

                    releaseTrackingLine = new ChangedRuleReleaseTrackingLine(ruleId,
                                                                             category, enabledByDefault, defaultSeverity,
                                                                             oldCategory, oldEnabledByDefault, oldDefaultSeverity,
                                                                             line.Span, sourceText, path, isShippedFile);
                }
                else
                {
                    releaseTrackingLine = new NewOrRemovedRuleReleaseTrackingLine(ruleId,
                                                                                  category, enabledByDefault, defaultSeverity, line.Span, sourceText,
                                                                                  path, isShippedFile, currentRuleEntryKind.Value);
                }

                if (!releaseTrackingDataByRulesBuilder.TryGetValue(ruleId, out var releaseTrackingDataForRuleBuilder))
                {
                    releaseTrackingDataForRuleBuilder = new ReleaseTrackingDataForRuleBuilder();
                    releaseTrackingDataByRulesBuilder.Add(ruleId, releaseTrackingDataForRuleBuilder);
                }

                releaseTrackingDataForRuleBuilder.AddEntry(currentVersion, releaseTrackingLine, out var hasExistingEntry);
                if (hasExistingEntry)
                {
                    onDuplicateEntryInRelease(ruleId, currentVersion, path, sourceText, line);
                }
            }

            var builder = ImmutableSortedDictionary.CreateBuilder <string, ReleaseTrackingDataForRule>();

            foreach (var(ruleId, value) in releaseTrackingDataByRulesBuilder)
            {
                var releaseTrackingDataForRule = new ReleaseTrackingDataForRule(ruleId, value);
                builder.Add(ruleId, releaseTrackingDataForRule);
            }

            return(new ReleaseTrackingData(builder.ToImmutable(), versionsBuilder.ToImmutable()));

            // Local functions
            void OnInvalidEntry(TextLine line, InvalidEntryKind invalidEntryKind)
            => onInvalidEntry(line, invalidEntryKind, path, sourceText);
 protected override ImmutableSortedDictionary <TKey, TValue> .Builder CreateProxy(int knownSize)
 => ImmutableSortedDictionary.CreateBuilder <TKey, TValue>();
 public void DebuggerAttributesValid()
 {
     DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSortedDictionary.CreateBuilder <string, int>());
     DebuggerAttributes.ValidateDebuggerTypeProxyProperties(ImmutableSortedDictionary.CreateBuilder <int, string>());
 }
Beispiel #22
0
 protected override ImmutableSortedDictionary <TKey, TValue> .Builder Create(int count)
 {
     return(ImmutableSortedDictionary.CreateBuilder <TKey, TValue>());
 }
        public MemoryChainStateStorage(Chain chain = null, ImmutableSortedDictionary <UInt256, UnspentTx> unspentTransactions = null, ImmutableDictionary <int, IImmutableList <SpentTx> > blockSpentTxes = null)
        {
            this.chain = chain != null?chain.ToBuilder() : new ChainBuilder();

            this.unspentTransactions = unspentTransactions != null?unspentTransactions.ToBuilder() : ImmutableSortedDictionary.CreateBuilder <UInt256, UnspentTx>();

            this.blockSpentTxes = blockSpentTxes != null?blockSpentTxes.ToBuilder() : ImmutableDictionary.CreateBuilder <int, IImmutableList <SpentTx> >();
        }
Beispiel #24
0
        private static ReleaseTrackingData ReadReleaseTrackingData(
            string path,
            SourceText sourceText,
            Action <Diagnostic> addInvalidFileDiagnostic,
            bool isShippedFile)
        {
            var releaseTrackingDataByRulesBuilder = new Dictionary <string, ReleaseTrackingDataForRuleBuilder>();
            var currentVersion = s_unshippedVersion;

            using var reportedInvalidLines = PooledHashSet <TextLine> .GetInstance();

            ReleaseTrackingHeaderKind?   expectedHeaderKind   = isShippedFile ? ReleaseTrackingHeaderKind.ReleaseHeader : ReleaseTrackingHeaderKind.TableHeaderTitle;
            ReleaseTrackingRuleEntryKind?currentRuleEntryKind = null;

            foreach (TextLine line in sourceText.Lines)
            {
                string lineText = line.ToString().Trim();
                if (string.IsNullOrWhiteSpace(lineText) || lineText.StartsWith(";", StringComparison.Ordinal))
                {
                    // Skip blank and comment lines.
                    continue;
                }

                // Parse release header if applicable.
                switch (expectedHeaderKind)
                {
                case ReleaseTrackingHeaderKind.ReleaseHeader:
                    // Parse new release, if any.
                    if (lineText.StartsWith(ReleasePrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        // Expect new table after this line.
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderTitle;

                        // Parse the release version.
                        string versionString = lineText.Substring(ReleasePrefix.Length).Trim();
                        if (!Version.TryParse(versionString, out var version))
                        {
                            ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                            return(ReleaseTrackingData.Default);
                        }
                        else
                        {
                            currentVersion = version;
                        }

                        continue;
                    }

                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderTitle:
                    if (lineText.StartsWith(TableTitleNewRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.New;
                    }
                    else if (lineText.StartsWith(TableTitleRemovedRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Removed;
                    }
                    else if (lineText.StartsWith(TableTitleChangedRules, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind   = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1;
                        currentRuleEntryKind = ReleaseTrackingRuleEntryKind.Changed;
                    }
                    else
                    {
                        ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                        return(ReleaseTrackingData.Default);
                    }

                    continue;

                case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine1:
                    if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine1, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2;
                        continue;
                    }

                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderNewOrRemovedRulesLine2:
                    expectedHeaderKind = null;
                    if (lineText.StartsWith(TableHeaderNewOrRemovedRulesLine2, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine1:
                    if (lineText.StartsWith(TableHeaderChangedRulesLine1, StringComparison.OrdinalIgnoreCase))
                    {
                        expectedHeaderKind = ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2;
                        continue;
                    }

                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                case ReleaseTrackingHeaderKind.TableHeaderChangedRulesLine2:
                    expectedHeaderKind = null;
                    if (lineText.StartsWith(TableHeaderChangedRulesLine2, StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Header);
                    return(ReleaseTrackingData.Default);

                default:
                    // We might be starting a new release or table.
                    if (lineText.StartsWith("## ", StringComparison.OrdinalIgnoreCase))
                    {
                        goto case ReleaseTrackingHeaderKind.ReleaseHeader;
                    }
                    else if (lineText.StartsWith("### ", StringComparison.OrdinalIgnoreCase))
                    {
                        goto case ReleaseTrackingHeaderKind.TableHeaderTitle;
                    }

                    break;
                }

                RoslynDebug.Assert(currentRuleEntryKind != null);

                var parts = lineText.Split('|').Select(s => s.Trim()).ToArray();
                if (IsInvalidEntry(parts, currentRuleEntryKind.Value))
                {
                    // Report invalid entry, but continue parsing remaining entries.
                    ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Other);
                    continue;
                }

                //  New or Removed rule entry:
                //      "Rule ID | Category | Severity | Notes"
                //      "   0    |     1    |    2     |        3           "
                //
                //  Changed rule entry:
                //      "Rule ID | New Category | New Severity | Old Category | Old Severity | Notes"
                //      "   0    |     1        |     2        |     3        |     4        |        5           "

                string ruleId = parts[0];

                InvalidEntryKind?invalidEntryKind = TryParseFields(parts, categoryIndex: 1, severityIndex: 2,
                                                                   out var category, out var defaultSeverity, out var enabledByDefault);
                if (invalidEntryKind.HasValue)
                {
                    ReportInvalidEntryDiagnostic(line, invalidEntryKind.Value);
                }

                ReleaseTrackingLine releaseTrackingLine;
                if (currentRuleEntryKind.Value == ReleaseTrackingRuleEntryKind.Changed)
                {
                    invalidEntryKind = TryParseFields(parts, categoryIndex: 3, severityIndex: 4,
                                                      out var oldCategory, out var oldDefaultSeverity, out var oldEnabledByDefault);
                    if (invalidEntryKind.HasValue)
                    {
                        ReportInvalidEntryDiagnostic(line, invalidEntryKind.Value);
                    }

                    // Verify at least one field is changed for the entry:
                    if (string.Equals(category, oldCategory, StringComparison.OrdinalIgnoreCase) &&
                        defaultSeverity == oldDefaultSeverity &&
                        enabledByDefault == oldEnabledByDefault)
                    {
                        ReportInvalidEntryDiagnostic(line, InvalidEntryKind.Other);
                        return(ReleaseTrackingData.Default);
                    }

                    releaseTrackingLine = new ChangedRuleReleaseTrackingLine(ruleId,
                                                                             category, enabledByDefault, defaultSeverity,
                                                                             oldCategory, oldEnabledByDefault, oldDefaultSeverity,
                                                                             line.Span, sourceText, path, isShippedFile);
                }
                else
                {
                    releaseTrackingLine = new NewOrRemovedRuleReleaseTrackingLine(ruleId,
                                                                                  category, enabledByDefault, defaultSeverity, line.Span, sourceText,
                                                                                  path, isShippedFile, currentRuleEntryKind.Value);
                }

                if (!releaseTrackingDataByRulesBuilder.TryGetValue(ruleId, out var releaseTrackingDataForRuleBuilder))
                {
                    releaseTrackingDataForRuleBuilder = new ReleaseTrackingDataForRuleBuilder();
                    releaseTrackingDataByRulesBuilder.Add(ruleId, releaseTrackingDataForRuleBuilder);
                }

                releaseTrackingDataForRuleBuilder.AddEntry(currentVersion, releaseTrackingLine, out var hasExistingEntry);
                if (hasExistingEntry && reportedInvalidLines.Add(line))
                {
                    // Rule '{0}' has more then one entry for release '{1}' in analyzer release file '{2}'.
                    string           arg1             = ruleId;
                    string           arg2             = currentVersion == s_unshippedVersion ? "unshipped" : currentVersion.ToString();
                    string           arg3             = Path.GetFileName(path);
                    LinePositionSpan linePositionSpan = sourceText.Lines.GetLinePositionSpan(line.Span);
                    Location         location         = Location.Create(path, line.Span, linePositionSpan);
                    var diagnostic = Diagnostic.Create(RemoveDuplicateEntriesForAnalyzerReleaseRule, location, arg1, arg2, arg3);
                    addInvalidFileDiagnostic(diagnostic);
                }
            }

            var builder = ImmutableSortedDictionary.CreateBuilder <string, ReleaseTrackingDataForRule>();

            foreach (var(ruleId, value) in releaseTrackingDataByRulesBuilder)
            {
                var releaseTrackingDataForRule = new ReleaseTrackingDataForRule(ruleId, value);
                builder.Add(ruleId, releaseTrackingDataForRule);
            }

            return(new ReleaseTrackingData(builder.ToImmutable()));

            // Local functions
            void ReportInvalidEntryDiagnostic(TextLine line, InvalidEntryKind invalidEntryKind)
            {
                if (!reportedInvalidLines.Add(line))
                {
                    // Already reported.
                    return;
                }

                var rule = invalidEntryKind switch
                {
                    // Analyzer release file '{0}' has a missing or invalid release header '{1}'.
                    InvalidEntryKind.Header => InvalidHeaderInAnalyzerReleasesFileRule,

                    // Analyzer release file '{0}' has an entry with one or more 'Undetected' fields that need to be manually filled in '{1}'.
                    InvalidEntryKind.UndetectedField => InvalidUndetectedEntryInAnalyzerReleasesFileRule,

                    // Analyzer release file '{0}' has an invalid entry '{1}'.
                    InvalidEntryKind.Other => InvalidEntryInAnalyzerReleasesFileRule,
                    _ => throw new NotImplementedException(),
                };

                string           arg1             = Path.GetFileName(path);
                string           arg2             = line.ToString();
                LinePositionSpan linePositionSpan = sourceText.Lines.GetLinePositionSpan(line.Span);
                Location         location         = Location.Create(path, line.Span, linePositionSpan);
                var diagnostic = Diagnostic.Create(rule, location, arg1, arg2);

                addInvalidFileDiagnostic(diagnostic);
            }