Skip to content

Conversation

tsimbalar
Copy link
Member

new PR based on discussion #1047 (comment) .

Make KeyValuePairSettings accept a IReadOnlyDictionary<string, string> instead of a IEnumerable<KeyValuePair<string, string>> to make it more obvious that :

  • it is not consuming the key-value pairs lazily
  • it expects unique keys
  • it will not touch the contents of the provided dictionary

This has no impact on the public API given that class KeyValuePairSettings is internal.

This PR also adds a few tests around the edge cases

@tsimbalar tsimbalar mentioned this pull request Oct 23, 2017
2 tasks
return Settings(new KeyValuePairSettings(settings));

var settingsDictionary = new ReadOnlyDictionary<string, string>(settings.ToDictionary(x => x.Key, x => x.Value));
return Settings(new KeyValuePairSettings(settingsDictionary));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Dictionary implements IReadOnlyDictionary in recent framework versions - I don't think the creation of the ReadOnlyDictionary should be needed here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, that's true. It's one of those strange things where a writable dictionary actually implements read-only dictionary ....

I'm not sure what the "best practices" are in that regard, but I guess in internal classes there is no need to be a control-freak :)

I'll change that.

.Where(kvp => _supportedDirectives.Any(kvp.Key.StartsWith))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

return new ReadOnlyDictionary<string, string>(directives);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here, if we don't create a ReadOnlyDictionary explicitly, we can get this back to a one-liner and avoid the additional method.

(Nitpick - the codebase doesn't use explicit internal modifiers elsewhere.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the method exists for the sake of tests ... but I don't know if the test

+        [Fact]
 +        public void IrrelevantKeysAreIgnored()
 +        {
 +            var irrelevantSettings = new Dictionary<string, string>()
 +            {
 +                ["whatever:foo:bar"] = "willBeIgnored",
 +                ["irrelevant"] = "willBeIgnored",
 +            };
 +
 +            var directives = KeyValuePairSettings.ExtractDirectives(irrelevantSettings);
 +
 +            Assert.False(directives.Any());
 +        }

provides much value ...

(about internal : dammit, I thought I had removed them all !)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I didn't notice the test dependency, thanks 👍

@tsimbalar
Copy link
Member Author

ReadFrom.KeyValuePairs(IEnumerable<KeyValuePair<string, string>>) now keeps the last one in case of duplicate keys. This will allow Serilog.Settings.Combined to rely on that behavior.

Maybe it should also be mentioned in the documentation somewhere ... (or included in #1053 )

For now, I have left an overload ReadFrom.KeyValyePairs(IReadOnlyDictionary<string, string>) as private (I'm trying to resist the itch to make it public :p )

@tsimbalar tsimbalar changed the title Pass a IReadOnlyDictionary to KeyValuePairSettings KeyValuePairSettings - IReadOnlyDictionary / latest setting win Nov 7, 2017
Thibaud DESODT added 6 commits November 8, 2017 07:46
Refactored KeyValuePairSettings to accept a IReadOnlyDictionary<string, string> instead of IEnumerable<KeyValuePair<string, string>> in order to reveal the fact that key-value settings are not lazily consumed. 

Added tests on edge cases : duplicate keys, irrelevant settings.
Instead, take the last value of each key, to remove work from future Serilog.Settings.Combined
@tsimbalar tsimbalar force-pushed the kvpsettings-readonlydict branch from d5f87b0 to 64e45ab Compare November 8, 2017 06:46
@nblumhardt nblumhardt merged commit 4806a0b into serilog:dev Nov 10, 2017
@nblumhardt
Copy link
Member

👍

@tsimbalar tsimbalar deleted the kvpsettings-readonlydict branch November 11, 2017 09:45
@nblumhardt nblumhardt mentioned this pull request Nov 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants