Skip to content

Commit 5c79239

Browse files
committed
refactor(core): extract VehicleSelectorBase
Pull the shared `dimension` and `comparison` fields of the five vehicle selector subtypes into a `VehicleSelectorBase` parent, and thread `discriminator="dimension"` through the `VehicleSelector` annotated union. The discriminator turns the union into a Pydantic discriminated union, so it serializes as JSON Schema's `oneOf` + `discriminator` rather than `anyOf`. Regenerated segment_baseline_schema.json captures the new shape. This is a prerequisite for downstream tooling that walks discriminated unions structurally (e.g. PySpark codegen for segment's nested vehicle scoping). Signed-off-by: Seth Fitzsimmons <seth@mojodna.net>
1 parent bc5d681 commit 5c79239

2 files changed

Lines changed: 61 additions & 33 deletions

File tree

packages/overture-schema-common/src/overture/schema/common/scoping/vehicle.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,30 @@ class VehicleRelation(str, Enum):
3838

3939

4040
@no_extra_fields
41-
class VehicleAxleCountSelector(BaseModel):
41+
class VehicleSelectorBase(BaseModel):
4242
"""
43-
Selects vehicles based on the number of axles they have.
43+
Common fields shared by all vehicle selector subtypes.
44+
45+
See also: `VehicleSelector`.
4446
"""
4547

46-
dimension: Literal[VehicleDimension.AXLE_COUNT]
48+
dimension: VehicleDimension
4749
comparison: VehicleRelation
50+
51+
52+
@no_extra_fields
53+
class VehicleAxleCountSelector(VehicleSelectorBase):
54+
"""Selects vehicles based on the number of axles they have."""
55+
56+
dimension: Literal[VehicleDimension.AXLE_COUNT]
4857
value: uint8 = Field(description="Number of axles on the vehicle")
4958

5059

5160
@no_extra_fields
52-
class VehicleHeightSelector(BaseModel):
53-
"""
54-
Selects vehicles based on their height.
55-
"""
61+
class VehicleHeightSelector(VehicleSelectorBase):
62+
"""Selects vehicles based on their height."""
5663

