Skip to content

Commit

Permalink
Merge pull request #3 from giacomocavalieri/fix-2
Browse files Browse the repository at this point in the history
  • Loading branch information
MystPi authored Apr 6, 2024
2 parents 3d99db4 + 10ef341 commit 8f7b16e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ffi.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ decode_tuple(X) ->

decode_custom_type(X) ->
case X of
% Variants with no fields are encoded as a single atom named as the variant.
Atom when is_atom(Atom) ->
case inspect_maybe_gleam_atom(erlang:atom_to_binary(Atom), none, <<>>) of
{ok, AtomName} -> {ok, {t_custom, AtomName, []}};
{error, nil} -> decode_error("CustomType", X)
end;
% Variants with fields are encoded as tuples where the first items is an
% atom with the variant's name.
Tuple when is_tuple(Tuple) ->
case tuple_to_list(Tuple) of
[Atom | Elements] when is_atom(Atom) ->
Expand Down
12 changes: 12 additions & 0 deletions test/pprint_test.gleam
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import gleam/dict
import gleam/dynamic
import gleeunit
import gleeunit/should
import birdie
import pprint
import pprint/decoder

type Foo {
Foo(Int, bar: String, baz: String)
Wibble
}

pub fn main() {
gleeunit.main()
}

// https://github.com/MystPi/pprint/issues/2
pub fn custom_type_with_no_fields_decoding_test() {
Wibble
|> dynamic.from
|> decoder.classify
|> should.equal(decoder.TCustom("Wibble", []))
}

pub fn pretty_list_test() {
["This", "is", "a", "very", "long", "list", "that", "should", "be", "wrapped"]
|> pprint.format
Expand Down

0 comments on commit 8f7b16e

Please sign in to comment.