ch-robinson / dotnet-avro

An Avro implementation for .NET
https://engineering.chrobinson.com/dotnet-avro/
MIT License
135 stars 51 forks source link

Add default value support to schema model #178

Closed dstelljes closed 2 years ago

dstelljes commented 2 years ago

This change adds a Default property to the RecordField type and implements reading/writing default values from/to JSON schema representations. Serialization remains unchanged for now; #176 is a prerequisite for getting that to work as expected.

To keep the default value implementation agnostic to encoding, the DefaultValue class is abstract and implemented by encoding-specific subclasses. Then, serde builders can call the .ToObject<T> method to get an object without having to worry about how to deserialize the value. There are some downsides to this approach: the big one is that a deserializer builder is tied to a default value, but that doesn’t have any practical impact on performance, and I think it’s an okay tradeoff to make for the simplicity of .ToObject<T>. There’s also a hacky check in JsonSchemaWriter—if a non-JSON default value is passed in, it’ll throw (since .ToObject<dynamic> doesn’t work reliably yet), but that’s something we’ll be able to remove once we have dynamic serdes nailed down.