From 570bb25eb56603bf947de60dadb3f2bbb9e5cd4c Mon Sep 17 00:00:00 2001 From: Daniel Slapman Date: Sun, 19 Nov 2023 14:15:08 +0100 Subject: [PATCH] Add chained test & usage example --- readme.md | 19 +++++++++++++++++++ .../ChainedOpticTests.cs | 13 +++++++++++++ src/LeviySoft.Visor.Gen.Tests/Sample.cs | 4 +++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 2522da1..d501977 100644 --- a/readme.md +++ b/readme.md @@ -62,6 +62,25 @@ var id = Sample.IdLens.Get(...); Sample.DescriptionLens.Set("replaced")(...); ``` +## Dealing with immutable collections & nullable properties + +`Visor` comes with a useful set of optics for manipulating values at nullable anr/or collection properties. +Although `System.Collections.Immutable` is supported I strongly recommend using language-ext's immutable collections +because standart ones breaks value equality of records. + +Here is a brief example of using build-in optics: + +```csharp +[Optics] internal partial record InnerItem(int Id, string Name); +[Optics] internal partial record Aux(string Comment, IImmutableList Tags); +[Optics(withNested: true)] internal partial record ItemWrapper(InnerItem Item, Aux? Aux); +[Optics(withNested: true)] internal partial record Warehouse(ItemWrapper Main); + +var warehouse = new Warehouse(new ItemWrapper(new InnerItem(1, "Default"), new Aux("test", ImmutableList.Create("tag1", "tag2")))); +var telescope = Warehouse.FocusMain.AuxLens.NotNull().Compose(Aux.TagsLens).Compose(Property.IImmutableList.First()); +var warehouse2 = telescope.Set("updated_tag")(warehouse); //Now warehouse2.Main.Aux?.Tags is ("updated_tag", "tag2") +``` + ## Coming soon - Prism - Traverse diff --git a/src/LeviySoft.Visor.Gen.Tests/ChainedOpticTests.cs b/src/LeviySoft.Visor.Gen.Tests/ChainedOpticTests.cs index 463e88d..46eca31 100644 --- a/src/LeviySoft.Visor.Gen.Tests/ChainedOpticTests.cs +++ b/src/LeviySoft.Visor.Gen.Tests/ChainedOpticTests.cs @@ -1,5 +1,7 @@ using Shouldly; using Xunit; +using System.Collections.Immutable; +using LeviySoft.Visor.Collections.Immutable; namespace LeviySoft.Visor.Gen.Tests; @@ -14,4 +16,15 @@ public void SetIdTest() warehouse2.Main.Item.Id.ShouldBe(42); } + + [Fact] + public void UpdateTagTest() + { + var warehouse = new Warehouse(new ItemWrapper(new InnerItem(1, "Default"), new Aux("test", ImmutableList.Create("tag1", "tag2")))); + + var telescope = Warehouse.FocusMain.AuxLens.NotNull().Compose(Aux.TagsLens).Compose(Property.IImmutableList.First()); + + var warehouse2 = telescope.Set("updated_tag")(warehouse); + warehouse2.Main.Aux?.Tags.ShouldBe(ImmutableList.Create("updated_tag", "tag2")); + } } \ No newline at end of file diff --git a/src/LeviySoft.Visor.Gen.Tests/Sample.cs b/src/LeviySoft.Visor.Gen.Tests/Sample.cs index 7ab0681..7842061 100644 --- a/src/LeviySoft.Visor.Gen.Tests/Sample.cs +++ b/src/LeviySoft.Visor.Gen.Tests/Sample.cs @@ -1,3 +1,5 @@ +using System.Collections.Immutable; + namespace LeviySoft.Visor.Gen.Tests; [Optics] @@ -8,7 +10,7 @@ internal partial record Sample(int Id, string Description, Inner Inner); [Optics] internal partial record InnerItem(int Id, string Name); -[Optics] internal partial record Aux(string Comment); +[Optics] internal partial record Aux(string Comment, IImmutableList Tags); [Optics(withNested: true)] internal partial record ItemWrapper(InnerItem Item, Aux? Aux);