Skip to content

Commit b3caec0

Browse files
authored
Merge pull request #1 from Billuc/dynamic_bit_array_support
feat(convert): dynamic & bitarray support
2 parents 09ccc03 + cf06732 commit b3caec0

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.0.1 - 28/12/2024
4+
5+
**Features**
6+
7+
- Dynamic and BitArray support
8+
39
## 1.0.0 - 04/14/2024
410

511
Initial release of convert

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,12 @@ Further documentation can be found at <https://hexdocs.pm/convert>.
5151
## Features
5252

5353
- Javascript and Erlang targets
54-
- Converters for all basic types (except BitArray)
54+
- Converters for all basic types
5555
- Define converters for List, Dict, Result & Option
56-
- Build decoders for custom objects and enums
57-
- Encode and decode to JSON.
56+
- Build converters for custom objects and enums
5857

5958
## Potential developments
6059

61-
- Add BitArray support
62-
- Add Tuple support
63-
- Add Yaml conversion
64-
6560
Feel free to open PRs and issues if you want more features !
6661

6762
## Development

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "convert"
2-
version = "1.0.0"
2+
version = "1.0.1"
33

44
# Fill out these fields if you intend to generate HTML documentation or publish
55
# your project to the Hex package manager.

src/convert.gleam

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ pub type GlitrType {
2020
Optional(of: GlitrType)
2121
Result(result: GlitrType, error: GlitrType)
2222
Enum(variants: List(#(String, GlitrType)))
23-
// Maybe add BitArray
23+
Dynamic
24+
BitArray
2425
}
2526

2627
/// This type is used to represent data values.
@@ -38,6 +39,8 @@ pub type GlitrValue {
3839
OptionalValue(value: option.Option(GlitrValue))
3940
ResultValue(value: Result(GlitrValue, GlitrValue))
4041
EnumValue(variant: String, value: GlitrValue)
42+
DynamicValue(value: dynamic.Dynamic)
43+
BitArrayValue(value: BitArray)
4144
}
4245

4346
/// A converter is an object with the data necessary to encode and decode a specific Gleam type.
@@ -342,7 +345,7 @@ pub fn dict(
342345
| Error(errs_1), Ok(_), Error(errs_2)
343346
-> Error(list.append(errs_1, errs_2))
344347
Error(errs), Error(errs_k), Error(errs_v) ->
345-
Error(list.concat([errs, errs_k, errs_v]))
348+
Error(list.flatten([errs, errs_k, errs_v]))
346349
}
347350
})
348351
|> result.map(dict.from_list)
@@ -440,6 +443,38 @@ pub fn enum(
440443
)
441444
}
442445

446+
/// Basic converter for Dynamic values
447+
pub fn dynamic() -> Converter(dynamic.Dynamic) {
448+
Converter(
449+
fn(v: dynamic.Dynamic) { DynamicValue(v) },
450+
fn(v: GlitrValue) {
451+
case v {
452+
DynamicValue(val) -> Ok(val)
453+
other ->
454+
Error([dynamic.DecodeError("DynamicValue", get_type(other), [])])
455+
}
456+
},
457+
Dynamic,
458+
dynamic.from(Nil),
459+
)
460+
}
461+
462+
/// Basic converter for BitArray values
463+
pub fn bit_array() -> Converter(BitArray) {
464+
Converter(
465+
fn(v: BitArray) { BitArrayValue(v) },
466+
fn(v: GlitrValue) {
467+
case v {
468+
BitArrayValue(val) -> Ok(val)
469+
other ->
470+
Error([dynamic.DecodeError("BitArrayValue", get_type(other), [])])
471+
}
472+
},
473+
BitArray,
474+
<<>>,
475+
)
476+
}
477+
443478
/// Create a converter by mapping the encode and decode functions from an existing one
444479
///
445480
/// Example:
@@ -456,12 +491,11 @@ pub fn enum(
456491
/// fn(v: String) {
457492
/// let elems = string.split(v, "/")
458493
/// case elems {
459-
/// [y, m, d, ..] -> Date(y, m, d)
460-
/// [y, m] -> Date(y, m, -1)
461-
/// [y] -> Date(y, -1, -1)
462-
/// [] -> Date(-1, -1, -1)
463-
/// }
494+
/// [y, m, d, ..] -> Ok(Date(y, m, d))
495+
/// _ -> Error([])
496+
/// },
464497
/// }
498+
/// Date(0, 0, 0) // This is required for now...
465499
/// )
466500
/// }
467501
/// ```
@@ -499,6 +533,8 @@ fn get_type(val: GlitrValue) -> String {
499533
OptionalValue(_) -> "OptionalValue"
500534
ResultValue(_) -> "ResultValue"
501535
StringValue(_) -> "StringValue"
536+
DynamicValue(_) -> "DynamicValue"
537+
BitArrayValue(_) -> "BitArrayValue"
502538
}
503539
}
504540

0 commit comments

Comments
 (0)