-
Notifications
You must be signed in to change notification settings - Fork 0
Support type overrides in addition to column overrides #8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.29.0 | ||
| # sqlc v1.30.0 | ||
| import dataclasses | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| package python | ||
|
|
||
| import ( | ||
| "bytes" | ||
| json "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| type OverrideColumn struct { | ||
| Column string `json:"column"` | ||
| DbType string `json:"db_type"` | ||
| PyType string `json:"py_type"` | ||
| PyImport string `json:"py_import"` | ||
| } | ||
|
|
@@ -19,3 +27,19 @@ type Config struct { | |
| InflectionExcludeTableNames []string `json:"inflection_exclude_table_names"` | ||
| Overrides []OverrideColumn `json:"overrides"` | ||
| } | ||
|
|
||
| func parseConfig(raw []byte) (Config, error) { | ||
| var conf Config | ||
| if len(raw) == 0 { | ||
| return conf, nil | ||
| } | ||
|
|
||
| dec := json.NewDecoder(bytes.NewReader(raw)) | ||
| dec.DisallowUnknownFields() | ||
| if err := dec.Decode(&conf); err != nil { | ||
| msg := strings.TrimPrefix(err.Error(), "json: ") | ||
| return Config{}, fmt.Errorf("invalid plugin options: %s", msg) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a Go question: why return
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the function returns |
||
| } | ||
|
|
||
| return conf, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package python | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParseConfigDisallowUnknownFields(t *testing.T) { | ||
| _, err := parseConfig([]byte(`{"emit_sync_querier":true,"db_typ":"jsonb"}`)) | ||
| if err == nil { | ||
| t.Fatal("expected unknown field error, got nil") | ||
| } | ||
| if !strings.Contains(err.Error(), "invalid plugin options") { | ||
| t.Fatalf("expected error to reference plugin options, got: %v", err) | ||
| } | ||
| if !strings.Contains(err.Error(), `unknown field "db_typ"`) { | ||
| t.Fatalf("expected unknown field in error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestParseConfigValid(t *testing.T) { | ||
| conf, err := parseConfig([]byte(`{"emit_sync_querier":true,"overrides":[{"db_type":"jsonb","py_type":"str"}]}`)) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if !conf.EmitSyncQuerier { | ||
| t.Fatal("expected emit_sync_querier to be true") | ||
| } | ||
| if len(conf.Overrides) != 1 || conf.Overrides[0].DbType != "jsonb" || conf.Overrides[0].PyType != "str" { | ||
| t.Fatal("unexpected parsed overrides") | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.29.0 | ||
| # sqlc v1.30.0 | ||
| import dataclasses | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.29.0 | ||
| # sqlc v1.30.0 | ||
| import pydantic | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.30.0 | ||
| import enum | ||
| import pydantic | ||
| from typing import Optional | ||
|
|
||
|
|
||
| class BookStatus(str, enum.Enum): | ||
| AVAILABLE = "available" | ||
| CHECKED_OUT = "checked_out" | ||
| OVERDUE = "overdue" | ||
|
|
||
|
|
||
| class Book(pydantic.BaseModel): | ||
| model_config = pydantic.ConfigDict( | ||
| validate_by_alias=True, | ||
| validate_by_name=True, | ||
| ) | ||
| id: int | ||
| title: str | ||
| status: Optional[BookStatus] | ||
| payload: my_lib.models.BookPayload | ||
| metadata: Optional[my_lib.models.JsonValue] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@allenap lmk if this is what you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this is cool.
Later on I also want to eliminate the use of
Any, e.g. default toobjectinstead. Having overrides means folks can choose a better type (or chooseAny🤦).