← 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 | CDecimal | CMoneyIn String | CNull ColType
  type Shape = SRecord (List ( String, ColType )) | SScalar ColType | SBlob
  type Codec a
  type alias CodecPayload a = { enc : a -> Value , dec : Decoder a , shp : Shape , sch : SchemaNode , zro : () -> Maybe Value , fzr : () -> List ( String, Value ) , fck : () -> List ( String, Value -> Maybe String ) , vck : List (Value -> Maybe String) , vcs : List ( String, Value -> Maybe String ) , prb : List String , jsn : a -> Maybe String }
  type alias JsonSchema = { schema : Value , exact : Bool }
  type SchemaNode
  type alias AutoField = { name : String , schema : Value , exact : Bool }
  type ObjectCodec a b
  type alias ObjectPayload a b = { encFields : a -> List ( String, Value ) , decValue : Decoder b , cols : List ( String, ColType ) , schs : List ( String, SchemaNode ) , zros : List ( String, () -> Maybe Value ) , fcks : List ( String, Value -> Maybe String ) , vcks : List ( String, Value -> Maybe String ) , oprb : List String }
  type Variant v
  type alias VariantPayload v = { tag : String , args : List SchemaNode , dec : Decoder v , vprb : List String }

Values
  auto : a -> Codec a
      Derive a codec from a record type — no field-by-field pipeline. The
  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.
  decimal : Codec Decimal
      Codec for a `Std.Decimal` (v0.29.2, opt-in) — an exact decimal column.
  dict : Codec comparable -> Codec v -> Codec (Dict comparable v)
      Codec for a `Dict k v` given codecs for the key and the value. Encodes as a
  enum : List ( v, String ) -> Codec v
      A nullary enum stored as a readable TEXT name, via `(value, name)` pairs:
  error : Codec Error
      The canonical codec for the stdlib `Error` type — the ONE wire contract for
  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.
  fieldReadError : Codec a -> String -> Maybe ( String, String )
      The first field of the JSON object `text` that its field's codec does
  fieldZeros : Codec a -> List ( String, Value )
      The zero of each field of a record codec, keyed by its JSON key (the
  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 —
  fromXml : String -> Codec a -> String -> Result Error a
      Read a value written as XML: the reverse of `toXml`, from a document whose
  inline : (a -> r) -> ObjectCodec r r -> ObjectCodec a (r -> b) -> ObjectCodec a b
      Add every field of another record's object builder to this one, flat:
  int : Codec Int
      Codec for an `Int` — JSON number; INTEGER/BIGINT column.
  isoCurrency : Codec Currency
      Codec for a `Currency` (v0.29.2): its ISO code as a JSON string,
  jsonSchema : Codec a -> JsonSchema
      The JSON Schema of the codec's JSON: `"string"`, `"integer"`, `"number"`
  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`, and only `null` decodes
  moneyIn : Currency -> Codec Money
      Codec for a `Money` in ONE currency (v0.29.2, opt-in): a decimal column
  moneyObject : Codec Money
      Codec for a `Money` in any currency (v0.29.2): the JSON object
  object : b -> ObjectCodec a b
      Start a record codec from its constructor.
  problems : Codec a -> List String
      What is wrong with the codec as built (v0.29.2), `[]` for a sound one: an
  result : Codec e -> Codec a -> Codec (Result e a)
      Codec for a `Result e a` given codecs for the error and the value. Encodes
  schemaFitError : Value -> Value -> Maybe String
      `Nothing` when every JSON value the JSON Schema `source` describes reads,
  schemaValueError : Value -> Value -> Maybe String
      `Nothing` when the JSON value is one the JSON Schema (as `jsonSchema`
  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
  toXml : String -> Codec a -> a -> String
      Write a value as XML: one root element named `root`, then the codec's value
  value : Codec Value
      Codec for a raw `Json.Value` (v0.29.2): the JSON as it is, both ways, so
  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.
  writeChecks : Codec a -> List ( String, Value -> Maybe String )
      The write checks of a record codec (v0.29.2): each field whose codec is
  zeroJson : Codec a -> Maybe Value
      The JSON of the codec's zero value, when the codec knows one. For an