diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 12f49ac..ab73877 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,8 +14,8 @@ jobs: - uses: actions/checkout@v3.2.0 - uses: erlef/setup-beam@v1.15.2 with: - otp-version: "25.2" - gleam-version: "0.26.2" + otp-version: "26.0" + gleam-version: "0.34.1" rebar3-version: "3" # elixir-version: "1.14.2" - run: gleam format --check src test diff --git a/gleam.toml b/gleam.toml index 4fc516a..467675d 100644 --- a/gleam.toml +++ b/gleam.toml @@ -14,4 +14,4 @@ gleam_stdlib = "~> 0.31" gleam_json = "~> 0.5" [dev-dependencies] -gleeunit = "~> 0.10" +gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 34be222..bfa9868 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,13 +2,13 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, - { name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" }, - { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, + { name = "gleam_json", version = "0.7.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "CB405BD93A8828BCD870463DE29375E7B2D252D9D124C109E5B618AAC00B86FC" }, + { name = "gleam_stdlib", version = "0.35.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5443EEB74708454B65650FEBBB1EF5175057D1DEC62AEA9D7C6D96F41DA79152" }, + { name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" }, { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, ] [requirements] gleam_json = { version = "~> 0.5" } gleam_stdlib = { version = "~> 0.31" } -gleeunit = { version = "~> 0.10" } +gleeunit = { version = "~> 1.0" } diff --git a/src/gleam_community/codec.gleam b/src/gleam_community/codec.gleam index 711ab18..87b67d3 100644 --- a/src/gleam_community/codec.gleam +++ b/src/gleam_community/codec.gleam @@ -1,10 +1,12 @@ // IMPORTS --------------------------------------------------------------------- -import gleam/dynamic.{type DecodeError as DynamicError, DecodeError as DynamicError, type Dynamic} +import gleam/dynamic.{ + type DecodeError as DynamicError, type Dynamic, DecodeError as DynamicError, +} import gleam/function import gleam/json.{type DecodeError as JsonError, type Json} import gleam/list -import gleam/map.{type Map} +import gleam/dict.{type Dict} import gleam/option.{type Option} import gleam/pair import gleam/result @@ -32,7 +34,7 @@ import gleam/int /// /// Importantly, the codec API means our encoders and decoders stay _isomorphic_. /// That is, we can guarantee that the conversions to JSON and from `Dynamic` are -/// always in sync. +/// always in sync. /// pub opaque type Codec(a) { Codec( @@ -44,7 +46,7 @@ pub opaque type Codec(a) { /// /// pub opaque type Custom(a) { - Custom(encode: fn(a) -> Json, decode: Map(String, Decoder(a))) + Custom(encode: fn(a) -> Json, decode: Dict(String, Decoder(a))) } /// @@ -125,23 +127,23 @@ pub fn optional(codec: Codec(a)) -> Codec(Option(a)) { /// /// -pub fn object(codec: Codec(a)) -> Codec(Map(String, a)) { - let encode = fn(map) { - map - |> map.to_list +pub fn object(codec: Codec(a)) -> Codec(Dict(String, a)) { + let encode = fn(dict) { + dict + |> dict.to_list |> list.map(pair.map_second(_, codec.encode)) |> json.object } - let decode = dynamic.map(dynamic.string, codec.decode) + let decode = dynamic.dict(dynamic.string, codec.decode) Codec(encode, decode) } /// /// -pub fn dictionary(key_codec: Codec(k), val_codec: Codec(v)) -> Codec(Map(k, v)) { +pub fn dictionary(key_codec: Codec(k), val_codec: Codec(v)) -> Codec(Dict(k, v)) { list(tuple2(key_codec, val_codec)) - |> map(map.to_list, map.from_list) + |> dict(dict.to_list, dict.from_list) } /// @@ -194,18 +196,15 @@ pub fn tuple3( // CONSTRUCTORS: CUSTOM TYPES -------------------------------------------------- pub fn custom(builder: Custom(a)) -> Codec(a) { - Codec( - encode: builder.encode, - decode: fn(dyn) { - let decode_tag = dynamic.field("$", dynamic.string) - use tag <- result.then(decode_tag(dyn)) + Codec(encode: builder.encode, decode: fn(dyn) { + let decode_tag = dynamic.field("$", dynamic.string) + use tag <- result.then(decode_tag(dyn)) - case map.get(builder.decode, tag) { - Ok(decoder) -> decoder(dyn) - Error(_) -> Error([DynamicError("Unknown tag", tag, ["$"])]) - } - }, - ) + case dict.get(builder.decode, tag) { + Ok(decoder) -> decoder(dyn) + Error(_) -> Error([DynamicError("Unknown tag", tag, ["$"])]) + } + }) } pub opaque type Variant(a) { @@ -241,7 +240,7 @@ pub fn make_variant(_: Int) -> List(Decoder(Dynamic)) { } pub fn make_custom(encode: fn(a) -> Json) -> Custom(a) { - Custom(encode, decode: map.new()) + Custom(encode, decode: dict.new()) } pub fn variant0( @@ -253,7 +252,7 @@ pub fn variant0( let decoder = fn(_) { Ok(constructor) } let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant1( @@ -268,7 +267,7 @@ pub fn variant1( let decoder = dynamic.decode1(constructor, dynamic.field("0", codec_a.decode)) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant2( @@ -293,7 +292,7 @@ pub fn variant2( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant3( @@ -321,7 +320,7 @@ pub fn variant3( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant4( @@ -352,7 +351,7 @@ pub fn variant4( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant5( @@ -386,7 +385,7 @@ pub fn variant5( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant6( @@ -423,7 +422,7 @@ pub fn variant6( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant7( @@ -463,7 +462,7 @@ pub fn variant7( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } pub fn variant8( @@ -506,7 +505,7 @@ pub fn variant8( ) let builder = builder(encoder) - Custom(..builder, decode: map.insert(builder.decode, tag, decoder)) + Custom(..builder, decode: dict.insert(builder.decode, tag, decoder)) } // QUERIES --------------------------------------------------------------------- @@ -546,7 +545,7 @@ pub fn then( /// /// -pub fn map(codec: Codec(a), from: fn(b) -> a, to: fn(a) -> b) -> Codec(b) { +pub fn dict(codec: Codec(a), from: fn(b) -> a, to: fn(a) -> b) -> Codec(b) { use a <- then(codec, from) succeed(to(a)) } diff --git a/test/gleam_community_codec_test.gleam b/test/gleam_community_codec_test.gleam index 782cf57..3759559 100644 --- a/test/gleam_community_codec_test.gleam +++ b/test/gleam_community_codec_test.gleam @@ -57,4 +57,4 @@ pub fn bool_test() { codec.encode_string(True, bool_codec) |> codec.decode_string(bool_codec) |> should.equal(Ok(True)) -} \ No newline at end of file +}