Skip to content

Commit

Permalink
Add chained test & usage example
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Nov 19, 2023
1 parent aa35302 commit f26dbde
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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<string>());
var warehouse2 = telescope.Set("updated_tag")(warehouse); //Now warehouse2.Main.Aux?.Tags is ("updated_tag", "tag2")
```

## Coming soon
- Prism
- Traverse
Expand Down
13 changes: 13 additions & 0 deletions src/LeviySoft.Visor.Gen.Tests/ChainedOpticTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Shouldly;
using Xunit;
using System.Collections.Immutable;
using LeviySoft.Visor.Collections.Immutable;

namespace LeviySoft.Visor.Gen.Tests;

Expand All @@ -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<string>());

var warehouse2 = telescope.Set("updated_tag")(warehouse);
warehouse2.Main.Aux?.Tags.ShouldBe(ImmutableList.Create("updated_tag", "tag2"));
}
}
4 changes: 3 additions & 1 deletion src/LeviySoft.Visor.Gen.Tests/Sample.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Immutable;

namespace LeviySoft.Visor.Gen.Tests;

[Optics]
Expand All @@ -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<string> Tags);

[Optics(withNested: true)] internal partial record ItemWrapper(InnerItem Item, Aux? Aux);

Expand Down

0 comments on commit f26dbde

Please sign in to comment.