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

Recursive override #3

Open
anpryl opened this issue Aug 19, 2020 · 1 comment
Open

Recursive override #3

anpryl opened this issue Aug 19, 2020 · 1 comment

Comments

@anpryl
Copy link

anpryl commented Aug 19, 2020

Is it possible to override ToJSON for all fields in nested records?

newtype ZeroInt = ZeroInt Int

instance ToJSON ZeroInt where
  toJSON _ = toJSON (0 :: Int)

data MyRec = MyRec
  { x :: X,
    y :: Int
  }
  deriving (Show, Eq, Generic, ToJSON)

data X = X {x :: Int}
  deriving (Show, Eq, Generic, ToJSON)

newtype MyRecZeroInt = MyRecZeroInt MyRe
  deriving (ToJSON) via Override MyRecZeroInt '[Int `As` ZeroInt]

ghci:

 Data.ByteString.Lazy.Char8.putStrLn $ encode $ MyRecZeroInt $ MyRec  ( X 1) 2
{"x":{"x":1},"y":0}

I want x to become zero too.

@carymrobbins
Copy link
Member

It might be possible, but it's not currently implemented. I'd also be concerned with how unpredictable it might be. If one of the nested records does not have a Generic instance, should recursive override fail to compile? Should it succeed but ignore that record? There are likely lots of questions and edge cases that need to be accounted for first. I am open to suggestions and PRs, although for the latter it's better to discuss the approach first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants