N t
deriving stock Show
type family MapFields x where
MapFields (S1 ('MetaSel _ a b c) (Rec0 (t <?> n))) = S1 ('MetaSel ('Just n) a b c) (Rec0 t)
MapFields (M1 i meta cs) = M1 i meta (MapFields cs)
MapFields (a :+: b) = MapFields a :+: MapFields b
MapFields (a :*: b) = MapFields a :*: MapFields b
MapFields other = other
newtype ViaNamed a = ViaNamed a
deriving anyclass instance (Generic (ViaNamed a), GFromJSON Zero (Rep (ViaNamed a))) => FromJSON (ViaNamed a)
deriving anyclass instance (Generic (ViaNamed a), GToJSON Zero (Rep (ViaNamed a))) => ToJSON (ViaNamed a)
instance (Generic a, Coercible (Rep a) (Rep (ViaNamed a))) => Generic (ViaNamed a) where
type Rep (ViaNamed a) = MapFields (Rep a)
from :: forall x . ViaNamed a -> Rep (ViaNamed a) x
from = coerce $ from @a @x
to :: forall x . Rep (ViaNamed a) x -> ViaNamed a
to = coerce $ to @a @x
data X = X { a :: Int <?> "b" , c :: Int <?> "d" }
deriving stock (Generic, Show)
deriving (FromJSON, ToJSON) via ViaNamed X
тут есть существенный недостаток
Обсуждают сегодня