|
| 1 | +import re |
| 2 | +from functools import partial |
| 3 | +from typing import Any |
1 | 4 | from typing import List
|
| 5 | +from typing import Mapping |
| 6 | +from typing import Optional |
2 | 7 |
|
| 8 | +from openapi_core.schema.protocols import SuportsGetAll |
| 9 | +from openapi_core.schema.protocols import SuportsGetList |
3 | 10 |
|
4 |
| -def split(value: str, separator: str = ",") -> List[str]: |
5 |
| - return value.split(separator) |
| 11 | + |
| 12 | +def split(value: str, separator: str = ",", step: int = 1) -> List[str]: |
| 13 | + parts = value.split(separator) |
| 14 | + |
| 15 | + if step == 1: |
| 16 | + return parts |
| 17 | + |
| 18 | + result = [] |
| 19 | + for i in range(len(parts)): |
| 20 | + if i % step == 0: |
| 21 | + if i + 1 < len(parts): |
| 22 | + result.append(parts[i] + separator + parts[i + 1]) |
| 23 | + return result |
| 24 | + |
| 25 | + |
| 26 | +def delimited_loads( |
| 27 | + explode: bool, |
| 28 | + name: str, |
| 29 | + schema_type: str, |
| 30 | + location: Mapping[str, Any], |
| 31 | + delimiter: str, |
| 32 | +) -> Any: |
| 33 | + value = location[name] |
| 34 | + |
| 35 | + explode_type = (explode, schema_type) |
| 36 | + if explode_type == (False, "array"): |
| 37 | + return split(value, separator=delimiter) |
| 38 | + if explode_type == (False, "object"): |
| 39 | + return dict( |
| 40 | + map( |
| 41 | + partial(split, separator=delimiter), |
| 42 | + split(value, separator=delimiter, step=2), |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + return value |
| 47 | + |
| 48 | + |
| 49 | +def matrix_loads( |
| 50 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 51 | +) -> Any: |
| 52 | + if explode == False: |
| 53 | + m = re.match(rf"^;{name}=(.*)$", location[f";{name}"]) |
| 54 | + if m is None: |
| 55 | + raise KeyError(name) |
| 56 | + value = m.group(1) |
| 57 | + # .;color=blue |
| 58 | + if schema_type == "string": |
| 59 | + return value |
| 60 | + # ;color=blue,black,brown |
| 61 | + if schema_type == "array": |
| 62 | + return split(value) |
| 63 | + # ;color=R,100,G,200,B,150 |
| 64 | + if schema_type == "object": |
| 65 | + return dict(map(split, split(value, step=2))) |
| 66 | + else: |
| 67 | + # ;color=blue |
| 68 | + if schema_type == "string": |
| 69 | + m = re.match(rf"^;{name}=(.*)$", location[f";{name}*"]) |
| 70 | + if m is None: |
| 71 | + raise KeyError(name) |
| 72 | + value = m.group(1) |
| 73 | + return value |
| 74 | + # ;color=blue;color=black;color=brown |
| 75 | + if schema_type == "array": |
| 76 | + return re.findall(rf";{name}=([^;]*)", location[f";{name}*"]) |
| 77 | + # ;R=100;G=200;B=150 |
| 78 | + if schema_type == "object": |
| 79 | + value = location[f";{name}*"] |
| 80 | + return dict( |
| 81 | + map( |
| 82 | + partial(split, separator="="), |
| 83 | + split(value[1:], separator=";"), |
| 84 | + ) |
| 85 | + ) |
| 86 | + return location[name] |
| 87 | + |
| 88 | + |
| 89 | +def label_loads( |
| 90 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 91 | +) -> Any: |
| 92 | + if explode == False: |
| 93 | + value = location[f".{name}"] |
| 94 | + # .blue |
| 95 | + if schema_type == "string": |
| 96 | + return value[1:] |
| 97 | + # .blue,black,brown |
| 98 | + if schema_type == "array": |
| 99 | + return split(value[1:]) |
| 100 | + # .R,100,G,200,B,150 |
| 101 | + if schema_type == "object": |
| 102 | + return dict(map(split, split(value[1:], separator=",", step=2))) |
| 103 | + else: |
| 104 | + value = location[f".{name}*"] |
| 105 | + # .blue |
| 106 | + if schema_type == "string": |
| 107 | + return value[1:] |
| 108 | + # .blue.black.brown |
| 109 | + if schema_type == "array": |
| 110 | + return split(value[1:], separator=".") |
| 111 | + # .R=100.G=200.B=150 |
| 112 | + if schema_type == "object": |
| 113 | + return dict( |
| 114 | + map( |
| 115 | + partial(split, separator="="), |
| 116 | + split(value[1:], separator="."), |
| 117 | + ) |
| 118 | + ) |
| 119 | + return location[name] |
| 120 | + |
| 121 | + |
| 122 | +def form_loads( |
| 123 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 124 | +) -> Any: |
| 125 | + if schema_type == "string": |
| 126 | + return location[name] |
| 127 | + |
| 128 | + explode_type = (explode, schema_type) |
| 129 | + # color=blue,black,brown |
| 130 | + if explode_type == (False, "array"): |
| 131 | + return split(location[name], separator=",") |
| 132 | + # color=blue&color=black&color=brown |
| 133 | + elif explode_type == (True, "array"): |
| 134 | + if name not in location: |
| 135 | + raise KeyError(name) |
| 136 | + if isinstance(location, SuportsGetAll): |
| 137 | + return location.getall(name) |
| 138 | + if isinstance(location, SuportsGetList): |
| 139 | + return location.getlist(name) |
| 140 | + return location[name] |
| 141 | + |
| 142 | + value = location[name] |
| 143 | + # color=R,100,G,200,B,150 |
| 144 | + if explode_type == (False, "object"): |
| 145 | + return dict(map(split, split(value, separator=",", step=2))) |
| 146 | + # R=100&G=200&B=150 |
| 147 | + elif explode_type == (True, "object"): |
| 148 | + return dict( |
| 149 | + map(partial(split, separator="="), split(value, separator="&")) |
| 150 | + ) |
| 151 | + |
| 152 | + return value |
| 153 | + |
| 154 | + |
| 155 | +def simple_loads( |
| 156 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 157 | +) -> Any: |
| 158 | + value = location[name] |
| 159 | + if schema_type == "string": |
| 160 | + return value |
| 161 | + |
| 162 | + # blue,black,brown |
| 163 | + if schema_type == "array": |
| 164 | + return split(value, separator=",") |
| 165 | + |
| 166 | + explode_type = (explode, schema_type) |
| 167 | + # R,100,G,200,B,150 |
| 168 | + if explode_type == (False, "object"): |
| 169 | + return dict(map(split, split(value, separator=",", step=2))) |
| 170 | + # R=100,G=200,B=150 |
| 171 | + elif explode_type == (True, "object"): |
| 172 | + return dict( |
| 173 | + map(partial(split, separator="="), split(value, separator=",")) |
| 174 | + ) |
| 175 | + |
| 176 | + return value |
| 177 | + |
| 178 | + |
| 179 | +def space_delimited_loads( |
| 180 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 181 | +) -> Any: |
| 182 | + return delimited_loads( |
| 183 | + explode, name, schema_type, location, delimiter="%20" |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def pipe_delimited_loads( |
| 188 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 189 | +) -> Any: |
| 190 | + return delimited_loads(explode, name, schema_type, location, delimiter="|") |
| 191 | + |
| 192 | + |
| 193 | +def deep_object_loads( |
| 194 | + explode: bool, name: str, schema_type: str, location: Mapping[str, Any] |
| 195 | +) -> Any: |
| 196 | + explode_type = (explode, schema_type) |
| 197 | + |
| 198 | + if explode_type != (True, "object"): |
| 199 | + return location[name] |
| 200 | + |
| 201 | + keys_str = " ".join(location.keys()) |
| 202 | + if not re.search(rf"{name}\[\w+\]", keys_str): |
| 203 | + raise KeyError(name) |
| 204 | + |
| 205 | + values = {} |
| 206 | + for key, value in location.items(): |
| 207 | + # Split the key from the brackets. |
| 208 | + key_split = re.split(pattern=r"\[|\]", string=key) |
| 209 | + if key_split[0] == name: |
| 210 | + values[key_split[1]] = value |
| 211 | + return values |
0 commit comments