glitr/convert

Types

A converter is an object with the data necessary to encode and decode a specific Gleam type.
You can build converters using the provided constructors.

pub opaque type Converter(a)

This type is used to define the shape of the data.
It isn’t meant to be used directly !
It is better to use converters that use GlitrTypes internally to decode data.

pub type GlitrType {
  String
  Bool
  Float
  Int
  Null
  List(of: GlitrType)
  Dict(key: GlitrType, value: GlitrType)
  Object(fields: List(#(String, GlitrType)))
  Optional(of: GlitrType)
  Result(result: GlitrType, error: GlitrType)
  Enum(variants: List(#(String, GlitrType)))
}

Constructors

  • String
  • Bool
  • Float
  • Int
  • Null
  • List(of: GlitrType)
  • Dict(key: GlitrType, value: GlitrType)
  • Object(fields: List(#(String, GlitrType)))
  • Optional(of: GlitrType)
  • Result(result: GlitrType, error: GlitrType)
  • Enum(variants: List(#(String, GlitrType)))

This type is used to represent data values.
It is an intermediate type between encoded data and Gleam types. It isn’t meant to be used directly !

pub type GlitrValue {
  StringValue(value: String)
  BoolValue(value: Bool)
  FloatValue(value: Float)
  IntValue(value: Int)
  NullValue
  ListValue(value: List(GlitrValue))
  DictValue(value: dict.Dict(GlitrValue, GlitrValue))
  ObjectValue(value: List(#(String, GlitrValue)))
  OptionalValue(value: option.Option(GlitrValue))
  ResultValue(value: Result(GlitrValue, GlitrValue))
  EnumValue(variant: String, value: GlitrValue)
}

Constructors

  • StringValue(value: String)
  • BoolValue(value: Bool)
  • FloatValue(value: Float)
  • IntValue(value: Int)
  • NullValue
  • ListValue(value: List(GlitrValue))
  • DictValue(value: dict.Dict(GlitrValue, GlitrValue))
  • ObjectValue(value: List(#(String, GlitrValue)))
  • OptionalValue(value: option.Option(GlitrValue))
  • ResultValue(value: Result(GlitrValue, GlitrValue))
  • EnumValue(variant: String, value: GlitrValue)

Intermediate type to build a converter for an object type

pub opaque type PartialConverter(base)

Functions

pub fn bool() -> Converter(Bool)

Basic converter for a Bool value

pub fn decode(
  converter: Converter(a),
) -> fn(GlitrValue) -> Result(a, List(DecodeError))

Decode a GlitrValue using the provided converter.

pub fn dict(
  key: Converter(a),
  value: Converter(b),
) -> Converter(Dict(a, b))

Basic converter for a Dict value.

key is a converter for the keys. value is a converter for the values.

Example:

let converter: Converter(Dict(String, Int)) = dict(string(), int())
pub fn encode(converter: Converter(a)) -> fn(a) -> GlitrValue

Encode a value into the corresponding GlitrValue using the converter.
If the converter isn’t valid, a NullValue is returned.

pub fn enum(
  tags: fn(a) -> String,
  converters: List(#(String, Converter(a))),
) -> Converter(a)

Create a converter for an enum type

tags is a function that associate a tag to each variant of the enum converters is a list of converters, each associated with a tag

Example:

type Action {
  Open(id: String)
  Close(id: String)
}

let open_converter = object({
  use id <- field("id", fn(v: Action) {
    case v {
      Open(id) -> Ok(id)
      _ -> Error(Nil)
    }
  }, string())
  success(Open(id:))
})

let close_converter = object({
  use id <- field("id", fn(v: Action) {
    case v {
      Close(id) -> Ok(id)
      _ -> Error(Nil)
    }
  }, string())
  success(Close(id:))
})

let action_converter = enum(
  fn(v) {
    case v {
      Open(_) -> "Open"
      Close(_) -> "Close"
    }
  },
  [
    #("Open", open_converter),
    #("Close", close_converter),
  ]
)
pub fn field(
  field_name: String,
  field_getter: fn(a) -> Result(b, Nil),
  field_type: Converter(b),
  next: fn(b) -> PartialConverter(a),
) -> PartialConverter(a)

Add a field to a PartialConverter
See object for its usage details

‘field_name’ is the field name that will be used in the converted value. It may not be equal to the actual field name.
‘field_getter’ is a function that returns the value of the field from the complete object.
‘field_type’ is a Converter associated to the type of the field.

pub fn float() -> Converter(Float)

Basic converter for a Float value

pub fn int() -> Converter(Int)

Basic converter for a Int value

pub fn list(of: Converter(a)) -> Converter(List(a))

Basic converter for a List value.

of is a converter for the type of the elements.

pub fn null() -> Converter(Nil)

Basic converter for a Nil value

pub fn object(converter: PartialConverter(a)) -> Converter(a)

Create a Converter from a PartialConverter

Example:

type Person {
  Person(name: String, age: Int)
}

let convert = object({
  use name <- field("name", fn(v: Person) { Ok(v.name) }, string())
  use age <- field("age", fn(v: Person) { Ok(v.age) }, int())
  success(Person(name:, age:))
})
pub fn optional(of: Converter(a)) -> Converter(Option(a))

Basic converter for a Option value.

of is a converter for the optional value.

pub fn result(
  res: Converter(a),
  error: Converter(b),
) -> Converter(Result(a, b))

Basic converter for a Result value.

res is a converter for the Ok value. error is a converter for the Error value.

pub fn string() -> Converter(String)

Basic converter for a String value

pub fn success(c: a) -> PartialConverter(a)

Used to initialize a PartialConverter
See object for its usage details

pub fn type_def(converter: Converter(a)) -> GlitrType

Return the GlitrType associated with the converter

Search Document