Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,12 @@ Further documentation can be found at <https://hexdocs.pm/convert>.
## 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
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
50 changes: 43 additions & 7 deletions src/convert.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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...
/// )
/// }
/// ```
Expand Down Expand Up @@ -499,6 +533,8 @@ fn get_type(val: GlitrValue) -> String {
OptionalValue(_) -> "OptionalValue"
ResultValue(_) -> "ResultValue"
StringValue(_) -> "StringValue"
DynamicValue(_) -> "DynamicValue"
BitArrayValue(_) -> "BitArrayValue"
}
}

Expand Down
Loading