|
| 1 | +# credo:disable-for-this-file |
| 2 | +defmodule PgQuery.A_ArrayExpr do |
| 3 | + @moduledoc false |
| 4 | + defstruct elements: [], location: 0 |
| 5 | + |
| 6 | + ( |
| 7 | + ( |
| 8 | + @spec encode(struct) :: {:ok, iodata} | {:error, any} |
| 9 | + def encode(msg) do |
| 10 | + try do |
| 11 | + {:ok, encode!(msg)} |
| 12 | + rescue |
| 13 | + e in [Protox.EncodingError, Protox.RequiredFieldsError] -> {:error, e} |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + @spec encode!(struct) :: iodata | no_return |
| 18 | + def encode!(msg) do |
| 19 | + [] |> encode_elements(msg) |> encode_location(msg) |
| 20 | + end |
| 21 | + ) |
| 22 | + |
| 23 | + [] |
| 24 | + |
| 25 | + [ |
| 26 | + defp encode_elements(acc, msg) do |
| 27 | + try do |
| 28 | + case msg.elements do |
| 29 | + [] -> |
| 30 | + acc |
| 31 | + |
| 32 | + values -> |
| 33 | + [ |
| 34 | + acc, |
| 35 | + Enum.reduce(values, [], fn value, acc -> |
| 36 | + [acc, "\n", Protox.Encode.encode_message(value)] |
| 37 | + end) |
| 38 | + ] |
| 39 | + end |
| 40 | + rescue |
| 41 | + ArgumentError -> |
| 42 | + reraise Protox.EncodingError.new(:elements, "invalid field value"), __STACKTRACE__ |
| 43 | + end |
| 44 | + end, |
| 45 | + defp encode_location(acc, msg) do |
| 46 | + try do |
| 47 | + if msg.location == 0 do |
| 48 | + acc |
| 49 | + else |
| 50 | + [acc, "\x10", Protox.Encode.encode_int32(msg.location)] |
| 51 | + end |
| 52 | + rescue |
| 53 | + ArgumentError -> |
| 54 | + reraise Protox.EncodingError.new(:location, "invalid field value"), __STACKTRACE__ |
| 55 | + end |
| 56 | + end |
| 57 | + ] |
| 58 | + |
| 59 | + [] |
| 60 | + ) |
| 61 | + |
| 62 | + ( |
| 63 | + ( |
| 64 | + @spec decode(binary) :: {:ok, struct} | {:error, any} |
| 65 | + def decode(bytes) do |
| 66 | + try do |
| 67 | + {:ok, decode!(bytes)} |
| 68 | + rescue |
| 69 | + e in [Protox.DecodingError, Protox.IllegalTagError, Protox.RequiredFieldsError] -> |
| 70 | + {:error, e} |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + ( |
| 75 | + @spec decode!(binary) :: struct | no_return |
| 76 | + def decode!(bytes) do |
| 77 | + parse_key_value(bytes, struct(PgQuery.A_ArrayExpr)) |
| 78 | + end |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + ( |
| 83 | + @spec parse_key_value(binary, struct) :: struct |
| 84 | + defp parse_key_value(<<>>, msg) do |
| 85 | + msg |
| 86 | + end |
| 87 | + |
| 88 | + defp parse_key_value(bytes, msg) do |
| 89 | + {field, rest} = |
| 90 | + case Protox.Decode.parse_key(bytes) do |
| 91 | + {0, _, _} -> |
| 92 | + raise %Protox.IllegalTagError{} |
| 93 | + |
| 94 | + {1, _, bytes} -> |
| 95 | + {len, bytes} = Protox.Varint.decode(bytes) |
| 96 | + {delimited, rest} = Protox.Decode.parse_delimited(bytes, len) |
| 97 | + {[elements: msg.elements ++ [PgQuery.Node.decode!(delimited)]], rest} |
| 98 | + |
| 99 | + {2, _, bytes} -> |
| 100 | + {value, rest} = Protox.Decode.parse_int32(bytes) |
| 101 | + {[location: value], rest} |
| 102 | + |
| 103 | + {tag, wire_type, rest} -> |
| 104 | + {_, rest} = Protox.Decode.parse_unknown(tag, wire_type, rest) |
| 105 | + {[], rest} |
| 106 | + end |
| 107 | + |
| 108 | + msg_updated = struct(msg, field) |
| 109 | + parse_key_value(rest, msg_updated) |
| 110 | + end |
| 111 | + ) |
| 112 | + |
| 113 | + [] |
| 114 | + ) |
| 115 | + |
| 116 | + ( |
| 117 | + @spec json_decode(iodata(), keyword()) :: {:ok, struct()} | {:error, any()} |
| 118 | + def json_decode(input, opts \\ []) do |
| 119 | + try do |
| 120 | + {:ok, json_decode!(input, opts)} |
| 121 | + rescue |
| 122 | + e in Protox.JsonDecodingError -> {:error, e} |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + @spec json_decode!(iodata(), keyword()) :: struct() | no_return() |
| 127 | + def json_decode!(input, opts \\ []) do |
| 128 | + {json_library_wrapper, json_library} = Protox.JsonLibrary.get_library(opts, :decode) |
| 129 | + |
| 130 | + Protox.JsonDecode.decode!( |
| 131 | + input, |
| 132 | + PgQuery.A_ArrayExpr, |
| 133 | + &json_library_wrapper.decode!(json_library, &1) |
| 134 | + ) |
| 135 | + end |
| 136 | + |
| 137 | + @spec json_encode(struct(), keyword()) :: {:ok, iodata()} | {:error, any()} |
| 138 | + def json_encode(msg, opts \\ []) do |
| 139 | + try do |
| 140 | + {:ok, json_encode!(msg, opts)} |
| 141 | + rescue |
| 142 | + e in Protox.JsonEncodingError -> {:error, e} |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + @spec json_encode!(struct(), keyword()) :: iodata() | no_return() |
| 147 | + def json_encode!(msg, opts \\ []) do |
| 148 | + {json_library_wrapper, json_library} = Protox.JsonLibrary.get_library(opts, :encode) |
| 149 | + Protox.JsonEncode.encode!(msg, &json_library_wrapper.encode!(json_library, &1)) |
| 150 | + end |
| 151 | + ) |
| 152 | + |
| 153 | + ( |
| 154 | + @deprecated "Use fields_defs()/0 instead" |
| 155 | + @spec defs() :: %{ |
| 156 | + required(non_neg_integer) => {atom, Protox.Types.kind(), Protox.Types.type()} |
| 157 | + } |
| 158 | + def defs() do |
| 159 | + %{ |
| 160 | + 1 => {:elements, :unpacked, {:message, PgQuery.Node}}, |
| 161 | + 2 => {:location, {:scalar, 0}, :int32} |
| 162 | + } |
| 163 | + end |
| 164 | + |
| 165 | + @deprecated "Use fields_defs()/0 instead" |
| 166 | + @spec defs_by_name() :: %{ |
| 167 | + required(atom) => {non_neg_integer, Protox.Types.kind(), Protox.Types.type()} |
| 168 | + } |
| 169 | + def defs_by_name() do |
| 170 | + %{elements: {1, :unpacked, {:message, PgQuery.Node}}, location: {2, {:scalar, 0}, :int32}} |
| 171 | + end |
| 172 | + ) |
| 173 | + |
| 174 | + ( |
| 175 | + @spec fields_defs() :: list(Protox.Field.t()) |
| 176 | + def fields_defs() do |
| 177 | + [ |
| 178 | + %{ |
| 179 | + __struct__: Protox.Field, |
| 180 | + json_name: "elements", |
| 181 | + kind: :unpacked, |
| 182 | + label: :repeated, |
| 183 | + name: :elements, |
| 184 | + tag: 1, |
| 185 | + type: {:message, PgQuery.Node} |
| 186 | + }, |
| 187 | + %{ |
| 188 | + __struct__: Protox.Field, |
| 189 | + json_name: "location", |
| 190 | + kind: {:scalar, 0}, |
| 191 | + label: :optional, |
| 192 | + name: :location, |
| 193 | + tag: 2, |
| 194 | + type: :int32 |
| 195 | + } |
| 196 | + ] |
| 197 | + end |
| 198 | + |
| 199 | + [ |
| 200 | + @spec(field_def(atom) :: {:ok, Protox.Field.t()} | {:error, :no_such_field}), |
| 201 | + ( |
| 202 | + def field_def(:elements) do |
| 203 | + {:ok, |
| 204 | + %{ |
| 205 | + __struct__: Protox.Field, |
| 206 | + json_name: "elements", |
| 207 | + kind: :unpacked, |
| 208 | + label: :repeated, |
| 209 | + name: :elements, |
| 210 | + tag: 1, |
| 211 | + type: {:message, PgQuery.Node} |
| 212 | + }} |
| 213 | + end |
| 214 | + |
| 215 | + def field_def("elements") do |
| 216 | + {:ok, |
| 217 | + %{ |
| 218 | + __struct__: Protox.Field, |
| 219 | + json_name: "elements", |
| 220 | + kind: :unpacked, |
| 221 | + label: :repeated, |
| 222 | + name: :elements, |
| 223 | + tag: 1, |
| 224 | + type: {:message, PgQuery.Node} |
| 225 | + }} |
| 226 | + end |
| 227 | + |
| 228 | + [] |
| 229 | + ), |
| 230 | + ( |
| 231 | + def field_def(:location) do |
| 232 | + {:ok, |
| 233 | + %{ |
| 234 | + __struct__: Protox.Field, |
| 235 | + json_name: "location", |
| 236 | + kind: {:scalar, 0}, |
| 237 | + label: :optional, |
| 238 | + name: :location, |
| 239 | + tag: 2, |
| 240 | + type: :int32 |
| 241 | + }} |
| 242 | + end |
| 243 | + |
| 244 | + def field_def("location") do |
| 245 | + {:ok, |
| 246 | + %{ |
| 247 | + __struct__: Protox.Field, |
| 248 | + json_name: "location", |
| 249 | + kind: {:scalar, 0}, |
| 250 | + label: :optional, |
| 251 | + name: :location, |
| 252 | + tag: 2, |
| 253 | + type: :int32 |
| 254 | + }} |
| 255 | + end |
| 256 | + |
| 257 | + [] |
| 258 | + ), |
| 259 | + def field_def(_) do |
| 260 | + {:error, :no_such_field} |
| 261 | + end |
| 262 | + ] |
| 263 | + ) |
| 264 | + |
| 265 | + [] |
| 266 | + |
| 267 | + ( |
| 268 | + @spec required_fields() :: [] |
| 269 | + def required_fields() do |
| 270 | + [] |
| 271 | + end |
| 272 | + ) |
| 273 | + |
| 274 | + ( |
| 275 | + @spec syntax() :: atom() |
| 276 | + def syntax() do |
| 277 | + :proto3 |
| 278 | + end |
| 279 | + ) |
| 280 | + |
| 281 | + [ |
| 282 | + @spec(default(atom) :: {:ok, boolean | integer | String.t() | float} | {:error, atom}), |
| 283 | + def default(:elements) do |
| 284 | + {:error, :no_default_value} |
| 285 | + end, |
| 286 | + def default(:location) do |
| 287 | + {:ok, 0} |
| 288 | + end, |
| 289 | + def default(_) do |
| 290 | + {:error, :no_such_field} |
| 291 | + end |
| 292 | + ] |
| 293 | + |
| 294 | + ( |
| 295 | + @spec file_options() :: nil |
| 296 | + def file_options() do |
| 297 | + nil |
| 298 | + end |
| 299 | + ) |
| 300 | +end |
0 commit comments