- Notifications
You must be signed in to change notification settings - Fork39
feat: add emit_str_enum config option#66
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
ac6bf25 feat: add emit_str_enum config option
devsteinb3f2471 Merge branch 'sqlc-dev:main' into main
devstein1f7fd85 docs: add docs for emit_str_enum
devstein8c3af57 tests(emit_str_enum): add end to end test for emit str enum
devsteineef4c98 chore(tests): update wasm sha
devstein397fe5b docs: add examples of with w/o
devstein1684bf5 docs: update to use correct dataclass syntax
devsteinFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
87 changes: 72 additions & 15 deletionsREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,78 @@ | ||
| ## Usage | ||
| ```yaml | ||
| version:"2" | ||
| plugins: | ||
| - name: py | ||
| wasm: | ||
| url: https://downloads.sqlc.dev/plugin/sqlc-gen-python_1.2.0.wasm | ||
| sha256: a6c5d174c407007c3717eea36ff0882744346e6ba991f92f71d6ab2895204c0e | ||
| sql: | ||
| - schema: "schema.sql" | ||
| queries: "query.sql" | ||
| engine: postgresql | ||
| codegen: | ||
| - out: src/authors | ||
| plugin: py | ||
| options: | ||
| package: authors | ||
| emit_sync_querier: true | ||
| emit_async_querier: true | ||
| ``` | ||
| ### Emit Pydantic Models instead of `dataclasses` | ||
| Option: `emit_pydantic_models` | ||
| By default, `sqlc-gen-python` will emit `dataclasses` for the models. If you prefer to use [`pydantic`](https://docs.pydantic.dev/latest/) models, you can enable this option. | ||
| with `emit_pydantic_models` | ||
| ```py | ||
| from pydantic import BaseModel | ||
| class Author(pydantic.BaseModel): | ||
| id: int | ||
| name: str | ||
| ``` | ||
| without `emit_pydantic_models` | ||
| ```py | ||
| import dataclasses | ||
| @dataclasses.dataclass() | ||
| class Author: | ||
| id: int | ||
| name: str | ||
| ``` | ||
| ### Use `enum.StrEnum` for Enums | ||
| Option: `emit_str_enum` | ||
| `enum.StrEnum` was introduce in Python 3.11. | ||
| `enum.StrEnum` is a subclass of `str` that is also a subclass of `Enum`. This allows for the use of `Enum` values as strings, compared to strings, or compared to other `enum.StrEnum` types. | ||
| This is convenient for type checking and validation, as well as for serialization and deserialization. | ||
| By default, `sqlc-gen-python` will emit `(str, enum.Enum)` for the enum classes. If you prefer to use `enum.StrEnum`, you can enable this option. | ||
| with `emit_str_enum` | ||
| ```py | ||
| class Status(enum.StrEnum): | ||
| """Venues can be either open or closed""" | ||
| OPEN = "op!en" | ||
| CLOSED = "clo@sed" | ||
| ``` | ||
| without `emit_str_enum` (current behavior) | ||
| ```py | ||
| class Status(str, enum.Enum): | ||
| """Venues can be either open or closed""" | ||
| OPEN = "op!en" | ||
| CLOSED = "clo@sed" | ||
| ``` |
1 change: 1 addition & 0 deletionsinternal/config.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/emit_pydantic_models/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletionsinternal/endtoend/testdata/emit_str_enum/db/models.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.27.0 | ||
| import dataclasses | ||
| import enum | ||
| from typing import Optional | ||
| class BookStatus(enum.StrEnum): | ||
| AVAILABLE = "available" | ||
| CHECKED_OUT = "checked_out" | ||
| OVERDUE = "overdue" | ||
| @dataclasses.dataclass() | ||
| class Book: | ||
| id: int | ||
| title: str | ||
| status: Optional[BookStatus] |
111 changes: 111 additions & 0 deletionsinternal/endtoend/testdata/emit_str_enum/db/query.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| # Code generated by sqlc. DO NOT EDIT. | ||
| # versions: | ||
| # sqlc v1.27.0 | ||
| # source: query.sql | ||
| from typing import AsyncIterator, Iterator, Optional | ||
| import sqlalchemy | ||
| import sqlalchemy.ext.asyncio | ||
| from db import models | ||
| CREATE_BOOK = """-- name: create_book \\:one | ||
| INSERT INTO books ( | ||
| title, status | ||
| ) VALUES ( | ||
| :p1, :p2 | ||
| ) RETURNING id, title, status | ||
| """ | ||
| DELETE_BOOK = """-- name: delete_book \\:exec | ||
| DELETE FROM books | ||
| WHERE id = :p1 | ||
| """ | ||
| GET_BOOK = """-- name: get_book \\:one | ||
| SELECT id, title, status FROM books | ||
| WHERE id = :p1 LIMIT 1 | ||
| """ | ||
| LIST_BOOKS = """-- name: list_books \\:many | ||
| SELECT id, title, status FROM books | ||
| ORDER BY title | ||
| """ | ||
| class Querier: | ||
| def __init__(self, conn: sqlalchemy.engine.Connection): | ||
| self._conn = conn | ||
| def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]: | ||
| row = self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status}).first() | ||
| if row is None: | ||
| return None | ||
| return models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) | ||
| def delete_book(self, *, id: int) -> None: | ||
| self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id}) | ||
| def get_book(self, *, id: int) -> Optional[models.Book]: | ||
| row = self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id}).first() | ||
| if row is None: | ||
| return None | ||
| return models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) | ||
| def list_books(self) -> Iterator[models.Book]: | ||
| result = self._conn.execute(sqlalchemy.text(LIST_BOOKS)) | ||
| for row in result: | ||
| yield models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) | ||
| class AsyncQuerier: | ||
| def __init__(self, conn: sqlalchemy.ext.asyncio.AsyncConnection): | ||
| self._conn = conn | ||
| async def create_book(self, *, title: str, status: Optional[models.BookStatus]) -> Optional[models.Book]: | ||
| row = (await self._conn.execute(sqlalchemy.text(CREATE_BOOK), {"p1": title, "p2": status})).first() | ||
| if row is None: | ||
| return None | ||
| return models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) | ||
| async def delete_book(self, *, id: int) -> None: | ||
| await self._conn.execute(sqlalchemy.text(DELETE_BOOK), {"p1": id}) | ||
| async def get_book(self, *, id: int) -> Optional[models.Book]: | ||
| row = (await self._conn.execute(sqlalchemy.text(GET_BOOK), {"p1": id})).first() | ||
| if row is None: | ||
| return None | ||
| return models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) | ||
| async def list_books(self) -> AsyncIterator[models.Book]: | ||
| result = await self._conn.stream(sqlalchemy.text(LIST_BOOKS)) | ||
| async for row in result: | ||
| yield models.Book( | ||
| id=row[0], | ||
| title=row[1], | ||
| status=row[2], | ||
| ) |
18 changes: 18 additions & 0 deletionsinternal/endtoend/testdata/emit_str_enum/query.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| -- name: GetBook :one | ||
| SELECT * FROM books | ||
| WHERE id = $1 LIMIT 1; | ||
| -- name: ListBooks :many | ||
| SELECT * FROM books | ||
| ORDER BY title; | ||
| -- name: CreateBook :one | ||
| INSERT INTO books ( | ||
| title, status | ||
| ) VALUES ( | ||
| $1, $2 | ||
| ) RETURNING *; | ||
| -- name: DeleteBook :exec | ||
| DELETE FROM books | ||
| WHERE id = $1; |
8 changes: 8 additions & 0 deletionsinternal/endtoend/testdata/emit_str_enum/schema.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| CREATE TYPE book_status AS ENUM ('available', 'checked_out', 'overdue'); | ||
| CREATE TABLE books ( | ||
| id BIGSERIAL PRIMARY KEY, | ||
| title text NOT NULL, | ||
| status book_status DEFAULT 'available' | ||
| ); |
19 changes: 19 additions & 0 deletionsinternal/endtoend/testdata/emit_str_enum/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| version: "2" | ||
| plugins: | ||
| - name: py | ||
| wasm: | ||
| url: file://../../../../bin/sqlc-gen-python.wasm | ||
| sha256: "d6846ffad948181e611e883cedd2d2be66e091edc1273a0abc6c9da18399e0ca" | ||
| sql: | ||
| - schema: schema.sql | ||
| queries: query.sql | ||
| engine: postgresql | ||
| codegen: | ||
| - plugin: py | ||
| out: db | ||
| options: | ||
| package: db | ||
| emit_sync_querier: true | ||
| emit_async_querier: true | ||
| emit_str_enum: true | ||
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/exec_result/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/exec_rows/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/inflection_exclude_table_names/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/query_parameter_limit_two/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/query_parameter_limit_undefined/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/query_parameter_limit_zero/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletioninternal/endtoend/testdata/query_parameter_no_limit/sqlc.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 12 additions & 5 deletionsinternal/gen.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.