gen icon indicating copy to clipboard operation
gen copied to clipboard

How do I map an enum when using gentool?

Open maguro opened this issue 7 months ago • 1 comments

Your Question

I have a column in my tables that represents an enum. I have the enum defined in my go code, foo.State. How do I get the generated models to use the type foo.State instead of string when using the gentool command?

The document you expected this should be explained

https://gorm.io/gen/gen_tool.html

Expected answer

Something where I can specify a table and column regex and the go enum type to use. Something like fieldTypeMappings. On the command line:

gentool -fieldTypeMappings "table1:.+_state:foo.state,table2:.special_state:foo.state" ...

or, in the gen.yaml file:

database:
  fieldMappings:
    - table: table1
      fields: .+_state
      type: foo.State
    - table: table2
      fields: special_state
      type: foo.State

Reading the code in this project, I think it's doable if I write a custom generator. I would like to avoid doing that.

maguro avatar Dec 03 '23 20:12 maguro