Skip to content

Commit e91e61e

Browse files
committed
Merge branch 'feat/wave3-py' into feat/wave3-uri-inet
2 parents e404641 + 8020902 commit e91e61e

9 files changed

Lines changed: 126 additions & 3 deletions

File tree

‎server/python/src/metaobjects/codegen/fr010_field_mapping.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def scalar_kind(sub_type: str) -> str | None:
101101
fc.FIELD_SUBTYPE_DATE,
102102
fc.FIELD_SUBTYPE_TIME,
103103
fc.FIELD_SUBTYPE_TIMESTAMP,
104+
# ADR-0036/0037 Wave 3: uri/inet are string scalars on the wire.
105+
fc.FIELD_SUBTYPE_URI,
106+
fc.FIELD_SUBTYPE_INET,
104107
):
105108
return "STRING"
106109
if sub_type == fc.FIELD_SUBTYPE_INT:

‎server/python/src/metaobjects/codegen/generators/entity_model.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ def _is_sql_expr_default(value: object) -> bool:
4848
return isinstance(value, str) and any(p.search(value) for p in _SQL_EXPR_DEFAULT_PATTERNS)
4949

5050

51+
# ADR-0036/0037 Wave 3 — the CANONICAL hostname matcher for @stringFormat: hostname.
52+
# The matcher lives in codegen (NOT author validator.regex) so every port replicates
53+
# the SAME canonical form — cross-language regex engines diverge, so this one
54+
# expression is the byte-identical source of truth. RFC 1123 labels: 1–63 chars each,
55+
# alphanumeric + internal hyphens, dot-separated; total length 1–253; anchored.
56+
# Byte-identical to the TS zod-validators HOSTNAME_REGEX_LITERAL body.
57+
_HOSTNAME_REGEX = (
58+
r"^(?=.{1,253}$)([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)"
59+
r"(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
60+
)
61+
62+
5163
def _validators(field: MetaField, sub_type: str) -> list[MetaField]:
5264
"""The field's own ``validator.<sub_type>`` children (effective, supers included)."""
5365
return [
@@ -111,6 +123,13 @@ def _validator_constraints(field: MetaField) -> dict[str, object]:
111123
kwargs["pattern"] = pattern
112124
break
113125

126+
# ADR-0036/0037 Wave 3 — @stringFormat: hostname -> a codegen-owned canonical
127+
# hostname pattern= (the matcher lives HERE, never author validator.regex, so
128+
# every port replicates the byte-identical form). @stringFormat: email is bound
129+
# as EmailStr in _field_line (a type, not a pattern), so it is not handled here.
130+
if field.attrs().get(fc.FIELD_ATTR_STRING_FORMAT) == fc.STRING_FORMAT_HOSTNAME:
131+
kwargs["pattern"] = _HOSTNAME_REGEX
132+
114133
return kwargs
115134

116135

@@ -132,6 +151,17 @@ def _field_line(field: MetaField, imports: set[str], config: GenConfig) -> tuple
132151
pt = py_type_for(field)
133152
imports.update(pt.imports)
134153
type_expr = pt.expr
154+
# ADR-0036/0037 Wave 3 — @stringFormat: email narrows a plain string to a
155+
# validated email. Bind Pydantic's idiomatic EmailStr (the Python analogue of
156+
# Zod .email()); hostname stays a plain str with a codegen-owned canonical
157+
# pattern= (handled in _validator_constraints). The field stays a string.
158+
if (
159+
field.sub_type == fc.FIELD_SUBTYPE_STRING
160+
and field.attrs().get(fc.FIELD_ATTR_STRING_FORMAT) == fc.STRING_FORMAT_EMAIL
161+
):
162+
base_expr = "EmailStr"
163+
imports.add("from pydantic import EmailStr")
164+
type_expr = f"list[{base_expr}]" if field_is_array(field) else base_expr
135165
if field.sub_type in (fc.FIELD_SUBTYPE_OBJECT, fc.FIELD_SUBTYPE_MAP):
136166
# A field.object (-> VO) and a field.map with @objectRef (-> dict[str, VO])
137167
# both reference a value-object by name; import it so the annotation

‎server/python/src/metaobjects/codegen/generators/filter_allowlist_generator.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ def ops_for_subtype_ordered(sub_type: str | None) -> tuple[str, ...]:
9191
return ()
9292
if sub_type in (fc.FIELD_SUBTYPE_STRING, fc.FIELD_SUBTYPE_ENUM):
9393
return _OPS_STRING
94-
if sub_type == fc.FIELD_SUBTYPE_UUID:
94+
# ADR-0036/0037 Wave 3 — field.uri is string-like (substring searchable);
95+
# field.inet is uuid-like (identity value, no substring search, no ordering).
96+
if sub_type == fc.FIELD_SUBTYPE_URI:
97+
return _OPS_STRING
98+
if sub_type in (fc.FIELD_SUBTYPE_UUID, fc.FIELD_SUBTYPE_INET):
9599
return _OPS_UUID
96100
if sub_type in (
97101
fc.FIELD_SUBTYPE_INT,

‎server/python/src/metaobjects/codegen/type_map.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ class PyType:
3030
# R6 Plan 2a — field.uuid binds the idiomatic native uuid.UUID (ADR-0001),
3131
# surfaced at build time. Wire/storage form stays a lowercase-canonical string.
3232
fc.FIELD_SUBTYPE_UUID: PyType("uuid.UUID", ("import uuid",)),
33+
# ADR-0036/0037 Wave 3 — field.uri binds Pydantic's AnyUrl (the idiomatic
34+
# native URL type; parses + validates scheme/authority/path — the Python
35+
# analogue of TS Zod z.string().url()). Wire/storage form stays a string;
36+
# DB column is text.
37+
fc.FIELD_SUBTYPE_URI: PyType("AnyUrl", ("from pydantic import AnyUrl",)),
38+
# ADR-0036/0037 Wave 3 — field.inet binds Pydantic's IPvAnyAddress (accepts
39+
# IPv4 + IPv6 — the Python analogue of TS Zod z.string().ip()). Wire/storage
40+
# form stays a string; DB column is the Postgres-native inet type.
41+
fc.FIELD_SUBTYPE_INET: PyType("IPvAnyAddress", ("from pydantic import IPvAnyAddress",)),
3342
}
3443

3544

‎server/python/src/metaobjects/core_types.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
FIELD_ATTR_REQUIRED,
2626
FIELD_ATTR_SCALE,
2727
FIELD_ATTR_STORAGE,
28+
FIELD_ATTR_STRING_FORMAT,
2829
FIELD_ATTR_UNIQUE,
2930
FIELD_ATTR_VALUE_TYPE,
3031
FIELD_ATTR_VALUES,
3132
FIELD_SUBTYPE_ENUM,
3233
STORAGE_VALUES,
34+
STRING_FORMAT_VALUES,
3335
)
3436
from .meta.core.field.meta_field import MetaField
3537
from .meta.persistence.db.db_constants import (
@@ -336,6 +338,25 @@ def _register_subtypes(
336338
)
337339
break
338340

341+
# field.string carries the @stringFormat attr (ADR-0036/0037 Wave 3 — a closed
342+
# validation format: email | hostname) IN ADDITION to the common field attrs. The
343+
# field stays a plain string (native binding + DB column unchanged); codegen owns
344+
# the canonical matcher per format. Append @stringFormat to the field.string def
345+
# registered by the bulk loop (mirrors TS, where field.string's attr set =
346+
# commonFieldAttrs + the @stringFormat closed-enum attr). The closed value-set
347+
# (allowed_values) gates the manifest's allowedValues + the loader value check.
348+
for _def in core_provider._defs: # noqa: SLF001 (provider build-time enrichment)
349+
if _def.type == TYPE_FIELD and _def.sub_type == fc.FIELD_SUBTYPE_STRING:
350+
_def.attrs.append(
351+
AttrSchema(
352+
name=FIELD_ATTR_STRING_FORMAT,
353+
value_type=ATTR_SUBTYPE_STRING,
354+
required=False,
355+
allowed_values=STRING_FORMAT_VALUES,
356+
)
357+
)
358+
break
359+
339360
# field.map carries the @valueType attr (scalar value subtype) IN ADDITION to the
340361
# common field attrs (@objectRef is already common). The bulk loop registered
341362
# field.map with the common attrs; append @valueType to that definition (mirrors

‎server/python/src/metaobjects/loader/validation_passes.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@
3333
FIELD_SUBTYPE_FLOAT,
3434
FIELD_SUBTYPE_INT,
3535
FIELD_SUBTYPE_LONG,
36+
FIELD_SUBTYPE_INET,
3637
FIELD_SUBTYPE_MAP,
3738
FIELD_SUBTYPE_OBJECT,
3839
FIELD_SUBTYPE_STRING,
3940
FIELD_SUBTYPE_TIME,
4041
FIELD_SUBTYPE_TIMESTAMP,
42+
FIELD_SUBTYPE_URI,
4143
FIELD_SUBTYPE_UUID,
4244
)
4345
from ..meta.persistence.db.db_constants import (
@@ -794,7 +796,12 @@ def ops_for_subtype(field_subtype: str) -> frozenset[str]:
794796
"""
795797
if field_subtype in _STRING_SUBTYPES:
796798
return _OPS_STRING
797-
if field_subtype == "uuid":
799+
# ADR-0036/0037 Wave 3 — field.uri is string-like (substring searchable):
800+
# eq/ne/in/like/isNull. field.inet is uuid-like (an identity value, no
801+
# substring search, no ordering): eq/ne/in/isNull.
802+
if field_subtype == FIELD_SUBTYPE_URI:
803+
return _OPS_STRING
804+
if field_subtype in ("uuid", FIELD_SUBTYPE_INET):
798805
return _OPS_UUID
799806
if field_subtype == "boolean":
800807
return _OPS_BOOLEAN

‎server/python/src/metaobjects/meta/core/field/field_constants.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@
2323
# (Python uuid.UUID) is a build-time codegen concern. A bare scalar like
2424
# field.long: no required attrs, no loader value-validation.
2525
FIELD_SUBTYPE_UUID = "uuid"
26+
# ADR-0036/0037 Wave 3 — field.uri: a URI/URL string. A concept WITH a native
27+
# type + behavior → a subtype (NOT a @stringFormat). String-backed on the wire;
28+
# the idiomatic native binding (Python urllib.parse / a URL-validated str) is a
29+
# build-time codegen concern. DB column is text (Postgres has no uri type). Ships
30+
# WITHOUT @kind in v1 (the native type subsumes url/urn). Mirrors TS FIELD_SUBTYPE_URI.
31+
FIELD_SUBTYPE_URI = "uri"
32+
# ADR-0036/0037 Wave 3 — field.inet: an IP-address string (IPv4 or IPv6). A
33+
# concept WITH a native type + behavior → a subtype. String-backed on the wire;
34+
# the idiomatic native binding (Python ipaddress / an IP-validated str) is a
35+
# build-time codegen concern. DB column is the Postgres-native inet type. Ships
36+
# WITHOUT @kind in v1 (the native type subsumes ipv4/ipv6). Mirrors TS FIELD_SUBTYPE_INET.
37+
FIELD_SUBTYPE_INET = "inet"
2638

2739
FIELD_SUBTYPES = (
2840
SUBTYPE_BASE,
@@ -40,6 +52,8 @@
4052
FIELD_SUBTYPE_MAP,
4153
FIELD_SUBTYPE_CURRENCY,
4254
FIELD_SUBTYPE_UUID,
55+
FIELD_SUBTYPE_URI,
56+
FIELD_SUBTYPE_INET,
4357
# Note: FIELD_SUBTYPE_ENUM is intentionally excluded here; it is registered
4458
# separately in core_types.py with its dedicated @values AttrSchema.
4559
)
@@ -105,6 +119,19 @@
105119
# Physical column name override (cross-port; renamed from @dbColumn).
106120
FIELD_ATTR_COLUMN = "column"
107121

122+
# ADR-0036/0037 Wave 3 — @stringFormat: a closed validation format for a plain
123+
# field.string that has NO native type or behavior of its own (email | hostname).
124+
# The field stays a plain string (native binding + DB column unchanged); codegen
125+
# emits the matching validator (the canonical matcher per format lives in each
126+
# port's codegen, NOT author validator.regex — cross-language regex engines
127+
# diverge). Named @stringFormat (not @format) to avoid colliding with template.*
128+
# @format. Concepts WITH a native type/behavior are subtypes instead (uuid →
129+
# field.uuid, url/uri → field.uri, ip → field.inet). Mirrors TS FIELD_ATTR_STRING_FORMAT.
130+
FIELD_ATTR_STRING_FORMAT = "stringFormat"
131+
STRING_FORMAT_EMAIL = "email"
132+
STRING_FORMAT_HOSTNAME = "hostname"
133+
STRING_FORMAT_VALUES = (STRING_FORMAT_EMAIL, STRING_FORMAT_HOSTNAME)
134+
108135
# @autoSet allowed values (cross-port).
109136
AUTO_SET_ON_CREATE = "onCreate"
110137
AUTO_SET_ON_UPDATE = "onUpdate"

‎server/python/src/metaobjects/meta/core/field/meta_field.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
# R6 Plan 2a — field.uuid is string-backed on the wire; the native uuid.UUID
2626
# binding is surfaced at codegen time (see codegen/type_map.py).
2727
fc.FIELD_SUBTYPE_UUID: DataType.STRING,
28+
# ADR-0036/0037 Wave 3 — field.uri / field.inet are string-backed on the wire
29+
# (same as field.uuid); the idiomatic native bindings (urllib.parse / ipaddress)
30+
# are surfaced at codegen time (see codegen/type_map.py).
31+
fc.FIELD_SUBTYPE_URI: DataType.STRING,
32+
fc.FIELD_SUBTYPE_INET: DataType.STRING,
2833
}
2934

3035

‎server/python/src/metaobjects/spec_metamodel/field.json‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"description": "Variable-length text. Binds to the native string type; DB column is VARCHAR/TEXT (use @maxLength for VARCHAR(n)).",
2525
"whenToUse": "Plain variable-length text. Set @maxLength to size the column. The default for textual data.",
2626
"children": [
27-
{ "type": "attr", "subType": "int", "name": "maxLength", "min": 0, "max": 1, "description": "Maximum character length for string-typed fields (drives VARCHAR(n))." }
27+
{ "type": "attr", "subType": "int", "name": "maxLength", "min": 0, "max": 1, "description": "Maximum character length for string-typed fields (drives VARCHAR(n))." },
28+
{ "type": "attr", "subType": "string", "name": "stringFormat", "min": 0, "max": 1, "allowedValues": ["email", "hostname"], "description": "ADR-0036/0037: a closed validation format for a plain string field that has NO native type or behavior of its own — email | hostname. The field stays a plain string (native binding + DB column unchanged); codegen emits the matching validator (Zod z.email() / a hostname check; PG CHECK where appropriate). The canonical matcher per format lives in each port's codegen, NOT author validator.regex (cross-language regex engines diverge). Named @stringFormat (not @format) to avoid colliding with template.* @format (output format). Concepts WITH a native type or behavior are subtypes instead (uuid → field.uuid, url/uri → field.uri, ip → field.inet)." }
2829
]
2930
},
3031
{
@@ -159,6 +160,22 @@
159160
"extendsBase": true,
160161
"description": "Logical UUID identity scalar. A bare scalar (no required attrs, no loader value-validation) — binds to TS string (no native UUID type); DB column is Postgres-native uuid.",
161162
"whenToUse": "A key or external identifier is a UUID. Use it for a typed UUID column instead of a plain string."
163+
},
164+
{
165+
"type": "field",
166+
"subType": "uri",
167+
"dataType": "string",
168+
"extendsBase": true,
169+
"description": "A URI/URL string (ADR-0036/0037 — a concept with a native type + behavior, so a subtype not a @stringFormat). Binds to TS string (TS has no native URI type, same as field.uuid); other ports bind to their native URI type (Java java.net.URI, C# System.Uri, Python urllib.parse, Kotlin java.net.URI) and parse scheme/authority/path. DB column is text (Postgres has no uri type). Codegen emits a URL/URI validator (TS Zod z.string().url()). v1 ships WITHOUT @kind — the native type subsumes url/urn; @kind is reserved for a later url|urn value-constraint.",
170+
"whenToUse": "A field holds a URL or URI. Use it for a typed URI column + URL validation instead of a plain string."
171+
},
172+
{
173+
"type": "field",
174+
"subType": "inet",
175+
"dataType": "string",
176+
"extendsBase": true,
177+
"description": "An IP-address string (IPv4 or IPv6) (ADR-0036/0037 — a concept with a native type + behavior, so a subtype not a @stringFormat). Binds to TS string (TS has no native IP type, same as field.uuid); other ports bind to their native IP type (Java java.net.InetAddress, C# System.Net.IPAddress, Python ipaddress, Kotlin java.net.InetAddress). DB column is the Postgres-native inet type. Codegen emits an IP validator accepting both v4 and v6 (TS Zod z.string().ip()). v1 ships WITHOUT @kind — the native type subsumes ipv4/ipv6; @kind is reserved for a later ipv4|ipv6 value-constraint.",
178+
"whenToUse": "A field holds an IPv4 or IPv6 address. Use it for a native Postgres inet column + IP validation instead of a plain string."
162179
}
163180
]
164181
}

0 commit comments

Comments
 (0)