From e7d46de430ca922b913b9feae546c8b06d4c97b7 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Sun, 7 Jan 2024 11:51:42 +0000 Subject: [PATCH] Update for latest Gleam --- .github/workflows/test.yml | 2 +- gleam.toml | 2 +- manifest.toml | 10 +++---- src/ids/cuid.gleam | 3 +- src/ids/nanoid.gleam | 57 +++++++++++++------------------------- src/ids/ulid.gleam | 23 +++++++-------- src/ids/uuid.gleam | 6 ++-- test/ids/cuid_test.gleam | 27 ++++++++---------- test/ids/ulid_test.gleam | 14 ++++------ 9 files changed, 60 insertions(+), 84 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 604fb9c..d746d27 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,7 +15,7 @@ jobs: - uses: erlef/setup-beam@v1.15.3 with: otp-version: "25.2" - gleam-version: "0.32.2" + gleam-version: "0.33.0" rebar3-version: "3" - run: gleam format --check src test - run: gleam deps download diff --git a/gleam.toml b/gleam.toml index 09ec3c0..5c4c502 100644 --- a/gleam.toml +++ b/gleam.toml @@ -6,7 +6,7 @@ repository = { type = "github", user = "rvcas", repo = "ids" } gleam = ">= 0.32.2" [dependencies] -gleam_stdlib = "~> 0.32" +gleam_stdlib = "~> 0.34" gleam_otp = "~> 0.7" gleam_erlang = "~> 0.22" diff --git a/manifest.toml b/manifest.toml index fd342fc..748904d 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,14 +2,14 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_erlang", version = "0.23.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DA7A8E5540948DE10EB01B530869F8FF2FF6CAD8CFDA87626CE6EF63EBBF87CB" }, - { name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" }, - { name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" }, - { name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" }, + { name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" }, + { name = "gleam_otp", version = "0.9.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5FADBBEC5ECF3F8B6BE91101D432758503192AE2ADBAD5602158977341489F71" }, + { name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, ] [requirements] gleam_erlang = { version = "~> 0.22" } gleam_otp = { version = "~> 0.7" } -gleam_stdlib = { version = "~> 0.32" } +gleam_stdlib = { version = "~> 0.34" } gleeunit = { version = "~> 1.0" } diff --git a/src/ids/cuid.gleam b/src/ids/cuid.gleam index ca2be3b..f88d30e 100644 --- a/src/ids/cuid.gleam +++ b/src/ids/cuid.gleam @@ -161,7 +161,8 @@ fn get_fingerprint() -> String { let hostid = { sum + list.length(localhost) + base } % operator - id + hostid + id + + hostid |> int.to_base36() } diff --git a/src/ids/nanoid.gleam b/src/ids/nanoid.gleam index ac3d747..ab2b930 100644 --- a/src/ids/nanoid.gleam +++ b/src/ids/nanoid.gleam @@ -53,7 +53,7 @@ pub fn generate() -> String { let size: Int = default_size - let assert Ok(True) = check_nanoid_args(size, alphabet) + let assert Ok(Nil) = check_nanoid_args(size, alphabet) let mask = calculate_mask(alphabet_length) @@ -103,7 +103,7 @@ fn generate_nanoid( mask: Int, ) -> Result(BitArray, String) { case check_nanoid_args(size, alphabet) { - Ok(True) -> + Ok(Nil) -> size |> random_bytes() |> list.map(fn(x: Int) -> BitArray { @@ -120,48 +120,30 @@ fn generate_nanoid( } } -fn check_nanoid_args(size: Int, alphabet: BitArray) -> Result(Bool, String) { +fn check_nanoid_args(size: Int, alphabet: BitArray) -> Result(Nil, String) { case check_size(size) { - Ok(True) -> - case check_alphabet(alphabet) { - Ok(True) -> - True - |> Ok - Error(error) -> - error - |> Error - } - Error(error) -> - error - |> Error + Ok(Nil) -> check_alphabet(alphabet) + Error(error) -> Error(error) } } -fn check_size(size: Int) -> Result(Bool, String) { +fn check_size(size: Int) -> Result(Nil, String) { case size > 0 { - True -> - True - |> Ok - False -> { - let error: String = - "Error: The specified ID size is too small. Increase the size of the ID." - error - |> Error - } + True -> Ok(Nil) + False -> + Error( + "Error: The specified ID size is too small. Increase the size of the ID.", + ) } } -fn check_alphabet(alphabet: BitArray) -> Result(Bool, String) { +fn check_alphabet(alphabet: BitArray) -> Result(Nil, String) { case bit_array.byte_size(alphabet) > 1 { - True -> - True - |> Ok - False -> { - let error: String = - "Error: The specified alphabet size is too small. Increase the size of the alphabet." - error - |> Error - } + True -> Ok(Nil) + False -> + Error( + "Error: The specified alphabet size is too small. Increase the size of the alphabet.", + ) } } @@ -190,9 +172,8 @@ fn calculate_mask(alphabet_length: Int) -> Int { fn calculate_step(mask: Int, size: Int, alphabet_length: Int) -> Int { let step: Float = float.ceiling( - 1.6 *. int.to_float(mask) *. int.to_float(size) /. int.to_float( - alphabet_length, - ), + 1.6 *. int.to_float(mask) *. int.to_float(size) + /. int.to_float(alphabet_length), ) float.round(step) } diff --git a/src/ids/ulid.gleam b/src/ids/ulid.gleam index 2f917af..621f895 100644 --- a/src/ids/ulid.gleam +++ b/src/ids/ulid.gleam @@ -152,7 +152,7 @@ fn encode_bytes(binary: BitArray) -> String { |> result.unwrap("0") |> string.append(encode_bytes(rest)) } - <<>> -> "" + _ -> "" } } @@ -161,22 +161,19 @@ fn decode_base32(binary: String) -> Result(BitArray, Nil) { let crockford_with_index = crockford_alphabet |> string.to_graphemes() - |> list.index_map(fn(i, x) { #(x, i) }) + |> list.index_map(fn(x, i) { #(x, i) }) let bits = binary |> string.to_graphemes() - |> list.fold( - <<>>, - fn(acc, c) { - let index = - crockford_with_index - |> list.key_find(c) - |> result.unwrap(0) - - <> - }, - ) + |> list.fold(<<>>, fn(acc, c) { + let index = + crockford_with_index + |> list.key_find(c) + |> result.unwrap(0) + + <> + }) let padding = bits diff --git a/src/ids/uuid.gleam b/src/ids/uuid.gleam index d6ab24c..68f25b8 100644 --- a/src/ids/uuid.gleam +++ b/src/ids/uuid.gleam @@ -23,7 +23,7 @@ fn crypto_strong_rand_bytes(n: Int) -> BitArray /// ``` /// pub fn generate_v4() -> Result(String, String) { - let <> = + let assert <> = crypto_strong_rand_bytes(16) cast(<>) @@ -47,7 +47,7 @@ pub fn generate_v7() -> Result(String, String) { /// Generates a version 7 UUID from a given unix timestamp in milliseconds. pub fn generate_v7_from_timestamp(timestamp: Int) -> Result(String, String) { - let <<_:size(48), _:size(4), a:size(12), _:size(2), b:size(62)>> = + let assert <<_:size(48), _:size(4), a:size(12), _:size(2), b:size(62)>> = crypto_strong_rand_bytes(16) cast(<>) @@ -254,6 +254,7 @@ fn e(n: Int) -> Int { 13 -> 100 14 -> 101 15 -> 102 + _ -> 102 } } @@ -275,5 +276,6 @@ fn d(n: Int) -> Int { 100 -> 13 101 -> 14 102 -> 15 + _ -> 15 } } diff --git a/test/ids/cuid_test.gleam b/test/ids/cuid_test.gleam index 8615acc..a47bbb0 100644 --- a/test/ids/cuid_test.gleam +++ b/test/ids/cuid_test.gleam @@ -1,7 +1,7 @@ import gleeunit/should import ids/cuid import gleam/iterator.{Done, Next} -import gleam/map +import gleam/dict import gleam/pair import gleam/string @@ -61,20 +61,17 @@ fn check_collision(func: fn() -> String) -> Bool { True -> Next(element: func(), accumulator: acc + 1) } }) - |> iterator.fold( - from: #(map.new(), True), - with: fn(acc, id) { - let #(id_map, flag) = acc + |> iterator.fold(from: #(dict.new(), True), with: fn(acc, id) { + let #(id_dict, flag) = acc - case flag { - False -> acc - True -> - case map.get(id_map, id) { - Ok(_) -> #(id_map, False) - Error(_) -> #(map.insert(id_map, id, id), True) - } - } - }, - ) + case flag { + False -> acc + True -> + case dict.get(id_dict, id) { + Ok(_) -> #(id_dict, False) + Error(_) -> #(dict.insert(id_dict, id, id), True) + } + } + }) |> pair.second() } diff --git a/test/ids/ulid_test.gleam b/test/ids/ulid_test.gleam index c392bdb..4c6fbe1 100644 --- a/test/ids/ulid_test.gleam +++ b/test/ids/ulid_test.gleam @@ -30,18 +30,16 @@ pub fn from_timestamp_test() { pub fn from_parts_test() { let assert Ok(id_1) = - ulid.from_parts( - 1_696_346_659_217, - <<150, 184, 121, 192, 42, 76, 148, 57, 61, 61>>, - ) + ulid.from_parts(1_696_346_659_217, << + 150, 184, 121, 192, 42, 76, 148, 57, 61, 61, + >>) id_1 |> should.equal("01HBV27PCHJTW7KG1A9JA3JF9X") let assert Ok(id_2) = - ulid.from_parts( - 281_474_976_710_655, - <<255, 255, 255, 255, 255, 255, 255, 255, 255, 255>>, - ) + ulid.from_parts(281_474_976_710_655, << + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + >>) id_2 |> should.equal("7ZZZZZZZZZZZZZZZZZZZZZZZZZ") }