-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
256 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
*.ez | ||
/build | ||
erl_crash.dump | ||
src/foo** | ||
src/internal/foo** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,61 @@ | ||
# gserde | ||
|
||
**warning**: alpha package with poor code hygiene, including assert/panic/todo | ||
statements. Use understanding that this package is not ready for primetime. | ||
|
||
[![Package Version](https://img.shields.io/hexpm/v/gserde)](https://hex.pm/packages/gserde) | ||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/gserde/) | ||
|
||
```sh | ||
gleam add gserde | ||
``` | ||
|
||
## usage | ||
|
||
1. Create custom type with a singular variant constructor: | ||
2. Run `gleam run -m gserde` | ||
3. Observe `src/foo_json.gleam`, which has the goodies you need for json (de)serialization. | ||
|
||
```gleam | ||
import gserde | ||
// src/foo.gleam | ||
import gleam/option.{type Option} | ||
pub type FooJSON { | ||
Foo( | ||
a_bool: Bool, | ||
b_int: Int, | ||
c_float: Float, | ||
d_two_tuple: #(Int, String), | ||
e_option_int: Option(Int), | ||
f_string_list: List(String), | ||
) | ||
} | ||
pub fn main() { | ||
// TODO: An example of the project in use | ||
// src/my_module.gleam | ||
import foo | ||
import foo_json | ||
pub fn serialization_identity_test() { | ||
let foo_1 = foo.Foo(..) | ||
let foo_2 = foo_1 | ||
|> foo_json.to_string // 👀 | ||
|> foo_json.from_string // 👀 | ||
foo_1 == foo_2 | ||
} | ||
``` | ||
|
||
## todo | ||
|
||
- [ ] complete all cases | ||
- [ ] remove all invocations of assert/panic/todo | ||
- [ ] support non-gleam primitive types | ||
- [ ] handle all module references properly | ||
|
||
Further documentation can be found at <https://hexdocs.pm/gserde>. | ||
|
||
## Development | ||
|
||
```sh | ||
gleam run # Run the project | ||
gleam test # Run the tests | ||
gleam shell # Run an Erlang shell | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import glance | ||
import internal/codegen/statements as gens | ||
import internal/codegen/types as t | ||
import gleam/string | ||
import gleam/list | ||
import gleam/option | ||
import gleam/io | ||
import gleam/int | ||
import request.{type Request, Request} | ||
import internal/path.{basename} | ||
|
||
fn quote(str) { | ||
"\"" <> str <> "\"" | ||
} | ||
|
||
fn gen_decoder(typ) { | ||
case typ { | ||
glance.NamedType(name, _module_todo, parameters) -> { | ||
case name { | ||
"List" -> { | ||
let assert Ok(t0) = list.at(parameters, 0) | ||
gens.call("dynamic.list", [gen_decoder(t0)]) | ||
} | ||
"Option" -> { | ||
let assert Ok(t0) = list.at(parameters, 0) | ||
gens.call("dynamic.optional", [gen_decoder(t0)]) | ||
} | ||
_ -> gens.VarPrimitive("dynamic." <> string.lowercase(name)) | ||
} | ||
} | ||
glance.TupleType(parts) -> { | ||
let m_tuple = | ||
list.length(of: parts) | ||
|> int.to_string | ||
gens.call( | ||
"dynamic.tuple" <> string.lowercase(m_tuple), | ||
parts | ||
|> list.map(gen_decoder), | ||
) | ||
} | ||
x -> { | ||
io.debug(#("warning: unsupported decoding", x)) | ||
gens.VarPrimitive("dynamic.toodoo") | ||
} | ||
} | ||
} | ||
|
||
fn gen_root_decoder(req) { | ||
let Request( | ||
src_module_name: src_module_name, | ||
type_name: _type_name, | ||
variant: variant, | ||
.., | ||
) = req | ||
let n_str = | ||
list.length(of: variant.fields) | ||
|> int.to_string | ||
gens.Function( | ||
"from_json", | ||
[gens.arg_typed("json_str", t.AnonymousType("String"))], | ||
[ | ||
gens.call("json.decode", [ | ||
gens.VarPrimitive("json_str"), | ||
gens.call("dynamic.decode" <> n_str, [ | ||
gens.VarPrimitive( basename(src_module_name) <> "." <> variant.name), | ||
..list.map(req.variant.fields, fn(field) { | ||
gens.call("dynamic.field", [ | ||
gens.VarPrimitive( | ||
option.lazy_unwrap(field.label, fn() { | ||
panic as "@todo/panic variants must be labeled" | ||
}) | ||
|> quote, | ||
), | ||
gen_decoder(field.item), | ||
]) | ||
}) | ||
]), | ||
]), | ||
], | ||
) | ||
} | ||
|
||
pub fn to(req: Request) { | ||
[gen_root_decoder(req)] | ||
|> list.map(gens.generate) | ||
|> string.join(with: "\n") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import gleam/string | ||
import gleam/list | ||
import gleam/result | ||
|
||
pub fn basename(path: String) { | ||
string.split(path, on: "/") | ||
|> list.last | ||
|> result.unwrap(or: "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.