jsontypedef / json-typedef-codegen

A CLI tool that generates code from JSON Typedef schemas
https://jsontypedef.com/docs/tools/jtd-codegen
MIT License
154 stars 31 forks source link

Feature Request/Discussion: Golang string types for discriminator #49

Open delaneyj opened 2 years ago

delaneyj commented 2 years ago

A definition of

 "foo": {
      "discriminator": "type",
      "mapping": {
        "a": {
          "properties": {}
        },
        "b": {
          "properties": {}
        },
        "c": {
          "properties": {}
        }
      }
    }

Generates a Go struct of

type Foo struct {
    Type string

    A FooA

    B FooB

    C FooC
}

Where it should be more acccurately be the same as an enum and look more like...

type FooTypeDiscriminator string

const (
    FooTypeDiscriminatorA FooTypeDiscriminator = "a"

    FooTypeDiscriminatorB FooTypeDiscriminator = "b"

    FooTypeDiscriminatorC FooTypeDiscriminator = "c"
)

type Foo struct {
    FooTypeDiscriminator string

    A FooA

    B FooB

    C FooC
}

This would improve auto-completion and validation.