diff --git a/agent/nanobot/hoist.go b/agent/nanobot/hoist.go index 9d207d9..98601ba 100644 --- a/agent/nanobot/hoist.go +++ b/agent/nanobot/hoist.go @@ -20,37 +20,34 @@ import ( // Hoist lifts an Arr[T] into Arr[S] by applying an isomorphism between S and T. type Hoister[S, T any] interface{ Hoist(Arr[T]) Arr[S] } -// BiMap creates a partial morphism between S and T. +// Codec creates a partial morphism between S and T. // It allows us to treat S and T as interchangeable for the purpose of applying Arr[T] to S. -func BiMap[S, A, T, B any]() Hoister[S, T] { - return iso[S, A, T, B]{ - sa: optics.ForProduct1[S, A](), - sb: optics.ForProduct1[S, B](), - ta: optics.ForProduct1[T, A](), - tb: optics.ForProduct1[T, B](), - } +func Codec[S, T, A, B any]() Hoister[S, T] { + return iso[S, T]{iso: optics.CodecS1T1[S, T, A, B]()} +} + +// Morph creates a partial morphism between S and T using a custom isomorphism. +func Morph[S, T any](m optics.Isomorphism[S, T]) Hoister[S, T] { + return iso[S, T]{iso: m} } -type iso[S, A, T, B any] struct { - sa optics.Lens[S, A] - sb optics.Lens[S, B] - ta optics.Lens[T, A] - tb optics.Lens[T, B] +type iso[S, T any] struct { + iso optics.Isomorphism[S, T] } // Hoist lifts an Arr[T] into Arr[S] using the isomorphism defined by the iso struct. -func (iso iso[S, A, T, B]) Hoist(arrT Arr[T]) Arr[S] { +func (iso iso[S, T]) Hoist(arrT Arr[T]) Arr[S] { return func(ctx context.Context, s S, opt ...chatter.Opt) (S, error) { t := alloc[T]() - iso.ta.Put(&t, iso.sa.Get(&s)) + iso.iso.Forward(&s, &t) t, err := arrT(ctx, t, opt...) if err != nil { return s, err } - iso.sb.Put(&s, iso.tb.Get(&t)) + iso.iso.Inverse(&t, &s) return s, nil } } diff --git a/agent/nanobot/hoist_test.go b/agent/nanobot/hoist_test.go index 3274e54..d60c969 100644 --- a/agent/nanobot/hoist_test.go +++ b/agent/nanobot/hoist_test.go @@ -35,7 +35,7 @@ func TestHoist(t *testing.T) { } // Create a hoister using BiMap - hoister := nanobot.BiMap[Source, int, Target, string]() + hoister := nanobot.Codec[Source, Target, int, string]() // Define an Arr[Target] that modifies the target arrTarget := func(ctx context.Context, target Target, opt ...chatter.Opt) (Target, error) { @@ -68,7 +68,7 @@ func TestHoist(t *testing.T) { FieldB string } - hoister := nanobot.BiMap[Source, int, Target, string]() + hoister := nanobot.Codec[Source, Target, int, string]() // Arr[Target] that returns an error testErr := errors.New("test error") @@ -99,7 +99,7 @@ func TestHoist(t *testing.T) { B bool } - hoister := nanobot.BiMap[Source, float64, Target, bool]() + hoister := nanobot.Codec[Source, Target, float64, bool]() arrTarget := func(ctx context.Context, target Target, opt ...chatter.Opt) (Target, error) { target.A *= 2 diff --git a/go.mod b/go.mod index e6726a8..9ee21df 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/aws/aws-sdk-go-v2/credentials v1.19.14 github.com/aws/aws-sdk-go-v2/service/sts v1.41.10 github.com/fogfish/faults v0.3.2 - github.com/fogfish/golem/optics v0.14.2 + github.com/fogfish/golem/optics v0.14.3 github.com/fogfish/guid/v2 v2.1.0 github.com/fogfish/it/v2 v2.2.4 github.com/goccy/go-yaml v1.19.2 diff --git a/go.sum b/go.sum index 75bcd75..80d6bca 100644 --- a/go.sum +++ b/go.sum @@ -33,8 +33,8 @@ github.com/fogfish/faults v0.3.2 h1:kQai2/VyXJxfd6SD/jYLHiqu0qDl/KXT48q1ppLMAnY= github.com/fogfish/faults v0.3.2/go.mod h1:y8zvZN2pQUe9vDS7rzz0mAnbdfYMorPOeqxpy83YOCk= github.com/fogfish/golem/hseq v1.3.1 h1:UyCmp44JcXvs7OVXJxnJldur/UpL5skJGTKdDFxYUsU= github.com/fogfish/golem/hseq v1.3.1/go.mod h1:17XORt8nNKl6KOhF43MHSmjK8NksbkBsohAoJGiinUs= -github.com/fogfish/golem/optics v0.14.2 h1:bKu+8kGeieRIgpEyqlGaMPXapNGqDrklc7k4HbSLh5M= -github.com/fogfish/golem/optics v0.14.2/go.mod h1:qbtka94yn+x5U1KDD+UX69C07M6I2J9xgwOciAYYeI8= +github.com/fogfish/golem/optics v0.14.3 h1:QOx9ABahDlcpM44HmSjPJkJ/Q50nd8yohhDthKdgo+s= +github.com/fogfish/golem/optics v0.14.3/go.mod h1:qbtka94yn+x5U1KDD+UX69C07M6I2J9xgwOciAYYeI8= github.com/fogfish/guid/v2 v2.1.0 h1:oEJHKM4yFOOCmKZdh0oH7eD3mL32n2+1YCc27lXB5rE= github.com/fogfish/guid/v2 v2.1.0/go.mod h1:KkZ5T4EE3BqWQJFZBPLSHV/tBe23Xq4KvuPfwtNtepU= github.com/fogfish/it/v2 v2.2.4 h1:hkBePGW7X/wDc1QCLG/j+/j47TG4obnozYsGMX51yMQ=