31 lines
1.0 KiB
C#
31 lines
1.0 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
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 DeserializerBuilder()
|
|
.WithNamingConvention(UnderscoredNamingConvention.Instance) // maps api_key -> apiKey
|
|
.IgnoreUnmatchedProperties()
|
|
.Build();
|
|
|
|
// Deserialize into a list of Rule
|
|
var rules = deserializer.Deserialize<List<Rule>>(yamlContent);
|
|
|
|
return rules ?? new List<Rule>();
|
|
}
|
|
} |