Skip to content

[#346] Added `AsRPC` type family

Diogo Castro requested to merge diogo/#346-th-poc-v2 into master

Description

This is a follow-up of !707 (closed).

This MR expose a new type family:

type family AsRPC (a :: k) = (b :: k)

This type family maps a storage type S to the type that will be returned from the Tezos RPC when we query that storage, AsRPC S.

type instance AsRPC (BigMap k v) = BigMapId k (AsRPC v)
type instance AsRPC [a] = [AsRPC a]
type instance AsRPC Integer = Integer
type instance AsRPC Natural = Natural
type instance AsRPC MText = MText

It also exports 4 template-haskell functions for deriving AsRPC instances for user-defined types:

deriveRPC :: String -> Q [Dec]
deriveRPCWithStrategy :: String -> GenericStrategy -> Q [Dec]

deriveManyRPC :: String -> [String] -> Q [Dec]
deriveManyRPCWithStrategy :: String -> [String] -> GenericStrategy -> Q [Dec]

There are plenty of examples in the Test.AsRPC module, but here's one:

data ExampleStorage a b = ExampleStorage
  { _esField1 :: Integer
  , _esField2 :: [BigMap Integer MText]
  , _esField3 :: a
  }
  deriving stock Generic
  deriving anyclass IsoValue

deriveRPC "ExampleStorage"

======>

data ExampleStorageRPC a (b :: k) = ExampleStorageRPC
  { _esField1RPC :: AsRPC Integer
  , _esField2RPC :: AsRPC [BigMap Integer MText]
  , _esField3RPC :: AsRPC a
  }

type instance AsRPC (ExampleStorage a (b :: k)) = ExampleStorageRPC a (b :: k)

deriving anyclass instance IsoValue (AsRPC a) => IsoValue (ExampleStorageRPC a (b :: k))

instance Generic (ExampleStorageRPC a (b :: k)) where ...

Related issue(s)

Resolves part of #346 (closed)

Checklist for your Merge Request

Related changes (conditional)

  • Tests (see short guidelines)

    • If I added new functionality, I added tests covering it.
    • If I fixed a bug, I added a regression test to prevent the bug from silently reappearing again.
  • Documentation

    • I checked whether I should update the docs and did so if necessary:
    • I updated changelog files of all affected packages released to Hackage if my changes are externally visible.

Stylistic guide (mandatory)

Edited by Diogo Castro

Merge request reports