bitemyapp / bitemyapp/esqueleto

Cannot express runtime casts

Open
#63 3 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Haskell
Stars
399
Forks
107
Avg merge
1d 17h
Merged PRs (30d)
1

Description

I've run in to a number of situations where I actually want to explicitly `cast` data in `sql`. The current casting model is just type level casting and has no runtime effects. To combat this I have a casting implementation loosly based on Idris' casting model.

```
class PostgresCast a b where
castType :: IsString x => Proxy a -> Proxy b -> x

cast :: forall b a. PostgresCast a b => SqlExpr (Value a) -> SqlExpr (Value b)
cast x = unsafeSqlFunction "CAST" . unsafeSqlBinOp " AS " x . unsafeSqlValue $ castType (Proxy @a) (Proxy @b)
```

Currently using these impls, but a library proper implementation would probably need to be more explicit than `Num n => PostgresCast n Double`
```
instance Num n => PostgresCast n Double where
castType _ _ = "real"

instance Num n => PostgresCast n Int where
castType _ _ = "integer"

instance SqlString n => PostgresCast n Text where
castType _ _ = "text"
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.