Skip to content

Commit

Permalink
Rename and remove pointer usage of union container
Browse files Browse the repository at this point in the history
  • Loading branch information
butzopower committed Mar 23, 2022
1 parent e4b7f31 commit 8a081fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
30 changes: 15 additions & 15 deletions adt/adt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package adt

import "adtExample/lib"

type ofUnion interface {
type Union interface {
lib.TypeA | lib.TypeB
}

type union interface {
type unionContainer interface {
atd()
}

Expand All @@ -22,26 +22,26 @@ type TypeBContainer struct {

func (t TypeBContainer) atd() {}

type Atd struct {
v union
type TaggedUnion struct {
v unionContainer
}

// Of
// UnionOf

func Of[T ofUnion](union T) *Atd {
func UnionOf[T Union](union T) TaggedUnion {
var t interface{}
t = union
switch v := t.(type) {
case lib.TypeA:
return &Atd{TypeAContainer{v}}
return TaggedUnion{TypeAContainer{v}}
case lib.TypeB:
return &Atd{TypeBContainer{v}}
return TaggedUnion{TypeBContainer{v}}
default:
panic("called Of with disallowed type")
panic("called UnionOf with disallowed type")
}
}

// Do
// UnionExecutor

type doFnTypeAFn func(aType lib.TypeA)
type doFnTypeBFn func(aType lib.TypeB)
Expand Down Expand Up @@ -73,7 +73,7 @@ func (chain *doChainA) WithTypeA(fn doFnTypeAFn) *doChainB {
return &doChainB{chain.doBuilder}
}

func (e doChainExecutor) Exec(of *Atd) {
func (e doChainExecutor) Exec(of TaggedUnion) {
switch container := of.v.(type) {
case TypeAContainer:
e.fns.doFnTypeA(container.v)
Expand All @@ -82,7 +82,7 @@ func (e doChainExecutor) Exec(of *Atd) {
}
}

func Do() *doChainA {
func UnionExecutor() *doChainA {
return &doChainA{doFns{}}
}

Expand Down Expand Up @@ -118,17 +118,17 @@ func (chain mapChainB[T]) WithTypeB(fn mapFnTypeBFn[T]) *mapChainMapper[T] {
return &mapChainMapper[T]{chain.fns}
}

func (mapper mapChainMapper[T]) Map(of *Atd) T {
func (mapper mapChainMapper[T]) Map(of TaggedUnion) T {
switch container := of.v.(type) {
case TypeAContainer:
return mapper.fns.mapFnTypeA(container.v)
case TypeBContainer:
return mapper.fns.mapFnTypeB(container.v)
default:
panic("called Of with disallowed type")
panic("called UnionOf with disallowed type")
}
}

func Mapper[T any]() *mapChainA[T] {
func UnionMapper[T any]() *mapChainA[T] {
return &mapChainA[T]{mapFns[T]{}}
}
18 changes: 9 additions & 9 deletions adt/adt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ func TestExecutingWithTwoTypes(t *testing.T) {
typeACalled := false
typeBCalled := false

do := adt.Do().
do := adt.UnionExecutor().
WithTypeA(func(typeA lib.TypeA) { typeACalled = true }).
WithTypeB(func(typeB lib.TypeB) { typeBCalled = true })

do.Exec(adt.Of(lib.TypeA{}))
do.Exec(adt.UnionOf(lib.TypeA{}))

require.Equal(t, typeACalled, true)
require.Equal(t, typeBCalled, false)

typeACalled = false
typeBCalled = false

do.Exec(adt.Of(lib.TypeB{}))
do.Exec(adt.UnionOf(lib.TypeB{}))

require.Equal(t, typeACalled, false)
require.Equal(t, typeBCalled, true)
}

func TestMapWithTwoTypes(t *testing.T) {
stringMapper := adt.Mapper[string]().
stringMapper := adt.UnionMapper[string]().
WithTypeA(func(typeA lib.TypeA) string { return "type-a" }).
WithTypeB(func(typeB lib.TypeB) string { return "type-b" })

typeAMappedString := stringMapper.Map(adt.Of(lib.TypeA{}))
typeBMappedString := stringMapper.Map(adt.Of(lib.TypeB{}))
typeAMappedString := stringMapper.Map(adt.UnionOf(lib.TypeA{}))
typeBMappedString := stringMapper.Map(adt.UnionOf(lib.TypeB{}))

require.Equal(t, "type-a", typeAMappedString)
require.Equal(t, "type-b", typeBMappedString)

intMapper := adt.Mapper[int]().
intMapper := adt.UnionMapper[int]().
WithTypeA(func(typeA lib.TypeA) int { return 1000 }).
WithTypeB(func(typeB lib.TypeB) int { return 2000 })

typeAMappedInt := intMapper.Map(adt.Of(lib.TypeA{}))
typeBMappedInt := intMapper.Map(adt.Of(lib.TypeB{}))
typeAMappedInt := intMapper.Map(adt.UnionOf(lib.TypeA{}))
typeBMappedInt := intMapper.Map(adt.UnionOf(lib.TypeB{}))

require.Equal(t, 1000, typeAMappedInt)
require.Equal(t, 2000, typeBMappedInt)
Expand Down

0 comments on commit 8a081fa

Please sign in to comment.