Skip to content

Commit

Permalink
Merge pull request #41 from TylerReid/optionToIgnoreProperties
Browse files Browse the repository at this point in the history
add IgnoreMissingProperties to TomlModelOptions
  • Loading branch information
xoofx committed Jul 1, 2022
2 parents e7b7629 + 443d20d commit 5605d7c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Tomlyn.Tests/ModelTests/ReflectionModelTests.cs
Expand Up @@ -424,6 +424,24 @@ public void TestModelWithFixedList()
CollectionAssert.AreEqual(new List<string> { "1", "2", "3" }, model.Values);
}

[Test]
public void TestModelWithMissingProperties()
{
var input = @"values = ['1', '2', '3']
some_thing_that_doesnt_exist = true";

StandardTests.DisplayHeader("input");
Console.WriteLine(input);
var model = Toml.ToModel<SimpleModel>(input, options: new TomlModelOptions
{
IgnoreMissingProperties = true,
});
CollectionAssert.AreEqual(new List<string> { "1", "2", "3" }, model.Values);

Assert.Throws<TomlException>(() => Toml.ToModel<SimpleModel>(input));

}

public class SimpleModel
{
public SimpleModel()
Expand Down
3 changes: 3 additions & 0 deletions src/Tomlyn/Model/Accessors/DynamicModelReadContext.cs
Expand Up @@ -18,6 +18,7 @@ public DynamicModelReadContext(TomlModelOptions options)
ConvertPropertyName = options.ConvertPropertyName;
CreateInstance = options.CreateInstance;
ConvertToModel = options.ConvertToModel;
IgnoreMissingProperties = options.IgnoreMissingProperties;
Diagnostics = new DiagnosticsBag();
_accessors = new Dictionary<Type, DynamicAccessor>();
}
Expand All @@ -31,6 +32,8 @@ public DynamicModelReadContext(TomlModelOptions options)
public Func<object, Type, object?>? ConvertToModel { get; set; }

public DiagnosticsBag Diagnostics { get; }

public bool IgnoreMissingProperties { get; set; }

public DynamicAccessor GetAccessor(Type type)
{
Expand Down
9 changes: 7 additions & 2 deletions src/Tomlyn/Model/Accessors/StandardObjectDynamicAccessor.cs
Expand Up @@ -165,8 +165,13 @@ public override bool TryGetPropertyType(SourceSpan span, string name, out Type?
return true;
}

// Otherwise, it's an error.
Context.Diagnostics.Error(span, $"The property `{name}` was not found on object type {TargetType.FullName}");
// If configured to ignore missing properties on the target type,
// return false to indicate it is missing but don't set an error
if (!Context.IgnoreMissingProperties)
{
// Otherwise, it's an error.
Context.Diagnostics.Error(span, $"The property `{name}` was not found on object type {TargetType.FullName}");
}
return false;
}

Expand Down
9 changes: 9 additions & 0 deletions src/Tomlyn/TomlModelOptions.cs
Expand Up @@ -24,6 +24,7 @@ public TomlModelOptions()
GetPropertyName = DefaultGetPropertyNameImpl;
CreateInstance = DefaultCreateInstance;
ConvertPropertyName = DefaultConvertPropertyName;
IgnoreMissingProperties = false;

AttributeListForIgnore = new List<string>()
{
Expand Down Expand Up @@ -113,6 +114,14 @@ public TomlModelOptions()
/// </remarks>
public List<string> AttributeListForGetName { get; }

/// <summary>
/// Gets or sets the option to ignore properties in the TOML that are missing from a custom model
/// </summary>
/// <remarks>
/// By default this is false
/// </remarks>
public bool IgnoreMissingProperties { get; set; }

/// <summary>
/// Default implementation for getting the property name
/// </summary>
Expand Down

0 comments on commit 5605d7c

Please sign in to comment.