diff --git a/CHANGELOG.md b/CHANGELOG.md index 08f79e5..25f2fad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## 1.0.1 - 28/12/2024 + +**Features** + +- Dynamic and BitArray support + ## 1.0.0 - 04/14/2024 Initial release of convert diff --git a/README.md b/README.md index 958ec34..9dcccbf 100644 --- a/README.md +++ b/README.md @@ -51,17 +51,12 @@ Further documentation can be found at . ## Features - Javascript and Erlang targets -- Converters for all basic types (except BitArray) +- Converters for all basic types - Define converters for List, Dict, Result & Option -- Build decoders for custom objects and enums -- Encode and decode to JSON. +- Build converters for custom objects and enums ## Potential developments -- Add BitArray support -- Add Tuple support -- Add Yaml conversion - Feel free to open PRs and issues if you want more features ! ## Development diff --git a/gleam.toml b/gleam.toml index 8aeb1e8..089a700 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "convert" -version = "1.0.0" +version = "1.0.1" # Fill out these fields if you intend to generate HTML documentation or publish # your project to the Hex package manager. diff --git a/src/convert.gleam b/src/convert.gleam index 555a0e3..1c17596 100644 --- a/src/convert.gleam +++ b/src/convert.gleam @@ -20,7 +20,8 @@ pub type GlitrType { Optional(of: GlitrType) Result(result: GlitrType, error: GlitrType) Enum(variants: List(#(String, GlitrType))) - // Maybe add BitArray + Dynamic + BitArray } /// This type is used to represent data values. @@ -38,6 +39,8 @@ pub type GlitrValue { OptionalValue(value: option.Option(GlitrValue)) ResultValue(value: Result(GlitrValue, GlitrValue)) EnumValue(variant: String, value: GlitrValue) + DynamicValue(value: dynamic.Dynamic) + BitArrayValue(value: BitArray) } /// A converter is an object with the data necessary to encode and decode a specific Gleam type. @@ -342,7 +345,7 @@ pub fn dict( | Error(errs_1), Ok(_), Error(errs_2) -> Error(list.append(errs_1, errs_2)) Error(errs), Error(errs_k), Error(errs_v) -> - Error(list.concat([errs, errs_k, errs_v])) + Error(list.flatten([errs, errs_k, errs_v])) } }) |> result.map(dict.from_list) @@ -440,6 +443,38 @@ pub fn enum( ) } +/// Basic converter for Dynamic values +pub fn dynamic() -> Converter(dynamic.Dynamic) { + Converter( + fn(v: dynamic.Dynamic) { DynamicValue(v) }, + fn(v: GlitrValue) { + case v { + DynamicValue(val) -> Ok(val) + other -> + Error([dynamic.DecodeError("DynamicValue", get_type(other), [])]) + } + }, + Dynamic, + dynamic.from(Nil), + ) +} + +/// Basic converter for BitArray values +pub fn bit_array() -> Converter(BitArray) { + Converter( + fn(v: BitArray) { BitArrayValue(v) }, + fn(v: GlitrValue) { + case v { + BitArrayValue(val) -> Ok(val) + other -> + Error([dynamic.DecodeError("BitArrayValue", get_type(other), [])]) + } + }, + BitArray, + <<>>, + ) +} + /// Create a converter by mapping the encode and decode functions from an existing one /// /// Example: @@ -456,12 +491,11 @@ pub fn enum( /// fn(v: String) { /// let elems = string.split(v, "/") /// case elems { -/// [y, m, d, ..] -> Date(y, m, d) -/// [y, m] -> Date(y, m, -1) -/// [y] -> Date(y, -1, -1) -/// [] -> Date(-1, -1, -1) -/// } +/// [y, m, d, ..] -> Ok(Date(y, m, d)) +/// _ -> Error([]) +/// }, /// } +/// Date(0, 0, 0) // This is required for now... /// ) /// } /// ``` @@ -499,6 +533,8 @@ fn get_type(val: GlitrValue) -> String { OptionalValue(_) -> "OptionalValue" ResultValue(_) -> "ResultValue" StringValue(_) -> "StringValue" + DynamicValue(_) -> "DynamicValue" + BitArrayValue(_) -> "BitArrayValue" } }