-
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide possible suggestions for unknown fields (#1106)
* Provide possible suggestions for unknown fields I've updated the deserialization to provide a list of suggestions for unknown field names. This uses the Fastenshtein library to calculate the edit distance between the field name entered by a user and the possible fields. I've chosen to return fields if they have an edit distance less than 3. This is fairly arbitrary and just based on some simple testing. I wanted to provide suggestions, but not provide ones that were completely different to the name entered. Also: - Updated the V1Deserializer to use the `Map()` syntax for mapping rather than doing everything manually. The main reason for this was so that the fields are added as suggestions, although it also removes some unnecessary code. - Altered MetricsDeclarationProvider to not set the defaults if validating the metrics fails. The reason I've done this is that if the metric defaults couldn't be deserialized, we end up with a NullReferenceException. Fixes #1105 * fixup! Provide possible suggestions for unknown fields
- Loading branch information
1 parent
1cac137
commit 5d6291e
Showing
10 changed files
with
232 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Promitor.Tests.Unit/Serialization/DeserializationContextTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Promitor.Core.Scraping.Configuration.Serialization; | ||
using Xunit; | ||
|
||
namespace Promitor.Tests.Unit.Serialization | ||
{ | ||
public class DeserializationContextTests | ||
{ | ||
[Fact] | ||
public void GetSuggestions_NoFieldsConfigured_Empty() | ||
{ | ||
// Arrange | ||
var context = new DeserializationContext<Person>( | ||
new HashSet<string>(), new List<FieldDeserializationInfo>()); | ||
|
||
// Act | ||
var suggestions = context.GetSuggestions("job"); | ||
|
||
// Assert | ||
Assert.Empty(suggestions); | ||
} | ||
|
||
[Fact] | ||
public void GetSuggestions_CloseEnoughSuggestion_ReturnsSuggestion() | ||
{ | ||
// Arrange | ||
var fields = new List<FieldDeserializationInfo> | ||
{ | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Name))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Age))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Country))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.County))) | ||
}; | ||
var context = new DeserializationContext<Person>(new HashSet<string>(), fields); | ||
|
||
// Act | ||
var suggestions = context.GetSuggestions("nmae"); | ||
|
||
// Assert | ||
Assert.Collection(suggestions, suggestion => Assert.Equal("name", suggestion)); | ||
} | ||
|
||
[Fact] | ||
public void GetSuggestions_CloseEnoughSuggestion_ReturnsAllSuggestions() | ||
{ | ||
// Arrange | ||
var fields = new List<FieldDeserializationInfo> | ||
{ | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Name))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Age))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Country))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.County))) | ||
}; | ||
var context = new DeserializationContext<Person>(new HashSet<string>(), fields); | ||
|
||
// Act | ||
var suggestions = context.GetSuggestions("count"); | ||
|
||
// Assert | ||
Assert.Collection(suggestions, | ||
suggestion => Assert.Equal("country", suggestion), | ||
suggestion => Assert.Equal("county", suggestion)); | ||
} | ||
|
||
[Fact] | ||
public void GetSuggestions_IgnoredFields_IncludesIgnoredFieldsInSuggestions() | ||
{ | ||
// Arrange | ||
var fields = new List<FieldDeserializationInfo> | ||
{ | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Name))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Age))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.Country))), | ||
CreateField(typeof(Person).GetProperty(nameof(Person.County))) | ||
}; | ||
var ignoredFields = new HashSet<string> { "named" }; | ||
var context = new DeserializationContext<Person>(ignoredFields, fields); | ||
|
||
// Act | ||
var suggestions = context.GetSuggestions("nam"); | ||
|
||
// Assert | ||
Assert.Collection(suggestions, | ||
suggestion => Assert.Equal("name", suggestion), | ||
suggestion => Assert.Equal("named", suggestion)); | ||
} | ||
|
||
private FieldDeserializationInfo CreateField(PropertyInfo propertyInfo) | ||
{ | ||
return new FieldDeserializationInfo(propertyInfo, false, null, null, null); | ||
} | ||
|
||
public class Person | ||
{ | ||
public string Name { get; set; } | ||
public int Age { get; set; } | ||
public string Country { get; set; } | ||
public string County { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.