sera-2 1.6.7__py3-none-any.whl → 1.7.1__py3-none-any.whl

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.
File without changes
sera/libs/dag/_dag.py ADDED
File without changes
@@ -1,10 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
+ import re
3
4
  from typing import Any, Callable
4
5
 
5
6
  from codegen.models import AST, PredefinedFn, Program, expr, stmt
6
7
  from codegen.models.var import DeferredVar
7
8
  from loguru import logger
9
+
8
10
  from sera.misc import (
9
11
  assert_isinstance,
10
12
  assert_not_null,
@@ -307,6 +309,10 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
307
309
  f"{cls.name}Id",
308
310
  dep=f"@.models.{pkg.dir.name}.{cls.name}.{cls.name}Id",
309
311
  )
312
+ else:
313
+ # for none id properties, we need to include a type for "invalid" value
314
+ tstype = _inject_type_for_invalid_value(tstype)
315
+
310
316
  if tstype.dep is not None:
311
317
  program.import_(tstype.dep, True)
312
318
 
@@ -821,6 +827,11 @@ def make_typescript_data_model(schema: Schema, target_pkg: Package):
821
827
  PredefinedFn.dict(
822
828
  [
823
829
  (expr.ExprIdent("name"), expr.ExprConstant(prop.name)),
830
+ (expr.ExprIdent("tsName"), expr.ExprConstant(propname)),
831
+ (
832
+ expr.ExprIdent("updateFuncName"),
833
+ expr.ExprConstant(f"update{to_pascal_case(prop.name)}"),
834
+ ),
824
835
  (
825
836
  expr.ExprIdent("label"),
826
837
  expr.ExprConstant(prop.label.to_dict()),
@@ -1067,3 +1078,48 @@ def make_typescript_enum(schema: Schema, target_pkg: Package):
1067
1078
  ),
1068
1079
  )
1069
1080
  enum_pkg.module("index").write(program)
1081
+
1082
+
1083
+ def _inject_type_for_invalid_value(tstype: TsTypeWithDep) -> TsTypeWithDep:
1084
+ """
1085
+ Inject a type for "invalid" values into the given TypeScript type. For context, see the discussion in Data Modeling Problems:
1086
+ What would be an appropriate type for an invalid value? Since it's user input, it will be a string type.
1087
+
1088
+ However, there are some exceptions such as boolean type, which will always be valid and do not need injection.
1089
+
1090
+ If the type already includes `string` type, no changes are needed. Otherwise, we add `string` to the type. For example:
1091
+ - (number | undefined) -> (number | undefined | string)
1092
+ - number | undefined -> number | undefined | string
1093
+ - number[] -> (number | string)[]
1094
+ - (number | undefined)[] -> (number | undefined | string)[]
1095
+ """
1096
+ if tstype.type == "boolean":
1097
+ return tstype
1098
+
1099
+ # TODO: fix me and make it more robust!
1100
+ m = re.match(r"(\(?[a-zA-Z \|]+\)?)(\[\])", tstype.type)
1101
+ if m is not None:
1102
+ # This is an array type, add string to the inner type
1103
+ inner_type = m.group(1)
1104
+ if "string" not in inner_type:
1105
+ if inner_type.startswith("(") and inner_type.endswith(")"):
1106
+ # Already has parentheses
1107
+ inner_type = f"{inner_type[:-1]} | string)"
1108
+ else:
1109
+ # Need to add parentheses
1110
+ inner_type = f"({inner_type} | string)"
1111
+ return TsTypeWithDep(inner_type + "[]", tstype.dep)
1112
+
1113
+ m = re.match(r"^\(?[a-zA-Z \|]+\)?$", tstype.type)
1114
+ if m is not None:
1115
+ if "string" not in tstype.type:
1116
+ if tstype.type.startswith("(") and tstype.type.endswith(")"):
1117
+ # Already has parentheses
1118
+ new_type = f"{tstype.type[:-1]} | string)"
1119
+ else:
1120
+ # Needs parentheses for clarity
1121
+ new_type = f"({tstype.type} | string)"
1122
+ return TsTypeWithDep(new_type, tstype.dep)
1123
+ return tstype
1124
+
1125
+ raise NotImplementedError(tstype.type)
sera/models/_datatype.py CHANGED
@@ -69,10 +69,21 @@ class TsTypeWithDep:
69
69
  return expr.ExprConstant(False)
70
70
  if self.type == "string | undefined":
71
71
  return expr.ExprConstant("undefined")
72
+ if self.type.endswith("| string)") or self.type.endswith("| string"):
73
+ return expr.ExprConstant("")
72
74
  raise ValueError(f"Unknown type: {self.type}")
73
75
 
74
76
  def as_list_type(self) -> TsTypeWithDep:
75
- return TsTypeWithDep(type=f"{self.type}[]", dep=self.dep)
77
+ """Convert the type to a list type.
78
+ If the type is not a simple identifier, wrap it in parentheses.
79
+ """
80
+ # Check if type is a simple identifier or needs parentheses
81
+ if not all(c.isalnum() or c == "_" for c in self.type.strip()):
82
+ # Type contains special chars like | or spaces, wrap in parentheses
83
+ list_type = f"({self.type})[]"
84
+ else:
85
+ list_type = f"{self.type}[]"
86
+ return TsTypeWithDep(type=list_type, dep=self.dep)
76
87
 
77
88
 
78
89
  @dataclass
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.6.7
3
+ Version: 1.7.1
4
4
  Summary:
5
5
  Author: Binh Vu
6
6
  Author-email: bvu687@gmail.com
@@ -4,13 +4,15 @@ sera/libs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  sera/libs/api_helper.py,sha256=hUEy0INHM18lxTQ348tgbXNceOHcjiAnqmuL_8CRpLQ,2509
5
5
  sera/libs/base_orm.py,sha256=dyh0OT2sbHku5qPJXvRzYAHRTSXvvbQaS-Qwg65Bw04,2918
6
6
  sera/libs/base_service.py,sha256=l5D4IjxIiz8LBRranUYddb8J0Y6SwSyetKYTLrCUdQA,4098
7
+ sera/libs/dag/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ sera/libs/dag/_dag.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
9
  sera/make/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
10
  sera/make/__main__.py,sha256=G5O7s6135-708honwqMFn2yPTs06WbGQTHpupID0eZ4,1417
9
11
  sera/make/make_app.py,sha256=n9NtW73O3s_5Q31VHIRmnd-jEIcpDO7ksAsOdovde2s,5999
10
12
  sera/make/make_python_api.py,sha256=RuJUm9z-4plBEtjobeOPr12o27OT-0tSeXI4ZlM3IY0,29433
11
13
  sera/make/make_python_model.py,sha256=xf4revAwVWEnF6QhxbbqPyUGgXOOB--Gu3jPxsESg0Y,36593
12
14
  sera/make/make_python_services.py,sha256=RsinYZdfkrTlTn9CT50VgqGs9w6IZawsJx-KEmqfnEY,2062
13
- sera/make/make_typescript_model.py,sha256=6R5zgDRZLeX2jzu3mYmDHwupO_2OaOlahsiOZDSfTu0,43025
15
+ sera/make/make_typescript_model.py,sha256=vBbVPJ3yZoMIwUOBfYykD7i3PwzDhE72SQWk6vgepPc,45513
14
16
  sera/misc/__init__.py,sha256=Dh4uDq0D4N53h3zhvmwfa5a0TPVRSUvLzb0hkFuPirk,411
15
17
  sera/misc/_formatter.py,sha256=aCGYL08l8f3aLODHxSocxBBwkRYEo3K1QzCDEn3suj0,1685
16
18
  sera/misc/_utils.py,sha256=V5g4oLGHOhUCR75Kkcn1w01pAvGvaepK-T8Z3pIgHjI,1450
@@ -18,7 +20,7 @@ sera/models/__init__.py,sha256=vJC5Kzo_N7wd16ocNPy1VvAZDGNiWeiAhWJ4ihATKvA,780
18
20
  sera/models/_class.py,sha256=Wf0e8x6-szG9TzoFkAlqj7_dG0SCICMBw_333n3paxk,2514
19
21
  sera/models/_collection.py,sha256=ZnQEriKC4X88Zz48Kn1AVZKH-1_l8OgWa-zf2kcQOOE,1414
20
22
  sera/models/_constraints.py,sha256=L6QwKL3hRJ5DvvIB4JNgLoyvTKbE9FrpYRzezM4CweE,1484
21
- sera/models/_datatype.py,sha256=uMxK_8wBLowaIMIAYCb3V17YmkzJrKKc5orjImzqWbA,5818
23
+ sera/models/_datatype.py,sha256=eF71q-X7LA91sNYG29aXQiDVMjjr8-e6ez7P9NRwq_E,6376
22
24
  sera/models/_default.py,sha256=ABggW6qdPR4ZDqIPJdJ0GCGQ-7kfsfZmQ_DchgZEa-I,137
23
25
  sera/models/_enum.py,sha256=sy0q7E646F-APsqrVQ52r1fAQ_DCAeaNq5YM5QN3zIk,2070
24
26
  sera/models/_module.py,sha256=8QRSCubZmdDP9rL58rGAS6X5VCrkc1ZHvuMu1I1KrWk,5043
@@ -27,6 +29,6 @@ sera/models/_parse.py,sha256=sJYfQtwek96ltpgxExG4xUbiLnU3qvNYhTP1CeyXGjs,9746
27
29
  sera/models/_property.py,sha256=CmEmgOShtSyNFq05YW3tGupwCIVRzPMKudXWld8utPk,5530
28
30
  sera/models/_schema.py,sha256=r-Gqg9Lb_wR3UrbNvfXXgt_qs5bts0t2Ve7aquuF_OI,1155
29
31
  sera/typing.py,sha256=Q4QMfbtfrCjC9tFfsZPhsAnbNX4lm4NHQ9lmjNXYdV0,772
30
- sera_2-1.6.7.dist-info/METADATA,sha256=aqcUqMwOA87kdUt7P3mLpj4HgjOZxLcIatDVjVl_3-Q,856
31
- sera_2-1.6.7.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
32
- sera_2-1.6.7.dist-info/RECORD,,
32
+ sera_2-1.7.1.dist-info/METADATA,sha256=xyOCEi73Y50ltZKFNEZSNats-Ho3tPkd3_B1Y8dlECY,856
33
+ sera_2-1.7.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
34
+ sera_2-1.7.1.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.2
2
+ Generator: poetry-core 2.1.3
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any