← all modules

Std.Codec

Std.Codec
  Std.Codec — one bidirectional codec per type, reused for JSON now and DB

Types
  type ColType = CText | CInt | CReal | CBool | CBlob | CNull ColType
  type Shape = SRecord (List ( String, ColType )) | SScalar ColType | SBlob
  type Codec = Codec { enc : a -> Value , dec : Decoder a , shp : Shape }
  type ObjectCodec = ObjectCodec { encFields : a -> List ( String, Value ) , decValue : Decoder b , cols : List ( String, ColType ) }
  type Variant = Variant String (Decoder v)

Values
  auto : a -> Codec a
      Derive a codec from a record type by reflection — no field-by-field
  autoCamel : a -> Codec a
      Like `auto`, but keeps camelCase column / JSON-key names (`priceMinor` stays
  autoWith : List ( String, Codec b ) -> a -> Codec a
      Like `auto`, but OVERRIDE the codec for named columns — auto-derive everything
  bool : Codec Bool
      Codec for a `Bool` — JSON boolean; BOOLEAN on Postgres / 0-1 INTEGER on
  buildObject : ObjectCodec a a -> Codec a
      Finish a record codec.
  enum : List ( v, String ) -> Codec v
      A nullary enum stored as a readable TEXT name, via `(value, name)` pairs:
  field : String -> (a -> f) -> Codec f -> ObjectCodec a (f -> b) -> ObjectCodec a b
      Add a field: its JSON key, a getter, and the field's codec.
  float : Codec Float
      Codec for a `Float` — JSON number; REAL column.
  fromJson : Codec a -> String -> Result Error a
      Decode from a JSON string.
  fromJsonSafe : Int -> Codec a -> String -> Result Error a
      Decode from JSON, rejecting input longer than `maxChars` BEFORE parsing —
  int : Codec Int
      Codec for an `Int` — JSON number; INTEGER/BIGINT column.
  list : Codec a -> Codec (List a)
      Codec for a `List a` given a codec for `a` — JSON array; stored as a JSON
  map : (a -> b) -> (b -> a) -> Codec a -> Codec b
      Adapt a codec to another type via a bijection (`to` on decode, `from` on
  maybe : Codec a -> Codec (Maybe a)
      Optional value. `Nothing` encodes as JSON `null`; a `null` or type-mismatch
  object : b -> ObjectCodec a b
      Start a record codec from its constructor.
  shape : Codec a -> Shape
      The codec's structural shape — how the DB backend derives columns (record
  string : Codec String
      Codec for a `String` — JSON string; TEXT column.
  taggedUnion : (v -> ( String, List Value )) -> List (Variant v) -> Codec v
      Codec for a custom type (tagged union). The first argument maps a value to
  toJson : Codec a -> a -> String
      Encode to a compact JSON string.
  toValue : Codec a -> a -> Value
      The codec's encoder as a plain function `a -> Value` (a JSON `Value`, the DB
  var0 : String -> v -> Variant v
      A variant with no arguments.
  var1 : String -> (a -> v) -> Codec a -> Variant v
      A variant with one argument.
  var2 : String -> (a -> b2 -> v) -> Codec a -> Codec b2 -> Variant v
      A variant with two arguments.
  var3 : String -> (a -> b2 -> c2 -> v) -> Codec a -> Codec b2 -> Codec c2 -> Variant v
      A variant with three arguments.