Skip to content

Commit 4ee808a

Browse files
committed
refactor: Mix format
1 parent 1ef27ba commit 4ee808a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2825
-2529
lines changed

.formatter.exs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
4+
line_length: 250,
5+
locals_without_parens: [
6+
# MongoDB
7+
after_load: :*,
8+
before_dump: :*,
9+
attribute: :*,
10+
collection: :*,
11+
embeds_many: :*,
12+
# Test
13+
## Assertions
14+
assert_receive_event: :*,
15+
refute_receive_event: :*
16+
]
417
]

lib/bson.ex

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@ defmodule BSON do
33
Functions for encoding and decoding BSON documents.
44
"""
55

6-
@type t :: document | String.t | atom | number | boolean | BSON.Binary.t |
7-
BSON.ObjectId.t | BSON.Regex.t |
8-
BSON.JavaScript.t | BSON.Timestamp.t | BSON.LongNumber.t | [t]
9-
@type document :: %{atom => BSON.t} | %{String.t => BSON.t} |
10-
[{atom, BSON.t}] | [{String.t, BSON.t}]
6+
@type t :: document | String.t() | atom | number | boolean | BSON.Binary.t() | BSON.ObjectId.t() | BSON.Regex.t() | BSON.JavaScript.t() | BSON.Timestamp.t() | BSON.LongNumber.t() | [t]
7+
@type document :: %{atom => BSON.t()} | %{String.t() => BSON.t()} | [{atom, BSON.t()}] | [{String.t(), BSON.t()}]
118

129
@doc """
1310
Encode a BSON document to `iodata`.
1411
"""
1512
@spec encode(document) :: iodata
1613
def encode(map) when is_map(map) do
1714
case Map.has_key?(map, :__struct__) do
18-
true -> BSON.Encoder.encode(Map.to_list(map))
19-
false -> BSON.Encoder.encode(map)
15+
true -> BSON.Encoder.encode(Map.to_list(map))
16+
false -> BSON.Encoder.encode(map)
2017
end
2118
end
2219

23-
def encode([{_, _}|_] = keyword) do
20+
def encode([{_, _} | _] = keyword) do
2421
BSON.Encoder.encode(keyword)
2522
end
2623

@@ -30,6 +27,6 @@ defmodule BSON do
3027
@spec decode(iodata) :: document
3128
def decode(iodata) do
3229
IO.iodata_to_binary(iodata)
33-
|> BSON.Decoder.decode
30+
|> BSON.Decoder.decode()
3431
end
3532
end

lib/bson/decimal128.ex

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
defmodule BSON.Decimal128 do
2-
32
@moduledoc """
43
see https://en.wikipedia.org/wiki/Decimal128_floating-point_format
54
"""
65

76
use Bitwise
87

98
@signed_bit_mask 1 <<< 63
10-
@combination_mask 0x1f
9+
@combination_mask 0x1F
1110
@combintation_infinity 30
1211
@combintation_nan 31
13-
@exponent_mask 0x3fff
12+
@exponent_mask 0x3FFF
1413
@exponent_bias 6176
1514
@max_exponent 6111
1615
@min_exponent -6176
17-
@s_nan_mask 0x1 <<< 57
18-
@significand_mask ((0x1 <<< 49)-1)
19-
@low_mask 0xffffffffffffffff
16+
@s_nan_mask 0x1 <<< 57
17+
@significand_mask (0x1 <<< 49) - 1
18+
@low_mask 0xFFFFFFFFFFFFFFFF
2019

2120
def decode(<<_::little-64, high::little-64>> = bits) do
22-
is_negative = (high &&& @signed_bit_mask) == (@signed_bit_mask)
23-
combination = (high >>> 58 &&& @combination_mask)
21+
is_negative = (high &&& @signed_bit_mask) == @signed_bit_mask
22+
combination = high >>> 58 &&& @combination_mask
2423
two_highest_bits_set = combination >>> 3 == 3
2524
is_infinity = two_highest_bits_set && combination == @combintation_infinity
26-
is_nan = case {(two_highest_bits_set && combination) == @combintation_nan, (high &&& @s_nan_mask) == @s_nan_mask} do
27-
{true, true} -> :sNan
28-
{true, false} -> :qNan
29-
_ -> false
30-
end
25+
26+
is_nan =
27+
case {(two_highest_bits_set && combination) == @combintation_nan, (high &&& @s_nan_mask) == @s_nan_mask} do
28+
{true, true} -> :sNan
29+
{true, false} -> :qNan
30+
_ -> false
31+
end
3132

3233
exponent = exponent(high, two_highest_bits_set)
3334

3435
value(
35-
%{is_negative: is_negative,
36-
is_infinity: is_infinity,
37-
is_nan: is_nan,
38-
two_highest_bits_set: two_highest_bits_set},
36+
%{is_negative: is_negative, is_infinity: is_infinity, is_nan: is_nan, two_highest_bits_set: two_highest_bits_set},
3937
coef(bits),
4038
exponent
4139
)
@@ -48,73 +46,88 @@ defmodule BSON.Decimal128 do
4846
"""
4947
def encode(%Decimal{sign: -1, coef: :inf}) do
5048
low = 0
51-
high = 0x3e <<< 58
49+
high = 0x3E <<< 58
5250
<<low::little-64, high::little-64>>
5351
end
52+
5453
def encode(%Decimal{coef: :inf}) do
5554
low = 0
56-
high = 0x1e <<< 58
55+
high = 0x1E <<< 58
5756
<<low::little-64, high::little-64>>
5857
end
58+
5959
def encode(%Decimal{coef: :qNaN}) do
6060
low = 0
61-
high = 0x1f <<< 58
61+
high = 0x1F <<< 58
6262
<<low::little-64, high::little-64>>
6363
end
64+
6465
def encode(%Decimal{coef: :sNaN}) do
6566
low = 0
66-
high = 0x3f <<< 57
67+
high = 0x3F <<< 57
6768
<<low::little-64, high::little-64>>
6869
end
70+
6971
def encode(%Decimal{sign: sign, coef: significand, exp: exponent}) when exponent >= @min_exponent and exponent <= @max_exponent do
7072
biasedExponent = exponent + @exponent_bias
71-
low = significand &&& @low_mask
72-
high = (significand >>> 64) &&& @significand_mask ## mask max significand
73+
low = significand &&& @low_mask
74+
## mask max significand
75+
high = significand >>> 64 &&& @significand_mask
7376
high = bor(high, biasedExponent <<< 49)
74-
high = case sign do
75-
1 -> high
76-
_ -> bor(high, @signed_bit_mask)
77-
end
77+
78+
high =
79+
case sign do
80+
1 -> high
81+
_ -> bor(high, @signed_bit_mask)
82+
end
7883

