Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ on:
pull_request:

env:
elixir_version: "1.12"
otp_version: "24.2"
elixir_version: "1.20"
otp_version: "28.0"

jobs:
test-dialyzer:
name: Check Dialyzer
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
env:
MIX_ENV: ci
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v6
- uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
- uses: actions/cache@v1
- uses: actions/cache@v4
with:
path: _build
key: ${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-_build-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-_build-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
- uses: actions/cache@v1
- uses: actions/cache@v4
with:
path: dialyzer_cache
key: ${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-dialyzer_cache-${{ github.sha }}
Expand All @@ -71,19 +71,19 @@ jobs:

test-coverage:
name: Build and Test
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
# Wait for Dialyzer to give it a go before building
needs:
- test-dialyzer
env:
MIX_ENV: test
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
- uses: actions/checkout@v6
- uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
- uses: actions/cache@v1
- uses: actions/cache@v4
with:
path: _build
key: ${{ runner.os }}-${{ env.elixir_version }}-${{ env.otp_version }}-_build-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}-${{ github.sha }}
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config
28 changes: 6 additions & 22 deletions lib/cyanide.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule Cyanide do

defp parse_value(0x2, map, key, <<string_size::little-32, string_and_rest::binary>>) do
with no_zero_size when no_zero_size >= 0 <- string_size - 1,
<<value::binary-size(no_zero_size), 0::8, rest::binary>> <- string_and_rest do
<<value::binary-size(^no_zero_size), 0::8, rest::binary>> <- string_and_rest do
Map.put(map, key, value)
|> parse_doc_bytes(rest)
else
Expand All @@ -113,7 +113,7 @@ defmodule Cyanide do

defp parse_value(0x3, map, key, <<subdoc_size::little-32, subdoc_and_rest::binary>>) do
with the_size when the_size >= 1 <- subdoc_size - 4,
<<subdocument::binary-size(the_size), rest::binary>> <- subdoc_and_rest do
<<subdocument::binary-size(^the_size), rest::binary>> <- subdoc_and_rest do
Map.put(map, key, parse_doc_bytes(%{}, subdocument))
|> parse_doc_bytes(rest)
else
Expand All @@ -129,7 +129,7 @@ defmodule Cyanide do

defp parse_value(0x4, map, key, <<subdoc_size::little-32, subdoc_and_rest::binary>>) do
with the_size when the_size >= 1 <- subdoc_size - 4,
<<subdocument::binary-size(the_size), rest::binary>> <- subdoc_and_rest,
<<subdocument::binary-size(^the_size), rest::binary>> <- subdoc_and_rest,
array_subdoc when is_map(array_subdoc) <- parse_doc_bytes(%{}, subdocument) do
array_max_index = map_size(array_subdoc) - 1

Expand All @@ -144,7 +144,7 @@ defmodule Cyanide do
end

with values_list when is_list(values_list) <-
Enum.reduce_while(array_max_index..0, [], map_array_to_list) do
Enum.reduce_while(array_max_index..0//-1, [], map_array_to_list) do
Map.put(map, key, values_list)
|> parse_doc_bytes(rest)
end
Expand All @@ -156,7 +156,7 @@ defmodule Cyanide do

defp parse_value(0x5, map, key, <<subdoc_size::little-32, subtype::8, subdoc_and_rest::binary>>) do
with the_size when the_size >= 0 <- subdoc_size,
<<subdocument::binary-size(the_size), rest::binary>> <- subdoc_and_rest,
<<subdocument::binary-size(^the_size), rest::binary>> <- subdoc_and_rest,
{:ok, subtype_atom} <- Binary.cast_subtype(subtype) do
Map.put(map, key, %Binary{subtype: subtype_atom, data: subdocument})
|> parse_doc_bytes(rest)
Expand Down Expand Up @@ -211,7 +211,7 @@ defmodule Cyanide do

defp split_cstring(blob, n, max_len) when n < max_len do
case blob do
<<cstring::binary-size(n), 0::8, rest::binary>> ->
<<cstring::binary-size(^n), 0::8, rest::binary>> ->
{cstring, rest}

_ ->
Expand Down Expand Up @@ -289,22 +289,6 @@ defmodule Cyanide do
[<<0x2>>, key_string, <<0, string_size::signed-little-32>>, value | <<0>>]
end

defp encode_value(key_string, %Binary{subtype: subtype, data: data}) do
subtype_int =
case subtype do
:generic -> 0x00
:function -> 0x01
:old_binary -> 0x02
:old_uuid -> 0x03
:uuid -> 0x04
:md5 -> 0x05
:encrypted_bson -> 0x06
ud when is_integer(ud) and ud >= 0x80 and ud <= 0xFF -> ud
end

encode_value(key_string, {subtype_int, data})
end

defp encode_value(key_string, %DateTime{} = value) do
timestamp_ms = DateTime.to_unix(value, :millisecond)
[<<0x9>>, key_string, <<0>> | <<timestamp_ms::signed-little-64>>]
Expand Down
20 changes: 20 additions & 0 deletions lib/cyanide/binary.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ defmodule Cyanide.Binary do
_ -> :error
end
end

defimpl Cyanide.Encoder do
alias Cyanide.Binary

def encode(%Binary{subtype: subtype, data: data}) do
subtype_int =
case subtype do
:generic -> 0x00
:function -> 0x01
:old_binary -> 0x02
:old_uuid -> 0x03
:uuid -> 0x04
:md5 -> 0x05
:encrypted_bson -> 0x06
ud when is_integer(ud) and ud >= 0x80 and ud <= 0xFF -> ud
end

{subtype_int, data}
end
end
end
7 changes: 6 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ defmodule Cyanide.MixProject do
deps: deps(),
package: package(),
source_url: "https://github.com/secomind/cyanide",
test_coverage: [tool: ExCoveralls],
test_coverage: [tool: ExCoveralls]
]
end

def cli do
[
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
Expand Down
20 changes: 20 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%{
"certifi": {:hex, :certifi, "2.17.0", "835748414307e15e05b17d0e518190228ce648b08d569a5cc93a85a40f3e5c9b", [:rebar3], [], "hexpm", "8122798a17f0293c80daada25d0f81c7f4d708c73fef782c7c9b1950e26e4d21"},
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm", "6c32a70ed5d452c6650916555b1f96c79af5fc4bf286997f8b15f213de786f73"},
"earmark_parser": {:hex, :earmark_parser, "1.4.46", "67607a0532e810c6f630a515c548d0b24949643f168cc556303bee4cf96105c7", [:mix], [], "hexpm", "9c44636e8a1c68c62f526b2dcd85d941dbbcee7ab82cf64ba06ce28bef8e89f5"},
"ex_doc": {:hex, :ex_doc, "0.40.3", "4a972ffe64bc07dc605af487e98fc19b72a4185f55ca031b94c0552d6071c1d9", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "2756e357742fecd9749b489b85d67c9ce99c465f2e75728d9e6dc8d704b973de"},
"excoveralls": {:hex, :excoveralls, "0.9.2", "299ea4903be7cb2959af0f919d258af116736ca8d507f86c12ef2184698e21a0", [:mix], [{:hackney, ">= 0.12.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "9f063f68cdc80f1f3fc03e51e39475b1d6f06e9315fcb367cd7ca822f4c979cb"},
"h2": {:hex, :h2, "0.12.0", "f393539ee2728f8118fb2024b6d5f3e2c45e40ceb31b18b4e9bf5e50d028f80f", [:rebar3], [], "hexpm", "beaafc93c54cdc5d623247334d3970cdf4bc66b6b8b296b74ba1d7c7513c3dfc"},
"hackney": {:hex, :hackney, "4.7.4", "8fe2ddaa3ca27de99d68e682d72b66d07d2331da680f77c8000580a0122c69e6", [:rebar3], [{:certifi, "~> 2.17.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:h2, "~> 0.12.0", [hex: :h2, repo: "hexpm", optional: false]}, {:idna, "~> 7.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.5", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.2", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:webtransport, "~> 0.4.5", [hex: :webtransport, repo: "hexpm", optional: false]}], "hexpm", "d07d7e1358353ab6cc75132f058c155287f3e013d43f709fbb79d79eeab98195"},
"idna": {:hex, :idna, "7.1.0", "1067a13043538129602d2f2ce6899d8713125c7d19734aa557ce2e3ea55bd4f1", [:rebar3], [], "hexpm", "6ae959a025bf36df61a8cab8508d9654891b5426a84c44d82deaffd6ddf8c71f"},
"jason": {:hex, :jason, "1.4.5", "2e3a008590b0b8d7388c20293e9dcc9cf3e5d642fd2a114e4cbbb52e595d940a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0 or ~> 3.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "b0c823996102bcd0239b3c2444eb00409b72f6a140c1950bc8b457d836b30684"},
"makeup": {:hex, :makeup, "1.2.2", "882d46dc0905e9ff7abf2aab61a7e6b3dcc555533977d8a23b06019e6c89ac94", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "9a1a24e5b343b8ae16abea0822c10a6f75da27af7fa802ada5251f7579bfccfa"},
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
"makeup_erlang": {:hex, :makeup_erlang, "1.1.0", "835f7e60792e08824cda445639555d7bf1bbbddb1b60b306e33cb6f6db24dc74", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "1cd6780fb1dd1a03979abaed0fe82712b0625118fd5257d3ebbf73f960c73c3c"},
"mimerl": {:hex, :mimerl, "1.5.0", "f35aca6f23242339b3666e0ac0702379e362b469d0aea167f6cc713547e777ed", [:rebar3], [], "hexpm", "db648ce065bae14ea84ca8b5dd123f42f49417cef693541110bf6f9e9be9ecc4"},
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
"parse_trans": {:hex, :parse_trans, "3.4.2", "c352ddc1a0d5e54f9b1654d45f9c432eef76f9cea371c55ddff769ef688fdb74", [:rebar3], [], "hexpm", "4c25347de3b7c35732d32e69ab43d1ceee0beae3f3b3ade1b59cbd3dd224d9ca"},
"quic": {:hex, :quic, "1.8.0", "7b074fc6f8a13b4a81e4e11254bf061db61c5c07b4366d568a79a38c5e8bb5f3", [:rebar3], [], "hexpm", "6b1f8f08a45412ac503bc6771efb8edc696bb14079bc48d2ca960b92933da8a4"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"webtransport": {:hex, :webtransport, "0.4.5", "0e387202bbe707389fe81373ef8c56faa9d5aa321bb4800fa4765ee7c1399785", [:rebar3], [{:h2, "~> 0.12", [hex: :h2, repo: "hexpm", optional: false]}, {:quic, "~> 1.8.0", [hex: :quic, repo: "hexpm", optional: false]}], "hexpm", "bcb512239e48e551d5bd5c667312a9a7de4f29b89d84b1d33c5af44e1f3f730d"},
}
Loading