Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DerivingVia for Generic table codecs #272

Closed
chshersh opened this issue May 15, 2020 · 4 comments · Fixed by #320
Closed

DerivingVia for Generic table codecs #272

chshersh opened this issue May 15, 2020 · 4 comments · Fixed by #320
Assignees
Labels
codec Conversion between TOML and custom user data types generics Generic bidirectional codecs

Comments

@chshersh
Copy link
Contributor

I've tried to use fancy tricks for table codecs with generics. You can find my commented code here:

{-
TODO: uncomment when higher-kinded roles will be implemented
* https://github.com/ghc-proposals/ghc-proposals/pull/233
{- | @newtype@ for generic deriving of 'HasCodec' typeclass for custom data
types that should we wrapped into separate table. Use it only for data types
that are fields of another data types.
@
data Person = Person
{ personName :: Text
, personAddress :: Address
} deriving (Generic)
data Address = Address
{ addressStreet :: Text
, addressHouse :: Int
} deriving (Generic)
deriving HasCodec via TomlTable Address
personCodec :: TomlCodec Person
personCodec = genericCodec
@
@personCodec@ corresponds to the TOML of the following structure:
@
name = "foo"
[address]
street = \"Bar\"
house = 42
@
-}
newtype TomlTable a = TomlTable
{ unTomlTable :: a
}
instance (Generic a, GenericCodec (Rep a)) => HasCodec (TomlTable a) where
hasCodec :: Key -> TomlCodec (TomlTable a)
hasCodec = Toml.diwrap . Toml.table (genericCodec @a)
instance (Generic a, GenericCodec (Rep a)) => HasItemCodec (TomlTable a) where
hasItemCodec = Right $ Toml.diwrap $ genericCodec @a
-}

It wasn't possible to implement this feature back then. But thanks to @vrom911 nice work on simplifying the Codec type, I think we should be able to implement that feature again.

@chshersh chshersh added codec Conversion between TOML and custom user data types generics Generic bidirectional codecs labels May 15, 2020
@chshersh chshersh added this to the v1.3.0.0: Big update milestone May 15, 2020
@vrom911 vrom911 self-assigned this May 15, 2020
@vrom911
Copy link
Member

vrom911 commented May 15, 2020

Currently, I am getting

     Couldn't match type TomlTable Address with Address
        arising from the coercion of the method hasCodec
          from type Toml.Key -> TomlCodec (TomlTable Address)
            to type Toml.Key -> TomlCodec Address
     When deriving the instance for (HasCodec Address)
    |
182 |       deriving HasCodec via (TomlTable Address)
    |    

With the example from docs. I don't really know how to solve this issue. Do you have any ideas, @chshersh ?

(link to the branch)

vrom911 added a commit that referenced this issue May 15, 2020
@chshersh
Copy link
Contributor Author

@vrom911 Ah, sh*t, here we go again. The error is different, but it doesn't tell me how to solve the issue. Well, I guess not in this release...

@chshersh chshersh removed this from the v1.3.0.0: Big update milestone May 15, 2020
@chshersh
Copy link
Contributor Author

I've asked on SO:

@vrom911 vrom911 pinned this issue May 16, 2020
@chshersh chshersh added this to the v1.3.0.0: Big update milestone May 18, 2020
@chshersh
Copy link
Contributor Author

Should be possible to do after #311 🙂

vrom911 added a commit that referenced this issue May 19, 2020
vrom911 added a commit that referenced this issue May 19, 2020
chshersh pushed a commit that referenced this issue May 19, 2020
* [#272] DerivingVia for Generic table codecs

Resolves #272

* Fix
@chshersh chshersh unpinned this issue May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codec Conversion between TOML and custom user data types generics Generic bidirectional codecs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants