Support type overrides in addition to column overrides#8
Conversation
Also updated tests to the latest sqlc version
| tableName := col.Table.Name | ||
| if col.Table.Schema != "" && col.Table.Schema != req.Catalog.DefaultSchema { | ||
| tableName = col.Table.Schema + "." + tableName | ||
| if len(conf.Overrides) > 0 { |
There was a problem hiding this comment.
core change starts here. append ?w=1 to the URL for easier review
|
|
||
| This will: | ||
| 1. Override the column `payload` in `some_table` to use the type `Payload` | ||
| 2. Override any column with the database type `jsonb` to use the type `Payload` |
There was a problem hiding this comment.
Yep, this is cool.
Later on I also want to eliminate the use of Any, e.g. default to object instead. Having overrides means folks can choose a better type (or choose Any 🤦).
| overrides: | ||
| - column: "books.payload" | ||
| py_import: "my_lib.models" | ||
| py_type: "BookPayload" |
There was a problem hiding this comment.
Does BookPayload need to be a pydantic model too? Or, how does deserialization/serialization happen?
There was a problem hiding this comment.
Depending on whether emit_pydantic_models is true I guess? Would need to test
| dec.DisallowUnknownFields() | ||
| if err := dec.Decode(&conf); err != nil { | ||
| msg := strings.TrimPrefix(err.Error(), "json: ") | ||
| return Config{}, fmt.Errorf("invalid plugin options: %s", msg) |
There was a problem hiding this comment.
This is a Go question: why return Config{} here instead of nothing? A caller could accidentally use an empty configuration.
There was a problem hiding this comment.
Because the function returns Config instead of *Config, we can't return nil here. It's quite idiomatic to do it this way, as the caller is expected to always check err before continuing.
|
|
||
| // Look for a matching override | ||
| // Then look for a matching db_type override | ||
| columnType := strings.ToLower(sdk.DataType(col.Type)) |
There was a problem hiding this comment.
Does this need ToLower since it's being compared with EqualFold later on?
There was a problem hiding this comment.
No, now that you point out.
|
|
||
| This will: | ||
| 1. Override the column `payload` in `some_table` to use the type `Payload` | ||
| 2. Override any column with the database type `jsonb` to use the type `Payload` |
There was a problem hiding this comment.
Yep, this is cool.
Later on I also want to eliminate the use of Any, e.g. default to object instead. Having overrides means folks can choose a better type (or choose Any 🤦).
Support type overrides with test coverage
fail for unknown yaml fields