Skip to content
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

Permit specifying/overriding field names #73

Open
BatmanAoD opened this issue Mar 30, 2023 · 1 comment
Open

Permit specifying/overriding field names #73

BatmanAoD opened this issue Mar 30, 2023 · 1 comment

Comments

@BatmanAoD
Copy link

I have a handwritten struct that is shared between services in Python, TypeScript, C#, and Go.

The Go version has a field like this (where Date is a custom date type):

    EffectiveDate Date `json:"effective"`

It looks like it's currently impossible to generate a drop-in replacement because the field name doesn't match the JSON field name. Would it be possible to add the equivalent of metadata.<lang>Type but for field-naming? I.e., I'd like the schema to be able to generate the field like this:

    "effective": {
      "metadata": {
        "description": "The date that this transaction takes effect",
        "goField": "EffectiveDate"
      },
      "type": "Date"
    },

Presumably the TypeScript generator wouldn't offer this option for the reasons explained here, but I think it makes sense to offer it for all other supported languages.

@BatmanAoD
Copy link
Author

I have a workaround that seems a bit dangerous, but should be pretty safe:

Assuming the type in question is called MyType, and the generated version lives in a package called generated, the handwritten type can be replaced with a wrapper using MyType as an embedded struct:

type MyType struct {
    generated.MyType
    // EffectiveDate causes 'Effective' to be ignored while marshaling.
    EffectiveDate Date `json:"effective"`
}

// Effective ensures that clients cannot accidentally access MyType.MyType.Effective.
func (mt MyType) Effective() Date {
    return mt.EffectiveDate
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant