Skip to content

Commit

Permalink
- add Support for Compilerpasses
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryVolkmer committed Jul 17, 2023
1 parent 88ed247 commit 1176339
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
8 changes: 8 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Container struct {
Building map[any]bool
// Tags
TaggedServices map[string][]string
compilerPasses []func(c *Container)
}

func NewContainer() *Container {
Expand Down Expand Up @@ -111,11 +112,18 @@ func (this *Container) Add(id string, service any) *Definition {
return this.Definitions[id]
}

func (this *Container) AddCompilerPass(cpfn func(c *Container)) {
this.compilerPasses = append(this.compilerPasses,cpfn)
}

func (this *Container) Compile() {
for id,definition := range this.Definitions {
this.Instances[id] = this.build(definition)
}
this.Compiled = true
for _,cpfn := range this.compilerPasses {
cpfn(this)
}
}

func (this *Container) build(def *Definition) any {
Expand Down
44 changes: 43 additions & 1 deletion container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ const USERNAME = "JohnDoo"
const PASSWORD = "123"

type ServiceOneFixture struct {
ServiceTwoFixture *ServiceTwoFixture `service:"di/ServiceTwoFixture"`
ServiceTwoFixture *ServiceTwoFixture `service:"di/ServiceTwoFixture"`
Strings []string
}

func (this *ServiceOneFixture) AddString(s string) {
this.Strings = append(this.Strings,s)
}

type StringAdder interface {
GetString() string
}

type ServiceTwoFixture struct {
Username string `serviceparam:"username"`
Password string `serviceparam:"password"`
}
func (this *ServiceTwoFixture) GetString() string {
return this.Username
}


type ServiceThreeFixture struct {
Username string `serviceparam:"username"`
Expand Down Expand Up @@ -110,4 +123,33 @@ func TestServiceCanBeTagged(t *testing.T) {
if baz != nil {
t.Errorf("Amount of Tagged baz should be 0 but %d found!",len(bar))
}
}

func TestCompilerPass(t *testing.T) {
c := NewContainer()
c.AddParameter("username",USERNAME)
c.AddParameter("password",PASSWORD)
c.Add("di/ServiceOneFixture",&ServiceOneFixture{})
c.Add("di/ServiceTwoFixture",&ServiceTwoFixture{}).Tag("string.adder")
c.AddCompilerPass(func(c *Container) {
service,ok := c.Get("di/ServiceOneFixture").(*ServiceOneFixture)
if !ok {
t.Errorf("No di/ServiceOneFixture found!")
return
}
services,ok := c.GetTaggedServices("string.adder")
if !ok {
t.Errorf("No Services with tag string.adder found!")
return
}
for _,taggedservice := range services {
implTaggedService := taggedservice.(StringAdder)
service.AddString(implTaggedService.GetString())
}
})
c.Compile()
service := c.Get("di/ServiceOneFixture").(*ServiceOneFixture)
if len(service.Strings) != 1 {
t.Errorf("Compilerpass was not added!")
}
}

0 comments on commit 1176339

Please sign in to comment.