Skip to content

Commit

Permalink
feat(glitr_convert): Added default_value param to map
Browse files Browse the repository at this point in the history
  • Loading branch information
Billuc committed Nov 17, 2024
1 parent 35ddb9d commit 27e3717
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
2 changes: 1 addition & 1 deletion glitr_convert/gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "glitr_convert"
version = "0.2.2"
version = "0.2.3"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down
19 changes: 10 additions & 9 deletions glitr_convert/src/glitr/convert.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,12 @@ pub fn field(
}
},
fields_def: {
let converter = next(field_type.default_value)
[#(field_name, field_type.type_def), ..converter.fields_def]
},
default_value: {
let converter = next(field_type.default_value)
converter.default_value
[
#(field_name, field_type.type_def),
..next(field_type.default_value).fields_def
]
},
default_value: { next(field_type.default_value).default_value },
)
}

Expand Down Expand Up @@ -469,7 +468,9 @@ pub fn enum(
pub fn map(
converter: Converter(a),
encode_map: fn(b) -> a,
decode_map: fn(a) -> b,
decode_map: fn(a) -> Result(b, List(dynamic.DecodeError)),
default_value: b,
// Kinda required until I find a more elegant way around this
) -> Converter(b) {
Converter(
fn(v: b) {
Expand All @@ -478,10 +479,10 @@ pub fn map(
},
fn(v: GlitrValue) {
converter.decoder(v)
|> result.map(decode_map)
|> result.then(decode_map)
},
converter.type_def,
converter.default_value |> decode_map,
default_value,
)
}

Expand Down
45 changes: 33 additions & 12 deletions glitr_convert/test/converters_test.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gleam/dynamic
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import gleeunit/should
import glitr/convert
Expand Down Expand Up @@ -126,6 +126,36 @@ pub type Date {
Date(year: Int, month: Int, day: Int)
}

fn date_parse(v: String) -> Result(Date, List(dynamic.DecodeError)) {
case string.split(v, "/") {
[y, m, d, ..] -> {
use year <- result_guard(int.parse(y), [
dynamic.DecodeError("An integer", y, ["year"]),
])
use month <- result_guard(int.parse(m), [
dynamic.DecodeError("An integer", m, ["month"]),
])
use day <- result_guard(int.parse(d), [
dynamic.DecodeError("An integer", d, ["day"]),
])

Ok(Date(year, month, day))
}
_ -> Error([dynamic.DecodeError("A string of format 'Y/M/D'", v, [])])
}
}

fn result_guard(
v: Result(a, _),
otherwise: b,
cb: fn(a) -> Result(c, b),
) -> Result(c, b) {
case v {
Error(_) -> Error(otherwise)
Ok(value) -> cb(value)
}
}

pub fn converter_map_test() {
// We are storing the date as a string for optimized memory storage
let date_converter = {
Expand All @@ -134,17 +164,8 @@ pub fn converter_map_test() {
fn(v: Date) {
[v.year, v.month, v.day] |> list.map(int.to_string) |> string.join("/")
},
fn(v: String) {
let elems =
string.split(v, "/")
|> list.map(fn(el) { int.parse(el) |> result.unwrap(-1) })
case elems {
[y, m, d, ..] -> Date(y, m, d)
[y, m] -> Date(y, m, -1)
[y] -> Date(y, -1, -1)
[] -> Date(-1, -1, -1)
}
},
date_parse,
Date(0, 0, 0),
)
}

Expand Down

0 comments on commit 27e3717

Please sign in to comment.