{"id":13727560,"url":"https://github.com/leviysoft/Visor","last_synced_at":"2025-05-07T23:31:37.487Z","repository":{"id":191912850,"uuid":"685578983","full_name":"leviysoft/visor","owner":"leviysoft","description":".NET optics library","archived":false,"fork":false,"pushed_at":"2023-11-20T18:16:48.000Z","size":57,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-16T00:17:07.471Z","etag":null,"topics":["dotnet","lens","optics","records"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leviysoft.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-08-31T14:45:00.000Z","updated_at":"2024-04-11T06:59:57.000Z","dependencies_parsed_at":"2023-09-01T13:55:42.006Z","dependency_job_id":"dd6cd52c-c21f-4ab1-8408-b3d5b7ea5868","html_url":"https://github.com/leviysoft/visor","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"b7d31e749c02830c99124021528d0f9bfc1d572b"},"previous_names":["leviysoft/visor"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leviysoft%2Fvisor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leviysoft%2Fvisor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leviysoft%2Fvisor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leviysoft%2Fvisor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leviysoft","download_url":"https://codeload.github.com/leviysoft/visor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224672770,"owners_count":17350805,"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":["dotnet","lens","optics","records"],"created_at":"2024-08-03T02:00:22.651Z","updated_at":"2024-11-14T18:31:10.233Z","avatar_url":"https://github.com/leviysoft.png","language":"C#","readme":"# Visor\n\nLeviySoft.Visor is an optics library for .NET\n\n## Rationale\n\nC# 9 introduced record types, which are a first-class implementation of immutable data types.\nWhile records are very useful for domain modeling, it can be pretty akward to modify deeply nested structures:\n\n```csharp\nrecord InnerItem(int Id, string Name);\nrecord ItemWrapper(InnerItem Item, ...);\nrecord Warehouse(ItemWrapper Main, ...);\n\nvar warehouse = new WareHouse(..); //We want to modify Id of InnerItem here\n\nvar warehouse2 = warehouse with {\n    Main = warehouse.Main with {\n        Item = warehouse.Main.Item with {\n            Id = 42 //Here is our new Id\n        }\n    }\n}\n```\n\nThis problem becomes more annoying with every new level of nesting,\nand here optics come on the scene. The overall concept of optics\nis to abstract accessing (and modifying) immutable data from\nthe data itself. Let's generate `Lens` optic for the case above and see,\nhow can we re-write our modification:\n\n```csharp\n[Optics] partial record InnerItem(int Id, string Name);\n[Optics(withNested: true)] partial record ItemWrapper(InnerItem Item, ...);\n[Optics(withNested: true)] partial record Warehouse(ItemWrapper Main, ...);\n\nvar warehouse = new WareHouse(..);\n\nvar warehouse2 = Warehouse.FocusMain.FocusItem.IdLens.Set(42)(warehouse);\n```\n\n`withNested` controls \"chained\" Lens generation.\n\n`Lens` is not the only optic on the table, with `Prism` for example you can\nabstract over subtyping, with `Traverse` over enumerable structures etc.\n\n## How-to\n\n- install `LeviySoft.Visor` and `LeviySoft.Visor.Gen`\n- add `[Optics]` attribute on your record and make your record partial\n\n```csharp\n[Optics]\npartial record Sample(int Id, string Description);\n```\n\n- now you can use `Sample` static class containing optics:\n\n```csharp\nvar id = Sample.IdLens.Get(...);\n\nSample.DescriptionLens.Set(\"replaced\")(...);\n```\n\n## Dealing with immutable collections \u0026 nullable properties\n\n`Visor` comes with a useful set of optics for manipulating values at nullable anr/or collection properties.\nAlthough `System.Collections.Immutable` is supported I strongly recommend using language-ext's immutable collections\nbecause standart ones breaks value equality of records.\n\nHere is a brief example of using build-in optics:\n\n```csharp\n[Optics] internal partial record InnerItem(int Id, string Name);\n[Optics] internal partial record Aux(string Comment, IImmutableList\u003cstring\u003e Tags);\n[Optics(withNested: true)] internal partial record ItemWrapper(InnerItem Item, Aux? Aux);\n[Optics(withNested: true)] internal partial record Warehouse(ItemWrapper Main);\n\nvar warehouse = new Warehouse(new ItemWrapper(new InnerItem(1, \"Default\"), new Aux(\"test\", ImmutableList.Create(\"tag1\", \"tag2\"))));\nvar telescope = Warehouse.FocusMain.AuxLens.NotNull().Compose(Aux.TagsLens).Compose(Property.IImmutableList.First\u003cstring\u003e());\nvar warehouse2 = telescope.Set(\"updated_tag\")(warehouse); //Now warehouse2.Main.Aux?.Tags is (\"updated_tag\", \"tag2\")\n```\n\n## Coming soon\n- Prism\n- Traverse\n- Fold\n- Iso\n\n## Other notes\n\nLeviySoft.Visor is an independently maintained fork of Tinkoff.Visor and is not related to Tinkoff in any kind.","funding_links":[],"categories":["Source Generators"],"sub_categories":["Functional Programming"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleviysoft%2FVisor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleviysoft%2FVisor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleviysoft%2FVisor/lists"}