protoc-gen-sqlmodel 0.1.0__tar.gz → 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- protoc_gen_sqlmodel-0.2.0/LICENSE +21 -0
- protoc_gen_sqlmodel-0.2.0/PKG-INFO +122 -0
- protoc_gen_sqlmodel-0.2.0/README.md +86 -0
- {protoc_gen_sqlmodel-0.1.0 → protoc_gen_sqlmodel-0.2.0}/pyproject.toml +11 -4
- {protoc_gen_sqlmodel-0.1.0 → protoc_gen_sqlmodel-0.2.0}/pyproject.toml.orig +7 -4
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/plugin.py +38 -0
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/proto/sqlmodel_extensions.proto +28 -0
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/proto/sqlmodel_extensions_pb.py +152 -0
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/utils/__init__.py +109 -0
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/utils/field_handlers.py +151 -0
- protoc_gen_sqlmodel-0.2.0/src/protoc_gen_sqlmodel/utils/message_handlers.py +29 -0
- protoc_gen_sqlmodel-0.1.0/PKG-INFO +0 -19
- protoc_gen_sqlmodel-0.1.0/README.md +0 -6
- protoc_gen_sqlmodel-0.1.0/src/protoc_gen_sqlmodel/plugin.py +0 -237
- protoc_gen_sqlmodel-0.1.0/src/protoc_gen_sqlmodel/proto/sqlmodel_extensions.proto +0 -15
- protoc_gen_sqlmodel-0.1.0/src/protoc_gen_sqlmodel/proto/sqlmodel_extensions_pb.py +0 -48
- {protoc_gen_sqlmodel-0.1.0 → protoc_gen_sqlmodel-0.2.0}/src/protoc_gen_sqlmodel/__init__.py +0 -0
- {protoc_gen_sqlmodel-0.1.0 → protoc_gen_sqlmodel-0.2.0}/src/protoc_gen_sqlmodel/proto/__init__.py +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 River Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: protoc-gen-sqlmodel
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: protoc plugin to generate SQLModel classes
|
|
5
|
+
Author: River Studio
|
|
6
|
+
Author-email: River Studio <git@river-studio.net>
|
|
7
|
+
License: MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2026 River Studio
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
|
28
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
29
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
30
|
+
Requires-Dist: logging>=0.4.9.6
|
|
31
|
+
Requires-Dist: protobuf-py>=0.4.0
|
|
32
|
+
Requires-Dist: protoc-gen-py>=0.4.0
|
|
33
|
+
Requires-Dist: sqlmodel>=0.0.42
|
|
34
|
+
Requires-Python: >=3.14
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# protoc-gen-sqlmodel
|
|
38
|
+
A protoc plugin for producing sqlmodel classes.
|
|
39
|
+
Meant to manage multi-language projects with a single source of truth called protobuf.
|
|
40
|
+
Generate SQLModel from your schema and manage your DB with alembic.
|
|
41
|
+
|
|
42
|
+
# Install
|
|
43
|
+
Add to your project:
|
|
44
|
+
```shell
|
|
45
|
+
uv add protoc-gen-sqlmodel
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Then configure like a regular `buf` plugin in a `bug.gen.yaml` file:
|
|
49
|
+
```yml
|
|
50
|
+
version: v2
|
|
51
|
+
inputs:
|
|
52
|
+
- directory: proto
|
|
53
|
+
plugins:
|
|
54
|
+
- local: protoc-gen-sqlmodel
|
|
55
|
+
out: gen
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
# Usage
|
|
59
|
+
Run with `protoc` or (preferred) with `uv run buf-generate`.
|
|
60
|
+
|
|
61
|
+
## Features
|
|
62
|
+
To mark a model as a table, include the following extension in your proto files:
|
|
63
|
+
```proto
|
|
64
|
+
syntax = "proto3";
|
|
65
|
+
|
|
66
|
+
package example.sqlmodel;
|
|
67
|
+
|
|
68
|
+
import "google/protobuf/descriptor.proto";
|
|
69
|
+
|
|
70
|
+
// Marks a protobuf message as a SQLModel table.
|
|
71
|
+
extend google.protobuf.MessageOptions {
|
|
72
|
+
bool table = 50001;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
And mark the message schema as:
|
|
77
|
+
```proto
|
|
78
|
+
syntax = "proto3";
|
|
79
|
+
|
|
80
|
+
package example.models;
|
|
81
|
+
|
|
82
|
+
import "sqlmodel_extensions.proto";
|
|
83
|
+
|
|
84
|
+
message Entity {
|
|
85
|
+
option (example.sqlmodel.table) = true;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The generated SQLModel will be marked as a table and you can use it directly with alembic for DB management.
|
|
90
|
+
|
|
91
|
+
To express field options add the following proto extensions to you fields:
|
|
92
|
+
```proto
|
|
93
|
+
syntax = "proto3";
|
|
94
|
+
|
|
95
|
+
package example.sqlmodel;
|
|
96
|
+
|
|
97
|
+
import "google/protobuf/descriptor.proto";
|
|
98
|
+
|
|
99
|
+
extend google.protobuf.FieldOptions {
|
|
100
|
+
string sa_type = 50002;
|
|
101
|
+
bool primary_key = 50003;
|
|
102
|
+
string sqlmodel_default = 50004;
|
|
103
|
+
string sqlmodel_default_factory = 50005;
|
|
104
|
+
string py_default = 50006;
|
|
105
|
+
bool index = 50007;
|
|
106
|
+
string foreign_key = 50008;
|
|
107
|
+
bool relationship = 50009;
|
|
108
|
+
string back_populates = 50010;
|
|
109
|
+
bool cascade_delete = 50011;
|
|
110
|
+
string on_delete = 50012;
|
|
111
|
+
string passive_deletes = 50013;
|
|
112
|
+
string link_model = 50014;
|
|
113
|
+
string server_default = 50015;
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Then express fields like:
|
|
118
|
+
```proto
|
|
119
|
+
map<string, string> import_metadata = 7 [
|
|
120
|
+
(example.sqlmodel.sa_type) = "sqlmodel.JSON"
|
|
121
|
+
];
|
|
122
|
+
```
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# protoc-gen-sqlmodel
|
|
2
|
+
A protoc plugin for producing sqlmodel classes.
|
|
3
|
+
Meant to manage multi-language projects with a single source of truth called protobuf.
|
|
4
|
+
Generate SQLModel from your schema and manage your DB with alembic.
|
|
5
|
+
|
|
6
|
+
# Install
|
|
7
|
+
Add to your project:
|
|
8
|
+
```shell
|
|
9
|
+
uv add protoc-gen-sqlmodel
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Then configure like a regular `buf` plugin in a `bug.gen.yaml` file:
|
|
13
|
+
```yml
|
|
14
|
+
version: v2
|
|
15
|
+
inputs:
|
|
16
|
+
- directory: proto
|
|
17
|
+
plugins:
|
|
18
|
+
- local: protoc-gen-sqlmodel
|
|
19
|
+
out: gen
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
# Usage
|
|
23
|
+
Run with `protoc` or (preferred) with `uv run buf-generate`.
|
|
24
|
+
|
|
25
|
+
## Features
|
|
26
|
+
To mark a model as a table, include the following extension in your proto files:
|
|
27
|
+
```proto
|
|
28
|
+
syntax = "proto3";
|
|
29
|
+
|
|
30
|
+
package example.sqlmodel;
|
|
31
|
+
|
|
32
|
+
import "google/protobuf/descriptor.proto";
|
|
33
|
+
|
|
34
|
+
// Marks a protobuf message as a SQLModel table.
|
|
35
|
+
extend google.protobuf.MessageOptions {
|
|
36
|
+
bool table = 50001;
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
And mark the message schema as:
|
|
41
|
+
```proto
|
|
42
|
+
syntax = "proto3";
|
|
43
|
+
|
|
44
|
+
package example.models;
|
|
45
|
+
|
|
46
|
+
import "sqlmodel_extensions.proto";
|
|
47
|
+
|
|
48
|
+
message Entity {
|
|
49
|
+
option (example.sqlmodel.table) = true;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The generated SQLModel will be marked as a table and you can use it directly with alembic for DB management.
|
|
54
|
+
|
|
55
|
+
To express field options add the following proto extensions to you fields:
|
|
56
|
+
```proto
|
|
57
|
+
syntax = "proto3";
|
|
58
|
+
|
|
59
|
+
package example.sqlmodel;
|
|
60
|
+
|
|
61
|
+
import "google/protobuf/descriptor.proto";
|
|
62
|
+
|
|
63
|
+
extend google.protobuf.FieldOptions {
|
|
64
|
+
string sa_type = 50002;
|
|
65
|
+
bool primary_key = 50003;
|
|
66
|
+
string sqlmodel_default = 50004;
|
|
67
|
+
string sqlmodel_default_factory = 50005;
|
|
68
|
+
string py_default = 50006;
|
|
69
|
+
bool index = 50007;
|
|
70
|
+
string foreign_key = 50008;
|
|
71
|
+
bool relationship = 50009;
|
|
72
|
+
string back_populates = 50010;
|
|
73
|
+
bool cascade_delete = 50011;
|
|
74
|
+
string on_delete = 50012;
|
|
75
|
+
string passive_deletes = 50013;
|
|
76
|
+
string link_model = 50014;
|
|
77
|
+
string server_default = 50015;
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Then express fields like:
|
|
82
|
+
```proto
|
|
83
|
+
map<string, string> import_metadata = 7 [
|
|
84
|
+
(example.sqlmodel.sa_type) = "sqlmodel.JSON"
|
|
85
|
+
];
|
|
86
|
+
```
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "protoc-gen-sqlmodel"
|
|
3
|
-
version = "0.
|
|
4
|
-
description = "
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "protoc plugin to generate SQLModel classes"
|
|
5
5
|
readme = "README.md"
|
|
6
|
+
classifiers = [
|
|
7
|
+
"License :: OSI Approved :: Apache Software License",
|
|
8
|
+
"Programming Language :: Python :: 3.14",
|
|
9
|
+
]
|
|
6
10
|
requires-python = ">=3.14"
|
|
7
11
|
dependencies = [
|
|
8
12
|
"logging>=0.4.9.6",
|
|
@@ -11,9 +15,12 @@ dependencies = [
|
|
|
11
15
|
"sqlmodel>=0.0.42",
|
|
12
16
|
]
|
|
13
17
|
|
|
18
|
+
[project.license]
|
|
19
|
+
file = "LICENSE"
|
|
20
|
+
|
|
14
21
|
[[project.authors]]
|
|
15
|
-
name = "
|
|
16
|
-
email = "
|
|
22
|
+
name = "River Studio"
|
|
23
|
+
email = "git@river-studio.net"
|
|
17
24
|
|
|
18
25
|
[project.scripts]
|
|
19
26
|
protoc-gen-sqlmodel = "protoc_gen_sqlmodel:main"
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "protoc-gen-sqlmodel"
|
|
3
|
-
version = "0.
|
|
4
|
-
description = "
|
|
3
|
+
version = "0.2.0"
|
|
4
|
+
description = "protoc plugin to generate SQLModel classes"
|
|
5
5
|
readme = "README.md"
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
license = { file = "LICENSE" }
|
|
7
|
+
authors = [{ name = "River Studio", email = "git@river-studio.net" }]
|
|
8
|
+
classifiers = [
|
|
9
|
+
"License :: OSI Approved :: Apache Software License",
|
|
10
|
+
"Programming Language :: Python :: 3.14",
|
|
8
11
|
]
|
|
9
12
|
requires-python = ">=3.14"
|
|
10
13
|
dependencies = [
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
This plugin parses protobuf files into SQLModel classes.
|
|
4
|
+
The plugin system exposes the following structure, used as a base for functional parsing:
|
|
5
|
+
|
|
6
|
+
Schema
|
|
7
|
+
└── DescFile
|
|
8
|
+
├── extensions Sequence[DescExtension]
|
|
9
|
+
├── dependencies Sequence[DescFile]
|
|
10
|
+
├── enums Sequence[DescEnum]
|
|
11
|
+
│ └── values Sequence[DescEnumValue]
|
|
12
|
+
├── messages Sequence[DescMessage]
|
|
13
|
+
│ ├── fields Sequence[DescField]
|
|
14
|
+
│ ├── nested_enums Sequence[DescEnum]
|
|
15
|
+
│ ├── nested_messages Sequence[DescMessage]
|
|
16
|
+
│ └── oneofs Sequence[DescOneof]
|
|
17
|
+
└── Sequence[DescService]
|
|
18
|
+
└── ...
|
|
19
|
+
|
|
20
|
+
Services are skipped at the moment.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
from protobuf.plugin import Schema
|
|
24
|
+
|
|
25
|
+
from protoc_gen_sqlmodel.utils import handle_extension, handle_leaf_enum
|
|
26
|
+
from protoc_gen_sqlmodel.utils.message_handlers import handle_message
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def generate(schema: Schema) -> None:
|
|
30
|
+
for desc in schema.files_to_generate:
|
|
31
|
+
f = schema.generate_file(desc, "_sqlmodel.py")
|
|
32
|
+
f.preamble(desc)
|
|
33
|
+
for ext in desc.extensions:
|
|
34
|
+
handle_extension(ext, f)
|
|
35
|
+
for e in desc.enums:
|
|
36
|
+
handle_leaf_enum(e, f)
|
|
37
|
+
for m in desc.messages:
|
|
38
|
+
handle_message(m, f)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
syntax = "proto3";
|
|
2
|
+
|
|
3
|
+
package example.sqlmodel;
|
|
4
|
+
|
|
5
|
+
import "google/protobuf/descriptor.proto";
|
|
6
|
+
|
|
7
|
+
// Marks a protobuf message as a SQLModel table.
|
|
8
|
+
extend google.protobuf.MessageOptions {
|
|
9
|
+
bool table = 50001;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Overrides the default protobuf-to-SQLModel field conversion.
|
|
13
|
+
extend google.protobuf.FieldOptions {
|
|
14
|
+
string sa_type = 50002;
|
|
15
|
+
bool primary_key = 50003;
|
|
16
|
+
string sqlmodel_default = 50004;
|
|
17
|
+
string sqlmodel_default_factory = 50005;
|
|
18
|
+
string py_default = 50006;
|
|
19
|
+
bool index = 50007;
|
|
20
|
+
string foreign_key = 50008;
|
|
21
|
+
bool relationship = 50009;
|
|
22
|
+
string back_populates = 50010;
|
|
23
|
+
bool cascade_delete = 50011;
|
|
24
|
+
string on_delete = 50012;
|
|
25
|
+
string passive_deletes = 50013;
|
|
26
|
+
string link_model = 50014;
|
|
27
|
+
string server_default = 50015;
|
|
28
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# Generated from sqlmodel_extensions.proto. DO NOT EDIT.
|
|
2
|
+
# Generated by protoc-gen-py v0.4.0 with parameter "".
|
|
3
|
+
# ruff: noqa: PGH004
|
|
4
|
+
# ruff: noqa
|
|
5
|
+
# fmt: off
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
from typing import Final, TYPE_CHECKING
|
|
10
|
+
|
|
11
|
+
from protobuf import Extension
|
|
12
|
+
from protobuf._codegen import file_desc
|
|
13
|
+
from protobuf.wkt import FieldOptions, MessageOptions, descriptor_pb
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from protobuf import DescFile
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
ext_table: Final[Extension[MessageOptions, bool]] = Extension()
|
|
20
|
+
"""
|
|
21
|
+
```proto
|
|
22
|
+
extend google.protobuf.MessageOptions { optional bool table = 50001 [json_name = "[example.sqlmodel.table]"] }
|
|
23
|
+
```
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
ext_sa_type: Final[Extension[FieldOptions, str]] = Extension()
|
|
27
|
+
"""
|
|
28
|
+
```proto
|
|
29
|
+
extend google.protobuf.FieldOptions { optional string sa_type = 50002 [json_name = "[example.sqlmodel.sa_type]"] }
|
|
30
|
+
```
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
ext_primary_key: Final[Extension[FieldOptions, bool]] = Extension()
|
|
34
|
+
"""
|
|
35
|
+
```proto
|
|
36
|
+
extend google.protobuf.FieldOptions { optional bool primary_key = 50003 [json_name = "[example.sqlmodel.primary_key]"] }
|
|
37
|
+
```
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
ext_sqlmodel_default: Final[Extension[FieldOptions, str]] = Extension()
|
|
41
|
+
"""
|
|
42
|
+
```proto
|
|
43
|
+
extend google.protobuf.FieldOptions { optional string sqlmodel_default = 50004 [json_name = "[example.sqlmodel.sqlmodel_default]"] }
|
|
44
|
+
```
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
ext_sqlmodel_default_factory: Final[Extension[FieldOptions, str]] = Extension()
|
|
48
|
+
"""
|
|
49
|
+
```proto
|
|
50
|
+
extend google.protobuf.FieldOptions { optional string sqlmodel_default_factory = 50005 [json_name = "[example.sqlmodel.sqlmodel_default_factory]"] }
|
|
51
|
+
```
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
ext_py_default: Final[Extension[FieldOptions, str]] = Extension()
|
|
55
|
+
"""
|
|
56
|
+
```proto
|
|
57
|
+
extend google.protobuf.FieldOptions { optional string py_default = 50006 [json_name = "[example.sqlmodel.py_default]"] }
|
|
58
|
+
```
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
ext_index: Final[Extension[FieldOptions, bool]] = Extension()
|
|
62
|
+
"""
|
|
63
|
+
```proto
|
|
64
|
+
extend google.protobuf.FieldOptions { optional bool index = 50007 [json_name = "[example.sqlmodel.index]"] }
|
|
65
|
+
```
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
ext_foreign_key: Final[Extension[FieldOptions, str]] = Extension()
|
|
69
|
+
"""
|
|
70
|
+
```proto
|
|
71
|
+
extend google.protobuf.FieldOptions { optional string foreign_key = 50008 [json_name = "[example.sqlmodel.foreign_key]"] }
|
|
72
|
+
```
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
ext_relationship: Final[Extension[FieldOptions, bool]] = Extension()
|
|
76
|
+
"""
|
|
77
|
+
```proto
|
|
78
|
+
extend google.protobuf.FieldOptions { optional bool relationship = 50009 [json_name = "[example.sqlmodel.relationship]"] }
|
|
79
|
+
```
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
ext_back_populates: Final[Extension[FieldOptions, str]] = Extension()
|
|
83
|
+
"""
|
|
84
|
+
```proto
|
|
85
|
+
extend google.protobuf.FieldOptions { optional string back_populates = 50010 [json_name = "[example.sqlmodel.back_populates]"] }
|
|
86
|
+
```
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
ext_cascade_delete: Final[Extension[FieldOptions, bool]] = Extension()
|
|
90
|
+
"""
|
|
91
|
+
```proto
|
|
92
|
+
extend google.protobuf.FieldOptions { optional bool cascade_delete = 50011 [json_name = "[example.sqlmodel.cascade_delete]"] }
|
|
93
|
+
```
|
|
94
|
+
"""
|
|
95
|
+
|
|
96
|
+
ext_on_delete: Final[Extension[FieldOptions, str]] = Extension()
|
|
97
|
+
"""
|
|
98
|
+
```proto
|
|
99
|
+
extend google.protobuf.FieldOptions { optional string on_delete = 50012 [json_name = "[example.sqlmodel.on_delete]"] }
|
|
100
|
+
```
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
ext_passive_deletes: Final[Extension[FieldOptions, str]] = Extension()
|
|
104
|
+
"""
|
|
105
|
+
```proto
|
|
106
|
+
extend google.protobuf.FieldOptions { optional string passive_deletes = 50013 [json_name = "[example.sqlmodel.passive_deletes]"] }
|
|
107
|
+
```
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
ext_link_model: Final[Extension[FieldOptions, str]] = Extension()
|
|
111
|
+
"""
|
|
112
|
+
```proto
|
|
113
|
+
extend google.protobuf.FieldOptions { optional string link_model = 50014 [json_name = "[example.sqlmodel.link_model]"] }
|
|
114
|
+
```
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
ext_server_default: Final[Extension[FieldOptions, str]] = Extension()
|
|
118
|
+
"""
|
|
119
|
+
```proto
|
|
120
|
+
extend google.protobuf.FieldOptions { optional string server_default = 50015 [json_name = "[example.sqlmodel.server_default]"] }
|
|
121
|
+
```
|
|
122
|
+
"""
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
_DESC = file_desc(
|
|
126
|
+
b'\n\x19sqlmodel_extensions.proto\x12\x10example.sqlmodel\x1a google/protobuf/descriptor.proto:7\n\x05table\x18\xd1\x86\x03 \x01(\x08\x12\x1f.google.protobuf.MessageOptionsR\x05table:8\n\x07sa_type\x18\xd2\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x06saType:@\n\x0bprimary_key\x18\xd3\x86\x03 \x01(\x08\x12\x1d.google.protobuf.FieldOptionsR\nprimaryKey:J\n\x10sqlmodel_default\x18\xd4\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x0fsqlmodelDefault:Y\n\x18sqlmodel_default_factory\x18\xd5\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x16sqlmodelDefaultFactory:>\n\npy_default\x18\xd6\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\tpyDefault:5\n\x05index\x18\xd7\x86\x03 \x01(\x08\x12\x1d.google.protobuf.FieldOptionsR\x05index:@\n\x0bforeign_key\x18\xd8\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\nforeignKey:C\n\x0crelationship\x18\xd9\x86\x03 \x01(\x08\x12\x1d.google.protobuf.FieldOptionsR\x0crelationship:F\n\x0eback_populates\x18\xda\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\rbackPopulates:F\n\x0ecascade_delete\x18\xdb\x86\x03 \x01(\x08\x12\x1d.google.protobuf.FieldOptionsR\rcascadeDelete:<\n\ton_delete\x18\xdc\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x08onDelete:H\n\x0fpassive_deletes\x18\xdd\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x0epassiveDeletes:>\n\nlink_model\x18\xde\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\tlinkModel:F\n\x0eserver_default\x18\xdf\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\rserverDefaultb\x06proto3',
|
|
127
|
+
[
|
|
128
|
+
descriptor_pb.desc(),
|
|
129
|
+
],
|
|
130
|
+
{
|
|
131
|
+
"table": ext_table,
|
|
132
|
+
"sa_type": ext_sa_type,
|
|
133
|
+
"primary_key": ext_primary_key,
|
|
134
|
+
"sqlmodel_default": ext_sqlmodel_default,
|
|
135
|
+
"sqlmodel_default_factory": ext_sqlmodel_default_factory,
|
|
136
|
+
"py_default": ext_py_default,
|
|
137
|
+
"index": ext_index,
|
|
138
|
+
"foreign_key": ext_foreign_key,
|
|
139
|
+
"relationship": ext_relationship,
|
|
140
|
+
"back_populates": ext_back_populates,
|
|
141
|
+
"cascade_delete": ext_cascade_delete,
|
|
142
|
+
"on_delete": ext_on_delete,
|
|
143
|
+
"passive_deletes": ext_passive_deletes,
|
|
144
|
+
"link_model": ext_link_model,
|
|
145
|
+
"server_default": ext_server_default,
|
|
146
|
+
},
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def desc() -> DescFile:
|
|
151
|
+
"""Returns the descriptor for the file `sqlmodel_extensions.proto`."""
|
|
152
|
+
return _DESC
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
from protobuf import (
|
|
2
|
+
DescEnum,
|
|
3
|
+
DescExtension,
|
|
4
|
+
DescFieldValueScalar,
|
|
5
|
+
DescMessage,
|
|
6
|
+
ScalarType,
|
|
7
|
+
)
|
|
8
|
+
from protobuf._descriptors import SupportedFieldPresence
|
|
9
|
+
from protobuf.plugin import File, Module
|
|
10
|
+
|
|
11
|
+
ENUM = Module("enum").ident("Enum")
|
|
12
|
+
FINAL = Module("typing").ident("Final")
|
|
13
|
+
EXTENSION = Module("protobuf").ident("Extension")
|
|
14
|
+
FIELD_OPT = Module("protobuf.wkt").ident("FieldOptions")
|
|
15
|
+
MESSAGE_OPT = Module("protobuf.wkt").ident("MessageOptions")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def get_presence(presence: SupportedFieldPresence) -> str:
|
|
19
|
+
match presence:
|
|
20
|
+
case SupportedFieldPresence.EXPLICIT:
|
|
21
|
+
return " | None "
|
|
22
|
+
case SupportedFieldPresence.IMPLICIT:
|
|
23
|
+
return ""
|
|
24
|
+
case _:
|
|
25
|
+
return ""
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_python_scalar_type(scalar: ScalarType):
|
|
29
|
+
match scalar:
|
|
30
|
+
case ScalarType.BOOL:
|
|
31
|
+
return "bool"
|
|
32
|
+
case ScalarType.BYTES:
|
|
33
|
+
return "bytes"
|
|
34
|
+
case (
|
|
35
|
+
ScalarType.DOUBLE
|
|
36
|
+
| ScalarType.FIXED32
|
|
37
|
+
| ScalarType.FIXED64
|
|
38
|
+
| ScalarType.FLOAT
|
|
39
|
+
| ScalarType.SFIXED32
|
|
40
|
+
| ScalarType.SFIXED64
|
|
41
|
+
):
|
|
42
|
+
return "float"
|
|
43
|
+
case (
|
|
44
|
+
ScalarType.INT32
|
|
45
|
+
| ScalarType.INT64
|
|
46
|
+
| ScalarType.UINT32
|
|
47
|
+
| ScalarType.UINT64
|
|
48
|
+
| ScalarType.SINT32
|
|
49
|
+
| ScalarType.SINT64
|
|
50
|
+
):
|
|
51
|
+
return "int"
|
|
52
|
+
case ScalarType.STRING:
|
|
53
|
+
return "str"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def handle_leaf_enum(desc: DescEnum, f: File):
|
|
57
|
+
with f.scope(f"class {desc.name}(", ENUM, ", int):"):
|
|
58
|
+
for v in desc.values:
|
|
59
|
+
f.print(f"{v.local_name} = {v.number}")
|
|
60
|
+
f.print()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def handle_import_desc(element: DescEnum | DescMessage):
|
|
64
|
+
if element.file.proto.package == "google.protobuf": # well known types
|
|
65
|
+
match element.name:
|
|
66
|
+
case "Timestamp":
|
|
67
|
+
return Module("datetime").ident("datetime")
|
|
68
|
+
case "Any":
|
|
69
|
+
return Module("typing").ident("Any")
|
|
70
|
+
# TODO - complete types registery
|
|
71
|
+
return Module.for_desc(element.file, "_sqlmodel").ident(element.name)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def handle_python_ref_str(type_name: str):
|
|
75
|
+
match type_name:
|
|
76
|
+
case "true" | "True":
|
|
77
|
+
return "True"
|
|
78
|
+
case "false" | "False":
|
|
79
|
+
return "False"
|
|
80
|
+
case s if "." in s:
|
|
81
|
+
parts = s.split(".")
|
|
82
|
+
object_name = parts.pop(-1)
|
|
83
|
+
object_package = ".".join(parts)
|
|
84
|
+
return Module(object_package).ident(object_name)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def handle_extension(desc: DescExtension, f: File):
|
|
88
|
+
match desc.extendee.type_name:
|
|
89
|
+
case "google.protobuf.MessageOptions":
|
|
90
|
+
ext = MESSAGE_OPT
|
|
91
|
+
case "google.protobuf.FieldOptions":
|
|
92
|
+
ext = FIELD_OPT
|
|
93
|
+
match desc.value:
|
|
94
|
+
case DescFieldValueScalar(scalar, _, _):
|
|
95
|
+
ext_type = get_python_scalar_type(scalar)
|
|
96
|
+
|
|
97
|
+
f.print(
|
|
98
|
+
f"ext_{desc.name}: ",
|
|
99
|
+
FINAL,
|
|
100
|
+
"[",
|
|
101
|
+
EXTENSION,
|
|
102
|
+
"[",
|
|
103
|
+
ext,
|
|
104
|
+
", ",
|
|
105
|
+
ext_type,
|
|
106
|
+
"]] = ",
|
|
107
|
+
EXTENSION,
|
|
108
|
+
"()",
|
|
109
|
+
)
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from protobuf import (
|
|
4
|
+
DescEnum,
|
|
5
|
+
DescField,
|
|
6
|
+
DescFieldValueEnum,
|
|
7
|
+
DescFieldValueList,
|
|
8
|
+
DescFieldValueMap,
|
|
9
|
+
DescFieldValueMessage,
|
|
10
|
+
DescFieldValueScalar,
|
|
11
|
+
DescMessage,
|
|
12
|
+
DescOneof,
|
|
13
|
+
ScalarType,
|
|
14
|
+
)
|
|
15
|
+
from protobuf.plugin import File, Module
|
|
16
|
+
|
|
17
|
+
from protoc_gen_sqlmodel.proto.sqlmodel_extensions_pb import (
|
|
18
|
+
ext_back_populates,
|
|
19
|
+
ext_cascade_delete,
|
|
20
|
+
ext_foreign_key,
|
|
21
|
+
ext_index,
|
|
22
|
+
ext_link_model,
|
|
23
|
+
ext_on_delete,
|
|
24
|
+
ext_passive_deletes,
|
|
25
|
+
ext_primary_key,
|
|
26
|
+
ext_py_default,
|
|
27
|
+
ext_relationship,
|
|
28
|
+
ext_sa_type,
|
|
29
|
+
ext_server_default,
|
|
30
|
+
ext_sqlmodel_default,
|
|
31
|
+
ext_sqlmodel_default_factory,
|
|
32
|
+
)
|
|
33
|
+
from protoc_gen_sqlmodel.utils import (
|
|
34
|
+
get_presence,
|
|
35
|
+
get_python_scalar_type,
|
|
36
|
+
handle_import_desc,
|
|
37
|
+
handle_python_ref_str,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
FIELD = Module("sqlmodel").ident("Field")
|
|
41
|
+
RELATIONSHIP = Module("sqlmodel").ident("Relationship")
|
|
42
|
+
|
|
43
|
+
FIELD_EXTENSIONS = {
|
|
44
|
+
ext_back_populates: "back_populates",
|
|
45
|
+
ext_cascade_delete: "cascade_delete",
|
|
46
|
+
ext_foreign_key: "foreign_key",
|
|
47
|
+
ext_index: "index",
|
|
48
|
+
ext_link_model: "link_model",
|
|
49
|
+
ext_on_delete: "on_delete",
|
|
50
|
+
ext_passive_deletes: "passive_deletes",
|
|
51
|
+
ext_primary_key: "primary_key",
|
|
52
|
+
ext_py_default: "py_default",
|
|
53
|
+
ext_sa_type: "sa_type",
|
|
54
|
+
ext_server_default: "server_default",
|
|
55
|
+
ext_sqlmodel_default: "sqlmodel_default",
|
|
56
|
+
ext_sqlmodel_default_factory: "sqlmodel_default_factory",
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def handle_field_extensions(desc: DescField, default_value: Any | None):
|
|
61
|
+
extension_components = []
|
|
62
|
+
options = False
|
|
63
|
+
|
|
64
|
+
if opts := desc.proto.options:
|
|
65
|
+
if ext_relationship in opts:
|
|
66
|
+
extension_components.append(RELATIONSHIP)
|
|
67
|
+
else:
|
|
68
|
+
extension_components.append(FIELD)
|
|
69
|
+
|
|
70
|
+
extension_components.append("(")
|
|
71
|
+
|
|
72
|
+
field_options = []
|
|
73
|
+
if default_value:
|
|
74
|
+
field_options.append(f"default={default_value}")
|
|
75
|
+
options = True
|
|
76
|
+
|
|
77
|
+
for ext, field_name in FIELD_EXTENSIONS.items():
|
|
78
|
+
if ext in opts:
|
|
79
|
+
field_options.append(f"{field_name}=")
|
|
80
|
+
field_options.append(handle_python_ref_str(str(opts[ext])))
|
|
81
|
+
field_options.append(", ")
|
|
82
|
+
options = True
|
|
83
|
+
extension_components.extend(field_options)
|
|
84
|
+
extension_components.append(")")
|
|
85
|
+
|
|
86
|
+
if options:
|
|
87
|
+
return extension_components
|
|
88
|
+
return []
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def handle_leaf_field(desc: DescField, f: File):
|
|
92
|
+
field_name, field_presence = desc.name, get_presence(desc.presence)
|
|
93
|
+
field_preamble = f"{field_name}: "
|
|
94
|
+
print_components = [field_preamble]
|
|
95
|
+
field_default = None
|
|
96
|
+
|
|
97
|
+
match desc.value:
|
|
98
|
+
case DescFieldValueScalar(scalar, default_value, _):
|
|
99
|
+
print_components.append(get_python_scalar_type(scalar))
|
|
100
|
+
print_components.append(field_presence)
|
|
101
|
+
if default_value or field_presence:
|
|
102
|
+
field_default = default_value
|
|
103
|
+
case DescFieldValueMessage(message, _, _):
|
|
104
|
+
print_components.extend([handle_import_desc(message), field_presence])
|
|
105
|
+
case DescFieldValueEnum(enum, default_value, _):
|
|
106
|
+
print_components.append(handle_import_desc(enum))
|
|
107
|
+
print_components.append(field_presence)
|
|
108
|
+
if default_value or field_presence:
|
|
109
|
+
field_default = default_value
|
|
110
|
+
case DescFieldValueMap(key, value):
|
|
111
|
+
print_components.append(f"dict[{get_python_scalar_type(key)}, ")
|
|
112
|
+
match value:
|
|
113
|
+
case ScalarType():
|
|
114
|
+
print_components.append(f"{get_python_scalar_type(value)}]")
|
|
115
|
+
case DescMessage() | DescEnum():
|
|
116
|
+
print_components.extend([handle_import_desc(value), "]"])
|
|
117
|
+
print_components.append(field_presence)
|
|
118
|
+
case DescFieldValueList(element, _, _):
|
|
119
|
+
print_components.append("list[")
|
|
120
|
+
match element:
|
|
121
|
+
case ScalarType():
|
|
122
|
+
print_components.append(f"{get_python_scalar_type(element)}]")
|
|
123
|
+
case DescEnum() | DescMessage():
|
|
124
|
+
print_components.extend(
|
|
125
|
+
[
|
|
126
|
+
handle_import_desc(element),
|
|
127
|
+
"]",
|
|
128
|
+
field_presence,
|
|
129
|
+
] # is this a bug? shouldn't field_presence be added to the scalar case too?
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
if opts := handle_field_extensions(desc, field_default):
|
|
133
|
+
print_components.append(" = ")
|
|
134
|
+
print_components.extend(opts)
|
|
135
|
+
|
|
136
|
+
f.print(*print_components)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def handle_leaf_oneof(desc: DescOneof, f: File):
|
|
140
|
+
f.print(f"# oneof {desc.local_name}")
|
|
141
|
+
distinguisher = f"which_{desc.local_name}: "
|
|
142
|
+
f.print(
|
|
143
|
+
distinguisher,
|
|
144
|
+
Module("typing").ident("Literal"),
|
|
145
|
+
"['",
|
|
146
|
+
"', '".join([field.local_name for field in desc.fields]),
|
|
147
|
+
"']",
|
|
148
|
+
)
|
|
149
|
+
for field in desc.fields:
|
|
150
|
+
handle_leaf_field(field, f)
|
|
151
|
+
f.print()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from protobuf import DescMessage
|
|
2
|
+
from protobuf.plugin import File, Module
|
|
3
|
+
|
|
4
|
+
from protoc_gen_sqlmodel.proto.sqlmodel_extensions_pb import ext_table
|
|
5
|
+
from protoc_gen_sqlmodel.utils import handle_leaf_enum
|
|
6
|
+
from protoc_gen_sqlmodel.utils.field_handlers import (
|
|
7
|
+
handle_leaf_field,
|
|
8
|
+
handle_leaf_oneof,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
SQLMODEL = Module("sqlmodel").ident("SQLModel")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def handle_message(desc: DescMessage, f: File, root: bool = True):
|
|
15
|
+
class_suffix = "):"
|
|
16
|
+
if opts := desc.proto.options:
|
|
17
|
+
class_suffix = ", table=True):" if ext_table in opts else "):"
|
|
18
|
+
with f.scope(f"class {desc.name}(", SQLMODEL, class_suffix):
|
|
19
|
+
for nm in desc.nested_enums:
|
|
20
|
+
handle_leaf_enum(nm, f)
|
|
21
|
+
for nm in desc.nested_messages:
|
|
22
|
+
handle_message(nm, f, False)
|
|
23
|
+
for field in desc.fields:
|
|
24
|
+
handle_leaf_field(field, f)
|
|
25
|
+
for oneof in desc.oneofs:
|
|
26
|
+
handle_leaf_oneof(oneof, f)
|
|
27
|
+
f.print()
|
|
28
|
+
if root:
|
|
29
|
+
f.print()
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: protoc-gen-sqlmodel
|
|
3
|
-
Version: 0.1.0
|
|
4
|
-
Summary: Add your description here
|
|
5
|
-
Author: river-studio-main
|
|
6
|
-
Author-email: river-studio-main <62842368+river-studio-main@users.noreply.github.com>
|
|
7
|
-
Requires-Dist: logging>=0.4.9.6
|
|
8
|
-
Requires-Dist: protobuf-py>=0.4.0
|
|
9
|
-
Requires-Dist: protoc-gen-py>=0.4.0
|
|
10
|
-
Requires-Dist: sqlmodel>=0.0.42
|
|
11
|
-
Requires-Python: >=3.14
|
|
12
|
-
Description-Content-Type: text/markdown
|
|
13
|
-
|
|
14
|
-
# protoc-gen-sqlmodel
|
|
15
|
-
A protoc plugin for producing sqlmodel classes
|
|
16
|
-
Run with:
|
|
17
|
-
```shell
|
|
18
|
-
uv run protoc input.proto --plugin=protoc-gen-custom-plugin=./plugin.py --custom-plugin_out=.
|
|
19
|
-
```
|
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
This plugin parses protobuf files into SQLModel classes.
|
|
4
|
-
The plugin system exposes the following structure, used as a base for functional parsing:
|
|
5
|
-
|
|
6
|
-
Schema
|
|
7
|
-
└── DescFile
|
|
8
|
-
├── extensions Sequence[DescExtension]
|
|
9
|
-
├── dependencies Sequence[DescFile]
|
|
10
|
-
├── enums Sequence[DescEnum]
|
|
11
|
-
│ └── values Sequence[DescEnumValue]
|
|
12
|
-
├── messages Sequence[DescMessage]
|
|
13
|
-
│ ├── fields Sequence[DescField]
|
|
14
|
-
│ ├── nested_enums Sequence[DescEnum]
|
|
15
|
-
│ ├── nested_messages Sequence[DescMessage]
|
|
16
|
-
│ └── oneofs Sequence[DescOneof]
|
|
17
|
-
└── Sequence[DescService]
|
|
18
|
-
└── ...
|
|
19
|
-
|
|
20
|
-
Services are skipped at the moment.
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
from logging import getLogger
|
|
24
|
-
|
|
25
|
-
from protobuf import (
|
|
26
|
-
DescEnum,
|
|
27
|
-
DescExtension,
|
|
28
|
-
DescField,
|
|
29
|
-
DescFieldValueEnum,
|
|
30
|
-
DescFieldValueList,
|
|
31
|
-
DescFieldValueMap,
|
|
32
|
-
DescFieldValueMessage,
|
|
33
|
-
DescFieldValueScalar,
|
|
34
|
-
DescMessage,
|
|
35
|
-
DescOneof,
|
|
36
|
-
ScalarType,
|
|
37
|
-
)
|
|
38
|
-
from protobuf._descriptors import SupportedFieldPresence
|
|
39
|
-
from protobuf.plugin import File, Module, Schema
|
|
40
|
-
|
|
41
|
-
ENUM = Module("enum").ident("Enum")
|
|
42
|
-
FIELD = Module("sqlmodel").ident("Field")
|
|
43
|
-
SQLMODEL = Module("sqlmodel").ident("SQLModel")
|
|
44
|
-
ANY = Module("typing").ident("Any")
|
|
45
|
-
FINAL = Module("typing").ident("Final")
|
|
46
|
-
EXTENSION = Module("protobuf").ident("Extension")
|
|
47
|
-
FIELD_OPT = Module("protobuf.wkt").ident("FieldOptions")
|
|
48
|
-
MESSAGE_OPT = Module("protobuf.wkt").ident("MessageOptions")
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
logger = getLogger()
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
def get_presence(presence: SupportedFieldPresence) -> str:
|
|
55
|
-
match presence:
|
|
56
|
-
case SupportedFieldPresence.EXPLICIT:
|
|
57
|
-
return " | None "
|
|
58
|
-
case SupportedFieldPresence.IMPLICIT:
|
|
59
|
-
return ""
|
|
60
|
-
case _:
|
|
61
|
-
return ""
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def get_python_scalar_type(scalar: ScalarType):
|
|
65
|
-
match scalar:
|
|
66
|
-
case ScalarType.BOOL:
|
|
67
|
-
return "bool"
|
|
68
|
-
case ScalarType.BYTES:
|
|
69
|
-
return "bytes"
|
|
70
|
-
case (
|
|
71
|
-
ScalarType.DOUBLE
|
|
72
|
-
| ScalarType.FIXED32
|
|
73
|
-
| ScalarType.FIXED64
|
|
74
|
-
| ScalarType.FLOAT
|
|
75
|
-
| ScalarType.SFIXED32
|
|
76
|
-
| ScalarType.SFIXED64
|
|
77
|
-
):
|
|
78
|
-
return "float"
|
|
79
|
-
case (
|
|
80
|
-
ScalarType.INT32
|
|
81
|
-
| ScalarType.INT64
|
|
82
|
-
| ScalarType.UINT32
|
|
83
|
-
| ScalarType.UINT64
|
|
84
|
-
| ScalarType.SINT32
|
|
85
|
-
| ScalarType.SINT64
|
|
86
|
-
):
|
|
87
|
-
return "int"
|
|
88
|
-
case ScalarType.STRING:
|
|
89
|
-
return "str"
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
def handle_leaf_enum(desc: DescEnum, f: File):
|
|
93
|
-
with f.scope(f"class {desc.name}(", ENUM, ", int):"):
|
|
94
|
-
for v in desc.values:
|
|
95
|
-
f.print(f"{v.local_name} = {v.number}")
|
|
96
|
-
f.print()
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def handle_leaf_oneof(desc: DescOneof, f: File):
|
|
100
|
-
f.print(f"# oneof {desc.local_name}")
|
|
101
|
-
distinguisher = f"which_{desc.local_name}: "
|
|
102
|
-
f.print(
|
|
103
|
-
distinguisher,
|
|
104
|
-
Module("typing").ident("Literal"),
|
|
105
|
-
"['",
|
|
106
|
-
"', '".join([field.local_name for field in desc.fields]),
|
|
107
|
-
"']",
|
|
108
|
-
)
|
|
109
|
-
for field in desc.fields:
|
|
110
|
-
handle_leaf_field(field, f)
|
|
111
|
-
f.print()
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def handle_import(element: DescEnum | DescMessage):
|
|
115
|
-
if element.file.proto.package == "google.protobuf": # well known types
|
|
116
|
-
match element.name:
|
|
117
|
-
case "Timestamp":
|
|
118
|
-
return Module("datetime").ident("datetime")
|
|
119
|
-
case "Any":
|
|
120
|
-
return Module("typing").ident("Any")
|
|
121
|
-
return Module.for_desc(element.file, "_sqlmodel").ident(element.name)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
def handle_leaf_field(desc: DescField, f: File):
|
|
125
|
-
field_name, field_presence = desc.name, get_presence(desc.presence)
|
|
126
|
-
field_preamble, field_presence_preamble = f"{field_name}: ", f"{field_presence}= "
|
|
127
|
-
print_components = [field_preamble]
|
|
128
|
-
|
|
129
|
-
match desc.value:
|
|
130
|
-
case DescFieldValueScalar(scalar, default_value, _):
|
|
131
|
-
print_components.append(get_python_scalar_type(scalar))
|
|
132
|
-
if default_value:
|
|
133
|
-
print_components.extend(
|
|
134
|
-
[field_presence_preamble, FIELD, f"(default={default_value})"]
|
|
135
|
-
)
|
|
136
|
-
else:
|
|
137
|
-
print_components.append(field_presence)
|
|
138
|
-
case DescFieldValueMessage(message, _, _):
|
|
139
|
-
print_components.extend([handle_import(message), field_presence])
|
|
140
|
-
case DescFieldValueEnum(enum, default_value, _):
|
|
141
|
-
print_components.append(handle_import(enum))
|
|
142
|
-
if default_value is not None: # can be 0
|
|
143
|
-
print_components.extend(
|
|
144
|
-
[field_presence_preamble, FIELD, f"(default={default_value})"]
|
|
145
|
-
)
|
|
146
|
-
else:
|
|
147
|
-
print_components.append(field_presence)
|
|
148
|
-
case DescFieldValueMap(key, value):
|
|
149
|
-
print_components.append(f"dict[{get_python_scalar_type(key)}, ")
|
|
150
|
-
match value:
|
|
151
|
-
case ScalarType():
|
|
152
|
-
print_components.append(f"{get_python_scalar_type(value)}]")
|
|
153
|
-
case DescMessage() | DescEnum():
|
|
154
|
-
print_components.extend([handle_import(value), "]"])
|
|
155
|
-
print_components.append(field_presence)
|
|
156
|
-
case DescFieldValueList(element, _, _):
|
|
157
|
-
print_components.append("list[")
|
|
158
|
-
match element:
|
|
159
|
-
case ScalarType():
|
|
160
|
-
print_components.append(f"{get_python_scalar_type(element)}]")
|
|
161
|
-
case DescEnum() | DescMessage():
|
|
162
|
-
print_components.extend(
|
|
163
|
-
[
|
|
164
|
-
handle_import(element),
|
|
165
|
-
"]",
|
|
166
|
-
field_presence,
|
|
167
|
-
] # is this a bug? shouldn't field_presence be added to the scalar case too?
|
|
168
|
-
)
|
|
169
|
-
# if opts := desc.proto.options:
|
|
170
|
-
# from .sqlmodel_extensions_pb import ext_sqlmodel_type
|
|
171
|
-
|
|
172
|
-
# if ext_sqlmodel_type in opts:
|
|
173
|
-
# print_components.extend(
|
|
174
|
-
# [
|
|
175
|
-
# field_presence_preamble,
|
|
176
|
-
# FIELD,
|
|
177
|
-
# f"(sa_column={handle_import(opts[ext_sqlmodel_type])})",
|
|
178
|
-
# ]
|
|
179
|
-
# )
|
|
180
|
-
f.print(*print_components)
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
def handle_message(desc: DescMessage, f: File, root: bool = True):
|
|
184
|
-
class_suffix = "):"
|
|
185
|
-
if opts := desc.proto.options:
|
|
186
|
-
from .proto.sqlmodel_extensions_pb import ext_table
|
|
187
|
-
|
|
188
|
-
class_suffix = ", table=True):" if ext_table in opts else "):"
|
|
189
|
-
with f.scope(f"class {desc.name}(", SQLMODEL, class_suffix):
|
|
190
|
-
for nm in desc.nested_enums:
|
|
191
|
-
handle_leaf_enum(nm, f)
|
|
192
|
-
for nm in desc.nested_messages:
|
|
193
|
-
handle_message(nm, f, False)
|
|
194
|
-
for field in desc.fields:
|
|
195
|
-
handle_leaf_field(field, f)
|
|
196
|
-
for oneof in desc.oneofs:
|
|
197
|
-
handle_leaf_oneof(oneof, f)
|
|
198
|
-
f.print()
|
|
199
|
-
if root:
|
|
200
|
-
f.print()
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
def handle_extension(desc: DescExtension, f: File):
|
|
204
|
-
match desc.extendee.type_name:
|
|
205
|
-
case "google.protobuf.MessageOptions":
|
|
206
|
-
ext = MESSAGE_OPT
|
|
207
|
-
case "google.protobuf.FieldOptions":
|
|
208
|
-
ext = FIELD_OPT
|
|
209
|
-
match desc.value:
|
|
210
|
-
case DescFieldValueScalar(scalar, _, _):
|
|
211
|
-
ext_type = get_python_scalar_type(scalar)
|
|
212
|
-
|
|
213
|
-
f.print(
|
|
214
|
-
f"ext_{desc.name}: ",
|
|
215
|
-
FINAL,
|
|
216
|
-
"[",
|
|
217
|
-
EXTENSION,
|
|
218
|
-
"[",
|
|
219
|
-
ext,
|
|
220
|
-
", ",
|
|
221
|
-
ext_type,
|
|
222
|
-
"]] = ",
|
|
223
|
-
EXTENSION,
|
|
224
|
-
"()",
|
|
225
|
-
)
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
def generate(schema: Schema) -> None:
|
|
229
|
-
for desc in schema.files_to_generate:
|
|
230
|
-
f = schema.generate_file(desc, "_sqlmodel.py")
|
|
231
|
-
f.preamble(desc)
|
|
232
|
-
for ext in desc.extensions:
|
|
233
|
-
handle_extension(ext, f)
|
|
234
|
-
for e in desc.enums:
|
|
235
|
-
handle_leaf_enum(e, f)
|
|
236
|
-
for m in desc.messages:
|
|
237
|
-
handle_message(m, f)
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
syntax = "proto3";
|
|
2
|
-
|
|
3
|
-
package example.sqlmodel;
|
|
4
|
-
|
|
5
|
-
import "google/protobuf/descriptor.proto";
|
|
6
|
-
|
|
7
|
-
// Marks a protobuf message as a SQLModel table.
|
|
8
|
-
extend google.protobuf.MessageOptions {
|
|
9
|
-
bool table = 50001;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// Overrides the default protobuf-to-SQLModel field conversion.
|
|
13
|
-
extend google.protobuf.FieldOptions {
|
|
14
|
-
string sqlmodel_type = 50002;
|
|
15
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
# Generated from sqlmodel_extensions.proto. DO NOT EDIT.
|
|
2
|
-
# Generated by protoc-gen-py v0.4.0 with parameter "".
|
|
3
|
-
# ruff: noqa: PGH004
|
|
4
|
-
# ruff: noqa
|
|
5
|
-
# fmt: off
|
|
6
|
-
|
|
7
|
-
from __future__ import annotations
|
|
8
|
-
|
|
9
|
-
from typing import Final, TYPE_CHECKING
|
|
10
|
-
|
|
11
|
-
from protobuf import Extension
|
|
12
|
-
from protobuf._codegen import file_desc
|
|
13
|
-
from protobuf.wkt import FieldOptions, MessageOptions, descriptor_pb
|
|
14
|
-
|
|
15
|
-
if TYPE_CHECKING:
|
|
16
|
-
from protobuf import DescFile
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
ext_table: Final[Extension[MessageOptions, bool]] = Extension()
|
|
20
|
-
"""
|
|
21
|
-
```proto
|
|
22
|
-
extend google.protobuf.MessageOptions { optional bool table = 50001 [json_name = "[example.sqlmodel.table]"] }
|
|
23
|
-
```
|
|
24
|
-
"""
|
|
25
|
-
|
|
26
|
-
ext_sqlmodel_type: Final[Extension[FieldOptions, str]] = Extension()
|
|
27
|
-
"""
|
|
28
|
-
```proto
|
|
29
|
-
extend google.protobuf.FieldOptions { optional string sqlmodel_type = 50002 [json_name = "[example.sqlmodel.sqlmodel_type]"] }
|
|
30
|
-
```
|
|
31
|
-
"""
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
_DESC = file_desc(
|
|
35
|
-
b'\n\x19sqlmodel_extensions.proto\x12\x10example.sqlmodel\x1a google/protobuf/descriptor.proto:7\n\x05table\x18\xd1\x86\x03 \x01(\x08\x12\x1f.google.protobuf.MessageOptionsR\x05table:D\n\rsqlmodel_type\x18\xd2\x86\x03 \x01(\t\x12\x1d.google.protobuf.FieldOptionsR\x0csqlmodelTypeb\x06proto3',
|
|
36
|
-
[
|
|
37
|
-
descriptor_pb.desc(),
|
|
38
|
-
],
|
|
39
|
-
{
|
|
40
|
-
"table": ext_table,
|
|
41
|
-
"sqlmodel_type": ext_sqlmodel_type,
|
|
42
|
-
},
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def desc() -> DescFile:
|
|
47
|
-
"""Returns the descriptor for the file `sqlmodel_extensions.proto`."""
|
|
48
|
-
return _DESC
|
|
File without changes
|
{protoc_gen_sqlmodel-0.1.0 → protoc_gen_sqlmodel-0.2.0}/src/protoc_gen_sqlmodel/proto/__init__.py
RENAMED
|
File without changes
|