-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
Overriding the name of a type dynamically #914
Comments
Yes, you can do this using the IYamlConvertible interface, using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Serialization;
var data = new Config();
data.Receivers.Add(new Receiver());
data.Receivers.Add(new Receiver());
data.Receivers.Add(new Receiver());
var serializer = new SerializerBuilder().Build();
var yaml = serializer.Serialize(data);
Console.WriteLine(yaml);
public class Config : IYamlConvertible
{
public List<Receiver> Receivers { get; } = [];
public void Read(IParser parser, Type expectedType, ObjectDeserializer nestedObjectDeserializer)
{
}
public void Write(IEmitter emitter, ObjectSerializer nestedObjectSerializer)
{
emitter.Emit(new YamlDotNet.Core.Events.MappingStart());
emitter.Emit(new Scalar("Receivers"));
emitter.Emit(new YamlDotNet.Core.Events.MappingStart());
for (var ordinal = 0; ordinal < Receivers.Count; ordinal++)
{
emitter.Emit(new Scalar($"otlp/{ordinal + 1}"));
nestedObjectSerializer(Receivers[ordinal], typeof(Receiver));
}
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
emitter.Emit(new YamlDotNet.Core.Events.MappingEnd());
}
}
public class Receiver
{
public string ReceiverType = Guid.NewGuid().ToString();
public string ReceiverName = Guid.NewGuid().ToString();
} Results in
|
Since I gave a working example and it's been a few weeks I'm going to close this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm playing around with building a class that will model how the OpenTelemetry Collector's config works.
Specifically, they have a format that takes a list of components, but does them as Objects
What I want to be able to do is represent that as a list in code (since the OTLP and OTLP/2 objects are the same thing.
So what I want to do is "just" override the name part, and the rendering of the List as a objects.
The classes will look something like this:
Is this possible, I've been struggling to make it work.
The text was updated successfully, but these errors were encountered: