Skip to content

Commit c7a0afe

Browse files
authored
First pass at AST-based codegen (#1333)
* Generate models.py via AST * Update examples with new formatting Currently, we generate code by using Go templates. While this approached has served us well, the code generators themselves are getting more complicated and difficult to understand. Templates aren't fully typed-checked. Functionality is split across Go code and template code. I'm experimenting with a new approach, starting with Python. Instead of using templates, we'll build an AST and then print that AST to a file. I'm aiming to replicate the go/ast and go/printer packages. Lastly, the AST itself is defined via Protocol Buffers so that we can serialize the AST. This will be helpful in the future when we have plugin support.
1 parent b68c669 commit c7a0afe

File tree

13 files changed

+2227
-68
lines changed

13 files changed

+2227
-68
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ psql:
2626

2727
mysqlsh:
2828
mysqlsh --sql --user root --password mysecretpassword --database dinotest 127.0.0.1:3306
29+
30+
31+
# $ protoc --version
32+
# libprotoc 3.17.3
33+
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
34+
proto:
35+
protoc -I ./protos --go_out=. --go_opt=module=github.com/kyleconroy/sqlc ./protos/**/*.proto

examples/python/src/authors/models.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
# Code generated by sqlc. DO NOT EDIT.
2-
from typing import Optional
3-
42
import dataclasses
5-
6-
3+
from typing import Optional
74

85

96
@dataclasses.dataclass()
107
class Author:
118
id: int
129
name: str
1310
bio: Optional[str]
14-
15-

examples/python/src/booktest/models.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Code generated by sqlc. DO NOT EDIT.
2-
from typing import List
2+
import dataclasses
33
import datetime
44
import enum
5-
6-
import dataclasses
7-
5+
from typing import List
86

97

108
class BookType(str, enum.Enum):
@@ -18,7 +16,6 @@ class Author:
1816
name: str
1917

2018

21-
2219
@dataclasses.dataclass()
2320
class Book:
2421
book_id: int
@@ -29,5 +26,3 @@ class Book:
2926
year: int
3027
available: datetime.datetime
3128
tags: List[str]
32-
33-

examples/python/src/booktest/query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

22
# Code generated by sqlc. DO NOT EDIT.
33
from typing import AsyncIterator, List, Optional
4+
import dataclasses
45
import datetime
56

6-
import dataclasses
77
import sqlalchemy
88
import sqlalchemy.ext.asyncio
99

examples/python/src/jets/models.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# Code generated by sqlc. DO NOT EDIT.
2-
3-
42
import dataclasses
53

64

7-
8-
95
@dataclasses.dataclass()
106
class Jet:
117
id: int
@@ -15,24 +11,19 @@ class Jet:
1511
color: str
1612

1713

18-
1914
@dataclasses.dataclass()
2015
class Language:
2116
id: int
2217
language: str
2318

2419

25-
2620
@dataclasses.dataclass()
2721
class Pilot:
2822
id: int
2923
name: str
3024

3125

32-
3326
@dataclasses.dataclass()
3427
class PilotLanguage:
3528
pilot_id: int
3629
language_id: int
37-
38-

examples/python/src/ondeck/models.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# Code generated by sqlc. DO NOT EDIT.
2-
from typing import List, Optional
2+
import dataclasses
33
import datetime
44
import enum
5-
6-
import dataclasses
5+
from typing import List, Optional
76

87

9-
# Venues can be either open or closed
108
class Status(str, enum.Enum):
9+
"""Venues can be either open or closed"""
1110
OPEN = "op!en"
1211
CLOSED = "clo@sed"
1312

@@ -18,9 +17,9 @@ class City:
1817
name: str
1918

2019

21-
# Venues are places where muisc happens
2220
@dataclasses.dataclass()
2321
class Venue:
22+
"""Venues are places where muisc happens"""
2423
id: int
2524
status: Status
2625
statuses: Optional[List[Status]]
@@ -32,5 +31,3 @@ class Venue:
3231
songkick_id: Optional[str]
3332
tags: Optional[List[str]]
3433
created_at: datetime.datetime
35-
36-

examples/python/src/ondeck/venue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

22
# Code generated by sqlc. DO NOT EDIT.
33
from typing import AsyncIterator, List, Optional
4-
54
import dataclasses
5+
66
import sqlalchemy
77
import sqlalchemy.ext.asyncio
88

0 commit comments

Comments
 (0)