-
Notifications
You must be signed in to change notification settings - Fork 131
Multiple value exception, fixes #103 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
7b18eef
99cb4af
36df0a6
5e6d67d
67e7ca8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,6 +221,10 @@ internal ILookup<string, Dictionary<string, IConfigurationArgumentValue>> GetMet | |
IConfigurationArgumentValue GetArgumentValue(IConfigurationSection argumentSection) | ||
{ | ||
IConfigurationArgumentValue argumentValue; | ||
|
||
if(argumentSection.Value != null && argumentSection.GetChildren().Any()) | ||
throw new InvalidOperationException($"Combined configuration sources must result in a discrete value (string, int, etc.) or complex value (section, list, etc.), not both. Argument: {argumentSection.Path}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! A fellow comment-junkie. I was just thinking, I had to explain this in more detail in our discussion, the check in the code probably warrants a blurb. Sometimes the configuration represents POCOs, for example, so I think I'll stick with "complex types" rather than aggregates. I do like scalar better, though technically MEC treats all discrete values as string -- if your JSON has an int, by the time you get it from config, it'll be the Thinking about this for a comment:
Revised exception message, considering these are always argument values being processed at this point in the code. I also realized there are only three valid scalar types in JSON:
Of course, now it occurs to me that the very clear exception message probably makes the code comment redundant. |
||
|
||
if (argumentSection.Value != null) | ||
{ | ||
argumentValue = new StringArgumentValue(() => argumentSection.Value, argumentSection.GetReloadToken); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,11 @@ namespace Serilog.Settings.Configuration.Tests | |
{ | ||
public class ConfigurationSettingsTests | ||
{ | ||
static LoggerConfiguration ConfigFromJson(string jsonString) | ||
static LoggerConfiguration ConfigFromJson(string jsonString, string secondJsonSource = null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could possibly make this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And YAGNI... I can't think of a test scenario where |
||
{ | ||
var config = new ConfigurationBuilder().AddJsonString(jsonString).Build(); | ||
var builder = new ConfigurationBuilder().AddJsonString(jsonString); | ||
if(secondJsonSource != null) builder.AddJsonString(secondJsonSource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please autoformat |
||
var config = builder.Build(); | ||
return new LoggerConfiguration() | ||
.ReadFrom.Configuration(config); | ||
} | ||
|
@@ -556,5 +558,42 @@ public void WriteToSubLoggerWithLevelSwitchIsSupported() | |
|
||
Assert.Equal(1, DummyRollingFileSink.Emitted.Count); | ||
} | ||
|
||
[Trait("Bugfix", "#103")] | ||
[Fact] | ||
public void MultipleArgumentValuesThrowsInvalidOperationException() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking: |
||
{ | ||
var jsonDiscreteValue = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [{ | ||
""Name"": ""DummyRollingFile"", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rolling file is deprecated, maybe example can start using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hm, I see its used in the rest of the test codebase here; prob leave it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My thinking as well. |
||
""Args"": {""pathFormat"" : ""C:\\""} | ||
}] | ||
} | ||
}"; | ||
|
||
var jsonComplexValue = @"{ | ||
""Serilog"": { | ||
""Using"": [""TestDummies""], | ||
""WriteTo"": [{ | ||
""Name"": ""DummyRollingFile"", | ||
""Args"": {""pathFormat"" : { ""foo"" : ""bar"" } } | ||
}] | ||
} | ||
}"; | ||
|
||
// These will combine into a ConfigurationSection object that has both | ||
// Value == "C:\" and GetChildren() == List<string>. No configuration | ||
// extension matching this exists (in theory an "object" argument could | ||
// accept either value). ConfigurationReader should throw as soon as | ||
// the multiple values are recognized; it will never attempt to locate | ||
// a matching argument. | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => ConfigFromJson(jsonDiscreteValue, jsonComplexValue)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you split the line please? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't match other |
||
|
||
Assert.Contains("Combined configuration sources", ex.Message); | ||
Assert.Contains("pathFormat", ex.Message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean about |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rest of code uses redundant braces ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use standard formatting (ctrl K D) can do it - e.g.
if (
should have a spaceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this has the same precondition, can move inside the other if to L230
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On reflection - the rest of this file is light on redundant braces; please disregard. would be nice if the exception message can be split across lines (example from further down:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed about the if-space -- not my style but definitely seems to be the Serilog way.
On if/else I don't think braces are redundant. I eliminate on stand-alone one-liner ifs, but plenty of other if/else statements elsewhere in the code with braces that could be eliminated.
I think it's much clearer to perform the test separately (my guess is the compiler will optimize anyway, not that it would be measurable).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point re it being subjective whether moving it inside there makes sense; I'll leave it to your judgement.