C
C#3w ago
Sckab

Having problem with TOML parser

Basically i'm building a console application, and i want the possibility of creating a .toml config file, for parsing the file i'm using Tomlyn, this is the code: This is the error it gives:
Tomlyn.TomlException: (1,2) : error : Unable to set the property Config on object type dinfo.core.Handlers.TomlConfiguration.ConfigFile.
(5,2) : error : Unable to set the property IgnoreTest on object type dinfo.core.Handlers.TomlConfiguration.ConfigFile.
Tomlyn.TomlException: (1,2) : error : Unable to set the property Config on object type dinfo.core.Handlers.TomlConfiguration.ConfigFile.
(5,2) : error : Unable to set the property IgnoreTest on object type dinfo.core.Handlers.TomlConfiguration.ConfigFile.
I can't share the code cuz it's too long, i will send it in separate messages
6 Replies
Sckab
SckabOP3w ago
classes for tables
using System.Runtime.Serialization;
using dinfo.core.Utils.Globals;
using Tomlyn;

namespace dinfo.core.Handlers.TomlConfiguration;

public class ConfigFile
{
[DataMember(Name = "Config")]
public ConfigSection Config { get; set; } = new();

[DataMember(Name = "Ignore")]
public IgnoreSection Ignore { get; set; } = new();
}

public class ConfigSection
{
public bool RecursiveConfig { get; set; } = false;
public bool GitignoreConfig { get; set; } = false;
public bool VerboseConfig { get; set; } = false;
public bool NoTuiConfig { get; set; } = false;
public bool IgnoreGitignoreConfig { get; set; } = false;
}

public class IgnoreSection
{
[DataMember(Name = "DirectoryPaths")]
public List<string> DirectoryPaths { get; set; } = new();

[DataMember(Name = "FilePaths")]
public List<string> FilePaths { get; set; } = new();
}
using System.Runtime.Serialization;
using dinfo.core.Utils.Globals;
using Tomlyn;

namespace dinfo.core.Handlers.TomlConfiguration;

public class ConfigFile
{
[DataMember(Name = "Config")]
public ConfigSection Config { get; set; } = new();

[DataMember(Name = "Ignore")]
public IgnoreSection Ignore { get; set; } = new();
}

public class ConfigSection
{
public bool RecursiveConfig { get; set; } = false;
public bool GitignoreConfig { get; set; } = false;
public bool VerboseConfig { get; set; } = false;
public bool NoTuiConfig { get; set; } = false;
public bool IgnoreGitignoreConfig { get; set; } = false;
}

public class IgnoreSection
{
[DataMember(Name = "DirectoryPaths")]
public List<string> DirectoryPaths { get; set; } = new();

[DataMember(Name = "FilePaths")]
public List<string> FilePaths { get; set; } = new();
}
main class:
public class ConfigFileHandler
{
public static void FindTomlRoot(string targetDirectory)
{
while (!string.IsNullOrEmpty(targetDirectory))
{
if (Directory.Exists(Path.Combine(targetDirectory, ".git")))
{
GlobalsUtils.ConfigFilePath = targetDirectory;
return;
}

var parentDir = Directory.GetParent(targetDirectory);
targetDirectory = parentDir != null ? parentDir.FullName : string.Empty;
}

GlobalsUtils.ConfigFilePath = string.Empty;
}

public static void DeserializeConfigFile(string configFilePath)
{
string configContent = File.ReadAllText(configFilePath);

var config = Toml.ToModel<ConfigFile>(configContent);

GlobalsUtils.Recursive = config.Config.RecursiveConfig;
GlobalsUtils.IgnoreGitignore = config.Config.IgnoreGitignoreConfig;
GlobalsUtils.NoTui = config.Config.NoTuiConfig;
GlobalsUtils.Verbose = config.Config.VerboseConfig;

GlobalsUtils.IgnoredFiles = config.Ignore.FilePaths;
GlobalsUtils.IgnoredDirectories = config.Ignore.DirectoryPaths;
}
}
public class ConfigFileHandler
{
public static void FindTomlRoot(string targetDirectory)
{
while (!string.IsNullOrEmpty(targetDirectory))
{
if (Directory.Exists(Path.Combine(targetDirectory, ".git")))
{
GlobalsUtils.ConfigFilePath = targetDirectory;
return;
}

var parentDir = Directory.GetParent(targetDirectory);
targetDirectory = parentDir != null ? parentDir.FullName : string.Empty;
}

GlobalsUtils.ConfigFilePath = string.Empty;
}

public static void DeserializeConfigFile(string configFilePath)
{
string configContent = File.ReadAllText(configFilePath);

var config = Toml.ToModel<ConfigFile>(configContent);

GlobalsUtils.Recursive = config.Config.RecursiveConfig;
GlobalsUtils.IgnoreGitignore = config.Config.IgnoreGitignoreConfig;
GlobalsUtils.NoTui = config.Config.NoTuiConfig;
GlobalsUtils.Verbose = config.Config.VerboseConfig;

GlobalsUtils.IgnoredFiles = config.Ignore.FilePaths;
GlobalsUtils.IgnoredDirectories = config.Ignore.DirectoryPaths;
}
}
Angius
Angius3w ago
What does the TOML file you're trying to read look like?
Sckab
SckabOP3w ago
It's a user made one, it should look something like this:
[Config]
Recursive = true
IgnoreGitignore = true

[Ignore]
Directories = [ "obj" ]
Files = [ output.json ]
[Config]
Recursive = true
IgnoreGitignore = true

[Ignore]
Directories = [ "obj" ]
Files = [ output.json ]
I want the user to be able to create a config file, just docker.toml
Angius
Angius3w ago
Well, this doesn't align:
public class IgnoreSection
{
[DataMember(Name = "DirectoryPaths")]
public List<string> DirectoryPaths { get; set; } = new();

[DataMember(Name = "FilePaths")]
public List<string> FilePaths { get; set; } = new();
}
public class IgnoreSection
{
[DataMember(Name = "DirectoryPaths")]
public List<string> DirectoryPaths { get; set; } = new();

[DataMember(Name = "FilePaths")]
public List<string> FilePaths { get; set; } = new();
}
[Ignore]
Directories = [ "obj" ]
Files = [ output.json ]
[Ignore]
Directories = [ "obj" ]
Files = [ output.json ]
Sckab
SckabOP3w ago
I know, i first wanna make sure it's working, then i will fix the names
Angius
Angius3w ago
Well, the exception seems to be happening because some property cannot be set to a given value. It's possible that it cannot be set, because there is no value to set it to. Or that the property doesn't exist in the TOML file

Did you find this page helpful?