Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change map to dict #4

Merged
merged 3 commits into from
Feb 22, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
- uses: actions/[email protected]
- uses: erlef/[email protected]
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
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ gleam_stdlib = "~> 0.31"
gleam_json = "~> 0.5"

[dev-dependencies]
gleeunit = "~> 0.10"
gleeunit = "~> 1.0"
8 changes: 4 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
65 changes: 32 additions & 33 deletions src/gleam_community/codec.gleam
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)))
}

///
Expand Down Expand Up @@ -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)
}

///
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 ---------------------------------------------------------------------
Expand Down Expand Up @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion test/gleam_community_codec_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ pub fn bool_test() {
codec.encode_string(True, bool_codec)
|> codec.decode_string(bool_codec)
|> should.equal(Ok(True))
}
}
Loading