7984
<<low::little-64, high::little-64>>
8085
end
86+
8187
def encode(%Decimal{exp: exponent}) do
8288
message = "Exponent is out of range for Decimal128 encoding, #{exponent}"
8389
raise ArgumentError, message
8490
end
8591

8692
defp exponent(high, _two_highest_bits_set = true) do
87-
biased_exponent = (high >>> 47) &&& @exponent_mask
93+
biased_exponent = high >>> 47 &&& @exponent_mask
8894
biased_exponent - @exponent_bias
8995
end
96+
9097
defp exponent(high, _two_highest_bits_not_set) do
91-
biased_exponent = (high >>> 49) &&& @exponent_mask
98+
biased_exponent = high >>> 49 &&& @exponent_mask
9299
biased_exponent - @exponent_bias
93100
end
94101

95102
defp value(%{is_negative: true, is_infinity: true}, _, _) do
96103
%Decimal{sign: -1, coef: :inf}
97104
end
105+
98106
defp value(%{is_negative: false, is_infinity: true}, _, _) do
99107
%Decimal{coef: :inf}
100108
end
109+
101110
defp value(%{is_nan: :qNan}, _, _) do
102111
%Decimal{coef: :qNaN}
103112
end
113+
104114
defp value(%{is_nan: :sNan}, _, _) do
105115
%Decimal{coef: :sNaN}
106116
end
117+
107118
defp value(%{two_highest_bits_set: true}, _, _) do
108119
%Decimal{sign: 0, coef: 0, exp: 0}
109120
end
121+
110122
defp value(%{is_negative: true}, coef, exponent) do
111123
%Decimal{sign: -1, coef: coef, exp: exponent}
112124
end
125+
113126
defp value(_, coef, exponent) do
114127
%Decimal{coef: coef, exp: exponent}
115128
end
116129

117130
defp coef(<<low::little-64, high::little-64>>) do
118-
bor((high &&& 0x1ffffffffffff) <<< 64, low)
131+
bor((high &&& 0x1FFFFFFFFFFFF) <<< 64, low)
119132
end
120-
end
133+
end

lib/bson/decoder.ex

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ defmodule BSON.Decoder do
1010

1111
def documents(binary), do: documents(binary, [])
1212
def documents("", acc), do: Enum.reverse(acc)
13+
1314
def documents(binary, acc) do
1415
{doc, rest} = document(binary)
15-
documents(rest, [doc|acc])
16+
documents(rest, [doc | acc])
1617
end
1718

1819
defp type(@type_float, <<0, 0, 0, 0, 0, 0, 240::little-integer-size(8), 127::little-integer-size(8), rest::binary>>) do
@@ -141,11 +142,11 @@ defmodule BSON.Decoder do
141142
{key, rest} = cstring(rest)
142143
{value, rest} = type(type, rest)
143144

144-
doc_fields(rest, [{key, value}|acc])
145+
doc_fields(rest, [{key, value} | acc])
145146
end
146147

147148
defp doc_fields("", acc) do
148-
acc |> Enum.reverse |> Enum.into(%{})
149+
acc |> Enum.reverse() |> Enum.into(%{})
149150
end
150151

151152
defp list(<<size::int32, rest::binary>>) do
@@ -160,11 +161,11 @@ defmodule BSON.Decoder do
160161
{^ix_string, rest} = cstring(rest)
161162
{value, rest} = type(type, rest)
162163

163-
list_elems(rest, ix + 1, [value|acc])
164+
list_elems(rest, ix + 1, [value | acc])
164165
end
165166

166167
defp list_elems("", _ix, acc) do
167-
acc |> Enum.reverse
168+
acc |> Enum.reverse()
168169
end
169170

170171
defp cstring(binary) do

lib/bson/encoder.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ defmodule BSON.Encoder do
115115

116116
{value, type} =
117117
case Mongo.Encoder.impl_for(value) do
118-
nil -> {encode(value), type(value)}
118+
nil ->
119+
{encode(value), type(value)}
120+
119121
_ ->
120122
new_value = value |> Mongo.Encoder.encode()
121123
{encode(new_value), type(new_value)}

0 commit comments

Comments
 (0)