Skip to content

Commit

Permalink
Initial ADT example
Browse files Browse the repository at this point in the history
  • Loading branch information
butzopower committed Mar 21, 2022
0 parents commit c27d3c1
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
83 changes: 83 additions & 0 deletions adt/adt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package adt

import "adtExample/lib"

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

type union interface {
atd()
}

type TypeAContainer struct {
v lib.TypeA
}

func (t TypeAContainer) atd() {}

type TypeBContainer struct {
v lib.TypeB
}

func (t TypeBContainer) atd() {}

type Atd struct {
v union
}

type doFnTypeAFn func(aType lib.TypeA)
type doFnTypeBFn func(aType lib.TypeB)

type doFns struct {
doFnTypeA doFnTypeAFn
doFnTypeB doFnTypeBFn
}

type doChainA struct {
doBuilder doFns
}

type doChainB struct {
fns doFns
}

type doChainExecutor struct {
fns doFns
}

func (chain *doChainB) WithTypeB(fn doFnTypeBFn) *doChainExecutor {
chain.fns.doFnTypeB = fn
return &doChainExecutor{chain.fns}
}

func (chain *doChainA) WithTypeA(fn doFnTypeAFn) *doChainB {
chain.doBuilder.doFnTypeA = fn
return &doChainB{chain.doBuilder}
}

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

func (e doChainExecutor) Exec(of *Atd) {
switch container := of.v.(type) {
case TypeAContainer:
e.fns.doFnTypeA(container.v)
case TypeBContainer:
e.fns.doFnTypeB(container.v)
}
}

func Of[T ofUnion](union T) *Atd {
var t interface{}
t = union
switch v := t.(type) {
case lib.TypeA:
return &Atd{TypeAContainer{v}}
case lib.TypeB:
return &Atd{TypeBContainer{v}}
default:
panic("called Of with disallowed type")
}
}
30 changes: 30 additions & 0 deletions adt/adt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package adt_test

import (
"adtExample/adt"
"adtExample/lib"
"github.com/stretchr/testify/require"
"testing"
)

func TestExecutingWithTwoTypes(t *testing.T) {
typeACalled := false
typeBCalled := false

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

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

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

typeACalled = false
typeBCalled = false

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

require.Equal(t, typeACalled, false)
require.Equal(t, typeBCalled, true)
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module adtExample

go 1.18

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
github.com/stretchr/testify v1.7.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
7 changes: 7 additions & 0 deletions lib/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package lib

type TypeA struct {
}

type TypeB struct {
}

0 comments on commit c27d3c1

Please sign in to comment.