Commit 9811868c authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Improve JSON{Object|Array}.path with logic from docker.py

parent 1f07fb3e
Loading
Loading
Loading
Loading
+10 −5
Original line number Diff line number Diff line
@@ -36,12 +36,17 @@ class JSONPathMixin:
	def path(self, path: str, kind: type[T], convert: Callable[[T], C]) -> C: ...

	def path(self, path: str, kind: type[T], convert: Callable[[T], C]|None = None) -> T|C:
		result = JSONPath(path).parse(self)[0]
		if convert is not None:
			return convert(result)
		elif isinstance(result, kind):
		result = JSONPath(path).parse(self)
		if "*" not in path:
			try:
				result = result[0]
			except IndexError:
				raise KeyError(path) from None
		if not isinstance(result, kind):
			raise TypeError(f"{path} is wrong type; expected {kind}; got {type(result)}")
		if convert is None:
			return result
		raise ValueError(f"{path} is wrong type; expected {kind}; got {type(result)}")
		return convert(result)


class JSONObject(JSONPathMixin, dict[str, Any]):