Files
VDR/VirtualDDNSRouter.Server/Services/YamlParser.cs

31 lines
1.1 KiB
C#

using VirtualDDNSRouter.Server.Context;
using VirtualDDNSRouter.Server.Interfaces;
using VirtualDDNSRouter.Server.Models;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace VirtualDDNSRouter.Server.Services;
public class YamlParser : IYamlParser
{
private readonly string _yamlFilePath = "rules.yaml";
public async Task<List<Rule>> GetRules()
{
if (!File.Exists(_yamlFilePath))
throw new FileNotFoundException($"YAML file not found: {_yamlFilePath}");
var yamlContent = await File.ReadAllTextAsync(_yamlFilePath).ConfigureAwait(false);
// Build the deserializer with explicit naming convention
var deserializer = new StaticDeserializerBuilder(new YamlStaticContextServer())
.WithNamingConvention(UnderscoredNamingConvention.Instance) // maps api_key -> apiKey
.IgnoreUnmatchedProperties()
.Build();
// Deserialize into a list of Rule
var rules = deserializer.Deserialize<List<Rule>>(yamlContent);
return rules;
}
}