{"id":15288107,"url":"https://github.com/lysxia/generic-data-surgery","last_synced_at":"2025-04-13T06:32:08.946Z","repository":{"id":54856249,"uuid":"148569764","full_name":"Lysxia/generic-data-surgery","owner":"Lysxia","description":"Surgery for generic data types","archived":false,"fork":false,"pushed_at":"2021-01-25T03:46:34.000Z","size":62,"stargazers_count":25,"open_issues_count":6,"forks_count":1,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-12T06:18:57.315Z","etag":null,"topics":["generics","haskell"],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lysxia.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-13T02:23:22.000Z","updated_at":"2025-01-01T03:36:58.000Z","dependencies_parsed_at":"2022-08-14T04:50:45.258Z","dependency_job_id":null,"html_url":"https://github.com/Lysxia/generic-data-surgery","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysxia%2Fgeneric-data-surgery","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysxia%2Fgeneric-data-surgery/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysxia%2Fgeneric-data-surgery/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lysxia%2Fgeneric-data-surgery/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lysxia","download_url":"https://codeload.github.com/Lysxia/generic-data-surgery/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248674659,"owners_count":21143760,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generics","haskell"],"created_at":"2024-09-30T15:44:09.109Z","updated_at":"2025-04-13T06:32:08.617Z","avatar_url":"https://github.com/Lysxia.png","language":"Haskell","readme":"# Surgery for generic data types [![Hackage](https://img.shields.io/hackage/v/generic-data-surgery.svg)](https://hackage.haskell.org/package/generic-data-surgery) [![GitHub CI](https://github.com/Lysxia/generic-data-surgery/workflows/CI/badge.svg)](https://github.com/Lysxia/generic-data-surgery/actions)\n\nModify, add, or remove constructors and fields in generic types, to be used\nwith generic implementations.\n\n## Example\n\nHere is a simple record type equipped with a `checksum` function:\n\n```haskell\ndata Foo = Foo { x, y, z :: Int }\n  deriving (Eq, Generic, Show)\n\nchecksum :: Foo -\u003e Checksum\n```\n\nLet's encode it as a JSON object with an extra `\"checksum\"` key,\nlooking like this, where `X`, `Y`, `Z` are integers:\n\n```\n{ \"x\": X\n, \"y\": Y\n, \"z\": Z\n, \"checksum\": X + Y + Z\n}\n```\n\nWe use `genericParseJSON`/`genericToJSON` to convert between JSON values\nand a generic 4-field record, and `removeRField`/`insertRField` to\nconvert between that generic 4-field record and the 3-field `Foo`.\n\n### Remove field\n\nWhen decoding, we check the checksum and then throw it away.\n\n```haskell\ninstance FromJSON Foo where\n  parseJSON v = do\n\n    r \u003c- genericParseJSON defaultOptions v\n    -- r: a generic 4-field record {x,y,z,checksum} (checksum at index 3).\n\n    let (cs, f) = (fmap fromOR . removeRField @\"checksum\" @3 . toOR') r\n    -- removeRField @\"checksum\" @3: split out the checksum field\n    -- from the three other fields. (cs, f) :: (Checksum, Foo)\n\n    if checksum f == cs then\n      pure f\n    else\n      fail \"Checksum failed\"\n```\n\n### Insert field\n\nWhen encoding, we must compute the checksum to write it out. We put the\nchecksum in a pair `(checksum f, f)` with the original record, and\n`insertRField` can then wrap it into a 4-field record passed into\n`genericToJSON`.\n\n```haskell\ninstance ToJSON Foo where\n  toJSON f =\n    (genericToJSON defaultOptions . fromOR' . insertRField @\"checksum\" @3 . fmap toOR)\n      (checksum f, f)\n```\n\n## See also\n\n- [*Surgery for data types*](https://blog.poisson.chat/posts/2018-11-26-type-surgery.html),\n  introductory blog post with another example.\n\n- The [`examples/`](https://github.com/Lysxia/generic-data-surgery/tree/master/examples)\n  directory in the source repo.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flysxia%2Fgeneric-data-surgery","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flysxia%2Fgeneric-data-surgery","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flysxia%2Fgeneric-data-surgery/lists"}