5764
dimension: Literal[VehicleDimension.HEIGHT]
58-
comparison: VehicleRelation
5965
value: Annotated[
6066
float64,
6167
Field(
@@ -66,13 +72,10 @@ class VehicleHeightSelector(BaseModel):
6672

6773

6874
@no_extra_fields
69-
class VehicleLengthSelector(BaseModel):
70-
"""
71-
Selects vehicles based on their length.
72-
"""
75+
class VehicleLengthSelector(VehicleSelectorBase):
76+
"""Selects vehicles based on their length."""
7377

7478
dimension: Literal[VehicleDimension.LENGTH]
75-
comparison: VehicleRelation
7679
value: Annotated[
7780
float64,
7881
Field(
@@ -83,13 +86,10 @@ class VehicleLengthSelector(BaseModel):
8386

8487

8588
@no_extra_fields
86-
class VehicleWeightSelector(BaseModel):
87-
"""
88-
Selects vehicles based on their weight.
89-
"""
89+
class VehicleWeightSelector(VehicleSelectorBase):
90+
"""Selects vehicles based on their weight."""
9091

9192
dimension: Literal[VehicleDimension.WEIGHT]
92-
comparison: VehicleRelation
9393
value: Annotated[
9494
float64,
9595
Field(
@@ -100,13 +100,10 @@ class VehicleWeightSelector(BaseModel):
100100

101101

102102
@no_extra_fields
103-
class VehicleWidthSelector(BaseModel):
104-
"""
105-
Selects vehicles based on their width.
106-
"""
103+
class VehicleWidthSelector(VehicleSelectorBase):
104+
"""Selects vehicles based on their width."""
107105

108106
dimension: Literal[VehicleDimension.WIDTH]
109-
comparison: VehicleRelation
110107
value: Annotated[
111108
float64,
112109
Field(
@@ -123,7 +120,8 @@ class VehicleWidthSelector(BaseModel):
123120
| VehicleWeightSelector
124121
| VehicleWidthSelector,
125122
Field(
126-
description="Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count."
123+
discriminator="dimension",
124+
description="Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count.",
127125
),
128126
]
129127
"""

packages/overture-schema-transportation-theme/tests/segment_baseline_schema.json

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,7 +1862,18 @@
18621862
"vehicle": {
18631863
"description": "A list of one or more vehicle parameters that limit the vehicles the containing AccessRestrictionRule applies to.",
18641864
"items": {
1865-
"anyOf": [
1865+
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count.",
1866+
"discriminator": {
1867+
"mapping": {
1868+
"axle_count": "#/$defs/VehicleAxleCountSelector",
1869+
"height": "#/$defs/VehicleHeightSelector",
1870+
"length": "#/$defs/VehicleLengthSelector",
1871+
"weight": "#/$defs/VehicleWeightSelector",
1872+
"width": "#/$defs/VehicleWidthSelector"
1873+
},
1874+
"propertyName": "dimension"
1875+
},
1876+
"oneOf": [
18661877
{
18671878
"$ref": "#/$defs/VehicleAxleCountSelector"
18681879
},
@@ -1878,8 +1889,7 @@
18781889
{
18791890
"$ref": "#/$defs/VehicleWidthSelector"
18801891
}
1881-
],
1882-
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count."
1892+
]
18831893
},
18841894
"minItems": 1,
18851895
"title": "Vehicle",
@@ -2025,7 +2035,18 @@
20252035
"vehicle": {
20262036
"description": "A list of one or more vehicle parameters that limit the vehicles the containing ProhibitedTransitionRule applies to.",
20272037
"items": {
2028-
"anyOf": [
2038+
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count.",
2039+
"discriminator": {
2040+
"mapping": {
2041+
"axle_count": "#/$defs/VehicleAxleCountSelector",
2042+
"height": "#/$defs/VehicleHeightSelector",
2043+
"length": "#/$defs/VehicleLengthSelector",
2044+
"weight": "#/$defs/VehicleWeightSelector",
2045+
"width": "#/$defs/VehicleWidthSelector"
2046+
},
2047+
"propertyName": "dimension"
2048+
},
2049+
"oneOf": [
20292050
{
20302051
"$ref": "#/$defs/VehicleAxleCountSelector"
20312052
},
@@ -2041,8 +2062,7 @@
20412062
{
20422063
"$ref": "#/$defs/VehicleWidthSelector"
20432064
}
2044-
],
2045-
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count."
2065+
]
20462066
},
20472067
"minItems": 1,
20482068
"title": "Vehicle",
@@ -2173,7 +2193,18 @@
21732193
"vehicle": {
21742194
"description": "A list of one or more vehicle parameters that limit the vehicles the containing SpeedLimitRule applies to.",
21752195
"items": {
2176-
"anyOf": [
2196+
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count.",
2197+
"discriminator": {
2198+
"mapping": {
2199+
"axle_count": "#/$defs/VehicleAxleCountSelector",
2200+
"height": "#/$defs/VehicleHeightSelector",
2201+
"length": "#/$defs/VehicleLengthSelector",
2202+
"weight": "#/$defs/VehicleWeightSelector",
2203+
"width": "#/$defs/VehicleWidthSelector"
2204+
},
2205+
"propertyName": "dimension"
2206+
},
2207+
"oneOf": [
21772208
{
21782209
"$ref": "#/$defs/VehicleAxleCountSelector"
21792210
},
@@ -2189,8 +2220,7 @@
21892220
{
21902221
"$ref": "#/$defs/VehicleWidthSelector"
21912222
}
2192-
],
2193-
"description": "Selects vehicles that a scope applies to based on criteria such as height, weight, or axle count."
2223+
]
21942224
},
21952225
"minItems": 1,
21962226
"title": "Vehicle",

0 commit comments

Comments
 (0)