MyNixOS website logo
Description

Picklers for de/serialising Generic data types to and from query strings.

Type classes, pickling combinators, and GHC.Generics implementations for Query Strings

querystring-pickle

Build Status

Table of Contents

Introduction

TODO

Examples

TODO

Common Instances

Lists

No default instance for IsQuery a => IsQuery [a] is specified since there are a number of possible implementations. To that end, two pickling combinators are supplied:

qpList

A query string of the format ?key=Saab,Audi,Holden can be derived using qpList as follows:

instance IsQuery a => IsQuery [a] where
    queryPickler = qpList queryPickler

qpOrdinalList

Alternatively, a query string of the format key.1=Saab&key.2=Audi&key.3=Holden can be derived using the qpOrdinalList combinator:

instance IsQuery a => IsQuery [a] where
    queryPickler = qpOrdinalList queryPickler

Ordering is preserved in both cases, although qpOrdinalList would preserve the order if the querystring was reshuffled.

Either/Maybe

IsQuery instances for Maybe a and Either a b are not supplied due to ambiguous semantics, take the following example:

instance IsQuery a => IsQuery (Maybe a) where
    queryPickler = qpOption queryPickler

instance (IsQuery a, IsQuery b) => IsQuery (Either a b) where
    queryPickler = queryPickler `qpEither` queryPickler

data A = A { aInt1 :: Int, aInt2 :: Int } deriving (Show, Generic)
data B = B { bA :: Maybe A } deriving (Show, Generic)
data C = C { cB :: B } deriving (Show, Generic)
data D = D { dAInt :: Either A Int } deriving (Show, Generic)

instance IsQuery A
instance IsQuery B
instance IsQuery C
instance IsQuery D

let c = C $ B Nothing
let d = D . Left $ A 1 2
let e = D $ Right 3

Running toQuery / fromQuery on the example bindings yields:


λ: toQuery c
[]

λ: fromQuery (toQuery c) :: Either String C
Left "qpElem: non-locatable - B - List []"

λ: toQuery d
[("AInt.Int1","1"),("AInt.Int2","2")]

λ: fromQuery (toQuery d) :: Either String D
Right (D {dAInt = Left (A {aInt1 = 1, aInt2 = 2})})

λ: toQuery e
[("AInt","3")]

λ: fromQuery (toQuery e) :: Either String D
Right (D {dAInt = Right 3})

If data type B has a second non-optional field, the fromQuery deserialisation of binding c will succeed.

This is due to the overly simple underlying rose tree used as the intermediate data structure for query transforms. Something that will hopefully be fixed in a future release.

It is left up to the consumer of the library to decide best how to handle this case. I apologies if it forces anyone to use orphaned instances.

Compatibility

Due to the dependency on GHC.Generics a version of base 4.6 or higher is required.

Contributing

For any problems, comments or feedback please create an issue here on GitHub.

Licence

querystring-pickle is released under the Mozilla Public License Version 2.0

Metadata

Version

0.2.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows