Skip to content

Commit 164054a

Browse files
committed
Python: implement object oneOf support
1 parent c86a52e commit 164054a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

glean-core/python/glean/_loader.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ def _object_factory(
226226
yield n, ty
227227
yield from fct
228228
fields.append((itemname, ty, field(default=None)))
229+
elif val["type"] == "oneof":
230+
# We don't actually enforce proper validation here
231+
subtypes = [_struct_type(st) for st in val["subtypes"]]
232+
fields.append((itemname, Union[None, *subtypes], field(default=None)))
229233
else:
230234
fields.append((itemname, _struct_type(val["type"]), field(default=None)))
231235
newclass = make_dataclass(name, fields, bases=(metrics.ObjectSerialize,))

samples/python/glean-sample/__main__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
# Does not throw an exception, but will record an error
3636
metrics.party.balloons.set([])
3737

38+
ch = metrics.party.ChooserObject()
39+
f = metrics.party.ChooserObjectItem(key="fortywo", value=42)
40+
ch.append(f)
41+
f = metrics.party.ChooserObjectItem(key="to-be", value=False)
42+
ch.append(f)
43+
f = metrics.party.ChooserObjectItem(key="to-be", value=["string"])
44+
ch.append(f)
45+
metrics.party.chooser.set(ch)
46+
3847
pings.prototype.submit()
3948

4049
Glean.shutdown()

samples/python/metrics.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,29 @@ party:
4848
type: string
4949
diameter:
5050
type: number
51+
52+
chooser:
53+
type: object
54+
description: |
55+
Array of key-value elements
56+
bugs:
57+
- https://bugzilla.mozilla.org/123456789
58+
data_reviews:
59+
- http://example.com/reviews
60+
notification_emails:
61+
62+
expires: never
63+
send_in_pings:
64+
- prototype
65+
structure:
66+
type: array
67+
items:
68+
type: object
69+
properties:
70+
key:
71+
type: string
72+
value:
73+
oneOf:
74+
- type: string
75+
- type: number
76+
- type: boolean

0 commit comments

Comments
 (0)