Git Product home page Git Product logo

Comments (5)

nicodeslandes avatar nicodeslandes commented on July 3, 2024 1

Sorry for the late reply, I've been sidetracked away from Avro these last few days.
I've got 2 possible suggestions:

  1. Handle the "happy" path only, if the default value is null of a scalar, generate the [DefaultValue] attribute correctly. If we're not in a supported scenario, issue a warning and don't produce anything.
  2. For the unsupported case like the one you highlighted above, still issue a warning, but decorate the property with a new attribute [DefaultSchemaValueJson], which reproduce the default value as it is defined in the originating schema, and is used when producing the schema from the record class.

Option 1 would be good enough for most cases, and would be enough to ensure we have a good story when one team generate a schema from C# code, and another team wants to produce a C# class from that same schema. The 2nd team would be able to used that class without new, potentially incompatible schemas being published to the registry.

Option 2 would even push this further by allowing a producer to handle schemas that don't come from a generated C# class.

from dotnet-avro.

dstelljes avatar dstelljes commented on July 3, 2024

This would be nice to have, but I’m not sure what we’d do when the default value couldn’t be expressed as a constant. For instance, with a (contrived) schema:

{
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {
      "name": "Value",
      "type": [
        "MyRecord",
        "null"
      ],
      "default": {
        "MyRecord": {
          "Value": null
        }
      }
    }
  ]
}

the code generator would try to express that as:

[DefaultValue(new MyRecord { Value = null })]
public class MyRecord
{
    public MyRecord? Value { get; set; }
}

which would not compile.

from dotnet-avro.

dstelljes avatar dstelljes commented on July 3, 2024

I’d want to give some more thought to Option 2 since that would presumably require generated code to depend on Chr.Avro.Json, but Option 1 definitely seems like a good first step. Codegen is still one big switch statement, but if we refactored a bit to match the schema/serde builder case pattern, that’d give us a mechanism to report warnings.

from dotnet-avro.

nicodeslandes avatar nicodeslandes commented on July 3, 2024

Yes I agree. Option 1 seems to me quite valuable even if we take a bit more time fleshing out Option 2.

I don't know whether Option 2 requires any additional dependencies though. Couldn't the default value json be treated as a simple string by the generated code? (and basically ignored until the need to publish a schema arises)

from dotnet-avro.

dstelljes avatar dstelljes commented on July 3, 2024

Yeah, the schema would just be the JSON string, but DefaultSchemaValueJsonAttribute would have to live somewhere.

from dotnet-avro.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.