Skip to content
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

Toml.TryToModel not calling ConvertTo option #30

Closed
donaldblodgett opened this issue Mar 10, 2022 · 1 comment
Closed

Toml.TryToModel not calling ConvertTo option #30

donaldblodgett opened this issue Mar 10, 2022 · 1 comment
Labels
bug Something isn't working enhancement New feature or request

Comments

@donaldblodgett
Copy link

I am attempting to use Tomlyn to read/write a model that contains properties that are non-primitive. Given the following code I would expect that the resulting model would contain the Uri as parsed from the sample text as the TomlModelOptions.ConvertTo is defined. Instead the test fails with the diagnostics containing the following error:

The property value of type System.String couldn't be converted to System.Uri for the property Tests+Config/service_uri

    [Test]
    public void TestUri()
    {
        var expected = new Uri("https://example.com");
        var text = @$"service_uri = ""{expected}""";
        var options = new TomlModelOptions
        {
            ConvertTo = (value, type) => new Uri((string)value)
        };
        var success = Toml.TryToModel<Config>(text, out var result, out var diagnostics, null, options);
        
        Assert.True(success);
        Assert.AreEqual(expected, result?.ServiceUri);
    }

    public class Config
    {
        public Uri ServiceUri { get; set; }
    }

Through debugging this issue it seems that if I temporarily change the value of the variable value at the following line to null

and then revert it back to the original value at the following line

then the ConvertTo option is successfully called and the test passes.

@xoofx
Copy link
Owner

xoofx commented Mar 10, 2022

This is fixed by commit 9b0e19e

[Test]
public void TestUri()
{
var expected = new Uri("https://example.com");
var text = @$"service_uri = ""{expected}""";
var options = new TomlModelOptions
{
ConvertToModel = (value, type) => type == typeof(Uri) ? new Uri((string)value) : null,
ConvertToToml = (value) => value is Uri uri ? uri.ToString() : null
};
var success = Toml.TryToModel<TestConfigWithUri>(text, out var result, out var diagnostics, null, options);
Assert.True(success);
Assert.AreEqual(expected, result?.ServiceUri);
var tomlText = Toml.FromModel(result!, options);
Assert.AreEqual($"service_uri = \"{expected}\"", tomlText.Trim());
}

There are now 2 properties:

  • TomlModelOptions.ConvertToModel (previously ConvertTo).
  • TomlModelOptions.ConvertToToml to allow to convert back a custom type to a TOML representation.

Should be available in 0.14.0+

@xoofx xoofx closed this as completed Mar 10, 2022
@xoofx xoofx added bug Something isn't working enhancement New feature or request labels Mar 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants