Skip to content

Commit

Permalink
R/0.5.2 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
firasdarwish authored Nov 15, 2024
2 parents ee9933b + 9f87f10 commit 9336c47
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 455 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ jobs:
- name: Test
run: go test -v ./...

- name: Build all example projects
run: |
for dir in examples/*/; do
if [ -d "$dir" ]; then
echo "Building project in $dir"
cd "$dir" || exit 1
go mod tidy
go build -v . || exit 1
cd - || exit 1
fi
done
- name: Run coverage
run: go test -race -coverprofile=coverage.out -covermode=atomic

Expand Down
218 changes: 79 additions & 139 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (c *simpleCounter) GetCount() int {
}

func (c *simpleCounter) New(ctx context.Context) (Counter, context.Context) {
return &simpleCounter{}, ctx
return &models.SimpleCounter{}, ctx
}
```

Expand All @@ -95,155 +95,116 @@ func (c *simpleCounter) New(ctx context.Context) (Counter, context.Context) {
### Eager Singleton

```go
package main
var c Counter
c = &models.SimpleCounter{}

import (
"context"
"github.com/firasdarwish/ore"
)
// register
ore.RegisterEagerSingleton[Counter](c)

func main() {
var c Counter
c = &simpleCounter{}

// register
ore.RegisterEagerSingleton[Counter](c)

ctx := context.Background()
ctx := context.Background()

// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()
}
// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()
```

<br />

### Lazy (using Creator[T] interface)

```go
package main
// register
ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{})

import (
"context"
"fmt"
"github.com/firasdarwish/ore"
)
// OR
//ore.RegisterLazyCreator[Counter](ore.Transient, &models.SimpleCounter{})
//ore.RegisterLazyCreator[Counter](ore.Singleton, &models.SimpleCounter{})

func main() {
// register
ore.RegisterLazyCreator[Counter](ore.Scoped, &simpleCounter{})

// OR
//ore.RegisterLazyCreator[Counter](ore.Transient, &simpleCounter{})
//ore.RegisterLazyCreator[Counter](ore.Singleton, &simpleCounter{})

ctx := context.Background()
ctx := context.Background()

// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()
// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()

// retrieve again
c, ctx = ore.Get[Counter](ctx)
c.AddOne()
// retrieve again
c, ctx = ore.Get[Counter](ctx)
c.AddOne()

// prints out: `TOTAL: 3`
fmt.Println("TOTAL: ", c.GetCount())
}
// prints out: `TOTAL: 3`
fmt.Println("TOTAL: ", c.GetCount())
```

<br />

### Lazy (using anonymous func)

```go
package main

import (
"context"
"fmt"
"github.com/firasdarwish/ore"
)

func main() {
// register
ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) {
return &simpleCounter{}, ctx
})
ore.RegisterLazyFunc[Counter](ore.Scoped, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
})

// OR
//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
// return &simpleCounter{}, ctx
//})
// OR
//ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
// return &models.SimpleCounter{}, ctx
//})

// Keyed service registration
//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
// return &simpleCounter{}, ctx
//}, "name here", 1234)
// Keyed service registration
//ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
// return &models.SimpleCounter{}, ctx
//}, "name here", 1234)

ctx := context.Background()
ctx := context.Background()

// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()
// retrieve
c, ctx := ore.Get[Counter](ctx)
c.AddOne()
c.AddOne()

// Keyed service retrieval
//c, ctx := ore.Get[Counter](ctx, "name here", 1234)
// Keyed service retrieval
//c, ctx := ore.Get[Counter](ctx, "name here", 1234)

// retrieve again
c, ctx = ore.Get[Counter](ctx)
c.AddOne()
// retrieve again
c, ctx = ore.Get[Counter](ctx)
c.AddOne()

// prints out: `TOTAL: 3`
fmt.Println("TOTAL: ", c.GetCount())
}
// prints out: `TOTAL: 3`
fmt.Println("TOTAL: ", c.GetCount())
```

<br />

### Several Implementations

```go
package main

import (
"context"
"github.com/firasdarwish/ore"
)

func main() {
// register
ore.RegisterLazyCreator[Counter](ore.Scoped, &simpleCounter{})
ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{})

ore.RegisterLazyCreator[Counter](ore.Scoped, &yetAnotherCounter{})
ore.RegisterLazyCreator[Counter](ore.Scoped, &yetAnotherCounter{})

ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
return &simpleCounter{}, ctx
})
ore.RegisterLazyFunc[Counter](ore.Transient, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
})

ore.RegisterLazyCreator[Counter](ore.Singleton, &yetAnotherCounter{})
ore.RegisterLazyCreator[Counter](ore.Singleton, &yetAnotherCounter{})

ctx := context.Background()
ctx := context.Background()

// returns a slice of `Counter` implementations
counters, ctx := ore.GetList[Counter](ctx)
// returns a slice of `Counter` implementations
counters, ctx := ore.GetList[Counter](ctx)

// to retrieve a slice of keyed services
//counters, ctx := ore.GetList[Counter](ctx, "my integer counters")
// to retrieve a slice of keyed services
//counters, ctx := ore.GetList[Counter](ctx, "my integer counters")

for _, c := range counters {
for _, c := range counters {
c.AddOne()
}

// It will always return the LAST registered implementation
defaultImplementation, ctx := ore.Get[Counter](ctx) // simpleCounter
defaultImplementation.AddOne()
}

// It will always return the LAST registered implementation
defaultImplementation, ctx := ore.Get[Counter](ctx) // simpleCounter
defaultImplementation.AddOne()
```

#### Injecting Mocks in Tests
Expand All @@ -255,34 +216,23 @@ The last registered implementation takes precedence, so you can register a mock
### Keyed Services Retrieval Example

```go
package main

import (
"context"
"fmt"
"github.com/firasdarwish/ore"
)

func main() {
// register
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
return &simpleCounter{}, ctx
}, "name here", 1234)
ore.RegisterLazyFunc[Counter](ore.Singleton, func(ctx context.Context) (Counter, context.Context) {
return &models.SimpleCounter{}, ctx
}, "name here", 1234)

//ore.RegisterLazyCreator[Counter](ore.Scoped, &simpleCounter{}, "name here", 1234)
//ore.RegisterLazyCreator[Counter](ore.Scoped, &models.SimpleCounter{}, "name here", 1234)

//ore.RegisterEagerSingleton[Counter](&simpleCounter{}, "name here", 1234)
//ore.RegisterEagerSingleton[Counter](&models.SimpleCounter{}, "name here", 1234)

ctx := context.Background()
ctx := context.Background()

// Keyed service retrieval
c, ctx := ore.Get[Counter](ctx, "name here", 1234)
c.AddOne()

// prints out: `TOTAL: 1`
fmt.Println("TOTAL: ", c.GetCount())
}
// Keyed service retrieval
c, ctx := ore.Get[Counter](ctx, "name here", 1234)
c.AddOne()

// prints out: `TOTAL: 1`
fmt.Println("TOTAL: ", c.GetCount())
```
<br />

Expand Down Expand Up @@ -561,24 +511,14 @@ func (gc *genericCounter[T]) GetCount(ctx context.Context) T {
```

```go
package main

import (
"context"
"github.com/firasdarwish/ore"
)

func main() {

// register
ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) {
// register
ore.RegisterLazyFunc[GenericCounter[int]](ore.Scoped, func(ctx context.Context) (GenericCounter[int], context.Context) {
return &genericCounter[int]{}, ctx
})

// retrieve
c, ctx := ore.Get[GenericCounter[int]](ctx)
}
})

// retrieve
c, ctx := ore.Get[GenericCounter[int]](ctx)
```

<br />
Expand Down
31 changes: 9 additions & 22 deletions alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ore

import (
"context"
"github.com/firasdarwish/ore/internal/interfaces"
"testing"

m "github.com/firasdarwish/ore/internal/models"
Expand Down Expand Up @@ -137,12 +138,12 @@ func TestGetGenericAlias(t *testing.T) {
for _, registrationType := range types {
container := NewContainer()

RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*simpleCounterUint, context.Context) {
return &simpleCounterUint{}, ctx
RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
RegisterAliasToContainer[someCounterGeneric[uint], *simpleCounterUint](container)
RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container)

c, _ := GetFromContainer[someCounterGeneric[uint]](container, context.Background())
c, _ := GetFromContainer[interfaces.SomeCounterGeneric[uint]](container, context.Background())

c.Add(1)
c.Add(1)
Expand All @@ -156,14 +157,14 @@ func TestGetListGenericAlias(t *testing.T) {
container := NewContainer()

for i := 0; i < 3; i++ {
RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*simpleCounterUint, context.Context) {
return &simpleCounterUint{}, ctx
RegisterLazyFuncToContainer(container, registrationType, func(ctx context.Context) (*m.SimpleCounterUint, context.Context) {
return &m.SimpleCounterUint{}, ctx
})
}

RegisterAliasToContainer[someCounterGeneric[uint], *simpleCounterUint](container)
RegisterAliasToContainer[interfaces.SomeCounterGeneric[uint], *m.SimpleCounterUint](container)

counters, _ := GetListFromContainer[someCounterGeneric[uint]](container, context.Background())
counters, _ := GetListFromContainer[interfaces.SomeCounterGeneric[uint]](container, context.Background())
assert.Equal(t, len(counters), 3)

c := counters[1]
Expand All @@ -173,17 +174,3 @@ func TestGetListGenericAlias(t *testing.T) {
assert.Equal(t, uint(2), c.GetCount())
}
}

var _ someCounterGeneric[uint] = (*simpleCounterUint)(nil)

type simpleCounterUint struct {
counter uint
}

func (this *simpleCounterUint) Add(number uint) {
this.counter += number
}

func (this *simpleCounterUint) GetCount() uint {
return this.counter
}
Loading

0 comments on commit 9336c47

Please sign in to comment.