11import inspect
22from dataclasses import asdict , dataclass
3- from typing import Any , Dict , Optional , get_args , get_origin , get_type_hints
3+ from types import SimpleNamespace
4+ from typing import (
5+ Any ,
6+ ClassVar ,
7+ Dict ,
8+ Optional ,
9+ get_args ,
10+ get_origin ,
11+ get_type_hints ,
12+ )
13+
14+ from linode_api4 .objects .filtering import FilterableAttribute
15+
16+ # Wraps the SimpleNamespace class and allows for
17+ # SQLAlchemy-style filter generation on JSONObjects.
18+ JSONFilterGroup = SimpleNamespace
19+
20+
21+ class JSONFilterableMetaclass (type ):
22+ def __init__ (cls , name , bases , dct ):
23+ setattr (
24+ cls ,
25+ "filters" ,
26+ JSONFilterGroup (
27+ ** {
28+ k : FilterableAttribute (k )
29+ for k in cls .__annotations__ .keys ()
30+ }
31+ ),
32+ )
33+
34+ super ().__init__ (name , bases , dct )
435
536
637@dataclass
7- class JSONObject :
38+ class JSONObject ( metaclass = JSONFilterableMetaclass ) :
839 """
940 A simple helper class for serializable API objects.
1041 This is typically used for nested object values.
@@ -13,6 +44,16 @@ class JSONObject:
1344 fields and static typing.
1445 """
1546
47+ filters : ClassVar [JSONFilterGroup ] = None
48+ """
49+ A group containing FilterableAttributes used to create SQLAlchemy-style filters.
50+
51+ Example usage::
52+ self.client.regions.availability(
53+ RegionAvailabilityEntry.filters.plan == "premium4096.7"
54+ )
55+ """
56+
1657 def __init__ (self ):
1758 raise NotImplementedError (
1859 "JSONObject is not intended to be constructed directly"
0 commit comments