sera-2 1.5.1__tar.gz → 1.5.3__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.
- {sera_2-1.5.1 → sera_2-1.5.3}/PKG-INFO +1 -1
- {sera_2-1.5.1 → sera_2-1.5.3}/pyproject.toml +1 -1
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/make_python_model.py +24 -6
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/make_typescript_model.py +3 -3
- sera_2-1.5.3/sera/models/_default.py +9 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_parse.py +22 -1
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_property.py +8 -4
- {sera_2-1.5.1 → sera_2-1.5.3}/README.md +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/__init__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/constants.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/libs/__init__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/libs/api_helper.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/libs/base_orm.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/libs/base_service.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/__init__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/__main__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/make_app.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/make_python_api.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/make/make_python_services.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/misc/__init__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/misc/_utils.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/__init__.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_class.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_collection.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_constraints.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_datatype.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_module.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_multi_lingual_string.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/models/_schema.py +0 -0
- {sera_2-1.5.1 → sera_2-1.5.3}/sera/typing.py +0 -0
@@ -154,7 +154,25 @@ def make_python_data_model(
|
|
154
154
|
else:
|
155
155
|
raise NotImplementedError(prop.data.constraints)
|
156
156
|
|
157
|
-
|
157
|
+
prop_default_value = None
|
158
|
+
if prop.default_value is not None:
|
159
|
+
prop_default_value = expr.ExprConstant(prop.default_value)
|
160
|
+
elif prop.default_factory is not None:
|
161
|
+
program.import_(prop.default_factory.pyfunc)
|
162
|
+
prop_default_value = expr.ExprFuncCall(
|
163
|
+
expr.ExprIdent("msgspec.field"),
|
164
|
+
[
|
165
|
+
PredefinedFn.keyword_assignment(
|
166
|
+
"default_factory",
|
167
|
+
expr.ExprIdent(prop.default_factory.pyfunc),
|
168
|
+
)
|
169
|
+
],
|
170
|
+
)
|
171
|
+
cls_ast(
|
172
|
+
stmt.DefClassVarStatement(
|
173
|
+
prop.name, pytype_type, prop_default_value
|
174
|
+
)
|
175
|
+
)
|
158
176
|
elif isinstance(prop, ObjectProperty):
|
159
177
|
if prop.target.db is not None:
|
160
178
|
# if the target class is in the database, we expect the user to pass the foreign key for it.
|
@@ -501,7 +519,7 @@ def make_python_relational_model(
|
|
501
519
|
"index", expr.ExprConstant(True)
|
502
520
|
)
|
503
521
|
)
|
504
|
-
if prop.
|
522
|
+
if prop.is_optional:
|
505
523
|
propvalargs.append(
|
506
524
|
PredefinedFn.keyword_assignment(
|
507
525
|
"nullable", expr.ExprConstant(True)
|
@@ -580,12 +598,12 @@ def make_python_relational_object_property(
|
|
580
598
|
"onupdate",
|
581
599
|
expr.ExprConstant(prop.db.on_target_update.to_sqlalchemy()),
|
582
600
|
),
|
583
|
-
PredefinedFn.keyword_assignment(
|
584
|
-
"nullable",
|
585
|
-
expr.ExprConstant(prop.is_optional),
|
586
|
-
),
|
587
601
|
],
|
588
602
|
),
|
603
|
+
PredefinedFn.keyword_assignment(
|
604
|
+
"nullable",
|
605
|
+
expr.ExprConstant(prop.is_optional),
|
606
|
+
),
|
589
607
|
],
|
590
608
|
)
|
591
609
|
|
@@ -711,9 +711,9 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
|
|
711
711
|
(
|
712
712
|
expr.ExprIdent("isRequired"),
|
713
713
|
expr.ExprConstant(
|
714
|
-
|
715
|
-
prop.
|
716
|
-
|
714
|
+
not prop.is_optional
|
715
|
+
and prop.default_value is None
|
716
|
+
and prop.default_factory is None
|
717
717
|
),
|
718
718
|
),
|
719
719
|
]
|
@@ -15,6 +15,7 @@ from sera.models._datatype import (
|
|
15
15
|
predefined_sql_datatypes,
|
16
16
|
predefined_ts_datatypes,
|
17
17
|
)
|
18
|
+
from sera.models._default import DefaultFactory
|
18
19
|
from sera.models._multi_lingual_string import MultiLingualString
|
19
20
|
from sera.models._property import (
|
20
21
|
Cardinality,
|
@@ -121,11 +122,13 @@ def _parse_property(
|
|
121
122
|
is_indexed=db.get("is_indexed", False)
|
122
123
|
or db.get("is_unique", False)
|
123
124
|
or db.get("is_primary_key", False),
|
124
|
-
is_nullable=db.get("is_nullable", False),
|
125
125
|
)
|
126
126
|
if "db" in prop
|
127
127
|
else None
|
128
128
|
),
|
129
|
+
is_optional=prop.get("is_optional", False),
|
130
|
+
default_value=_parse_default_value(prop.get("default_value", None)),
|
131
|
+
default_factory=_parse_default_factory(prop.get("default_factory", None)),
|
129
132
|
)
|
130
133
|
|
131
134
|
assert "target" in prop, prop
|
@@ -213,3 +216,21 @@ def _parse_datatype(datatype: dict | str) -> DataType:
|
|
213
216
|
)
|
214
217
|
|
215
218
|
raise NotImplementedError(datatype)
|
219
|
+
|
220
|
+
|
221
|
+
def _parse_default_value(
|
222
|
+
default_value: str | int | bool | None,
|
223
|
+
) -> str | int | bool | None:
|
224
|
+
if default_value is None:
|
225
|
+
return None
|
226
|
+
if not isinstance(default_value, (str, int, bool)):
|
227
|
+
raise NotImplementedError(default_value)
|
228
|
+
return default_value
|
229
|
+
|
230
|
+
|
231
|
+
def _parse_default_factory(default_factory: str | None) -> str | None:
|
232
|
+
if default_factory is None:
|
233
|
+
return None
|
234
|
+
return DefaultFactory(
|
235
|
+
pyfunc=default_factory["pyfunc"], tsfunc=default_factory["tsfunc"]
|
236
|
+
)
|
@@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Literal, Optional
|
|
6
6
|
|
7
7
|
from sera.models._constraints import Constraint
|
8
8
|
from sera.models._datatype import DataType
|
9
|
+
from sera.models._default import DefaultFactory
|
9
10
|
from sera.models._multi_lingual_string import MultiLingualString
|
10
11
|
|
11
12
|
if TYPE_CHECKING:
|
@@ -87,6 +88,8 @@ class Property:
|
|
87
88
|
description: MultiLingualString
|
88
89
|
# other attributes for generating data model such as upsert and return.
|
89
90
|
data: PropDataAttrs = field(default_factory=PropDataAttrs)
|
91
|
+
# whether this property is optional or not
|
92
|
+
is_optional: bool = False
|
90
93
|
|
91
94
|
|
92
95
|
@dataclass(kw_only=True)
|
@@ -101,14 +104,17 @@ class DataPropDBInfo:
|
|
101
104
|
is_unique: bool = False
|
102
105
|
# whether this property is indexed or not
|
103
106
|
is_indexed: bool = False
|
104
|
-
# whether this property is nullable or not
|
105
|
-
is_nullable: bool = False
|
106
107
|
|
107
108
|
|
108
109
|
@dataclass(kw_only=True)
|
109
110
|
class DataProperty(Property):
|
110
111
|
# data type of the property
|
111
112
|
datatype: DataType
|
113
|
+
# default value of this property if it is not provided (then optional is false by default)
|
114
|
+
default_value: Optional[str | int | bool] = None
|
115
|
+
# default value factory of this property if it is not provided
|
116
|
+
default_factory: Optional[DefaultFactory] = None
|
117
|
+
|
112
118
|
# other database properties of this property
|
113
119
|
db: Optional[DataPropDBInfo] = None
|
114
120
|
|
@@ -148,8 +154,6 @@ class ObjectProperty(Property):
|
|
148
154
|
# to store the relationship -- users can overwrite this generated class by define the one with the same
|
149
155
|
# name
|
150
156
|
cardinality: Cardinality
|
151
|
-
# whether this property is optional or not
|
152
|
-
is_optional: bool = False
|
153
157
|
# whether this property is stored as a mapping dic[str, Target] or not
|
154
158
|
# only valid for *-to-many relationships
|
155
159
|
is_map: bool = False
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|