MyNixOS website logo
Description

Alter your records with ease.

Please see the README on GitHub at https://github.com/lumihq/record-wrangler#readme

record-wrangler

Build Status

This package uses TemplateHaskell to alter records in ways that may be convenient for you.

Inspiration

Let's say you've got a set of API types and a set of domain types. The API types define your HTTP contract, and you generically derive ToJSON and FromJSON instances from their field labels. They look like this:

data User = User
    { id :: Int
    , name :: String
    , age :: Int
    }

data Dog = Dog
    { id :: Int
    , name :: String
    , toy :: FavoriteToy
    , ownerId :: Int
    }

The domain types for your business logic look a little different, though. The record fields are prefixed with an _ underscore character, to allow for lens derivation. We can pretend they have other differences that are relevant to internal details and irrelevant for showing off record-wrangler if that makes you feel better.

data Dog = Dog
    { _id :: Int
    , _name :: String
    , _toy :: FavoriteToy
    , _ownerId :: Int
    }

makeLenses ''Dog

data User = User
    { _id :: Int
    , _name :: String
    , _age :: Int
    }

makeLenses ''User

If the record fields were the same, then we could just write the conversion function using RecordWildCards and it'd be fine:

convertUser Api.User{..} = Domain.User{..}

However, because the field labels are different, we have to do it manually:

convertUser Api.User{..} = Domain.User
    { _id = id
    , _name = name
    , _age = age 
    }

This is annoying and boilerplatey. I don't know about you, but I hate writing code, for every line of code I write is a chance to mess up. Let's use record-wrangler to make these conversions easier.

wrangle ''Api.User defWrangleOpts 
    { fieldLabelModifier = \fieldStr -> '_' : fieldStr 
    }

This is going to take the Api.User type and modify it slightly. By default, we append a ' to the constructor name, type name, and field labels. Here, we've decided to prepend a _ character to the field label. It also generates a function wrangleUserToUser' which converts the old record to the modified one.

With the power of view patterns, our conversion function is now quite concise:

convertUser :: Api.User -> Domain.User
convertUser (wrangleUserToUser' -> User'{..}) = Domain.User{..}
Metadata

Version

0.1.1.0

License

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