-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic interactor constructor (#8)
- Loading branch information
Showing
8 changed files
with
181 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: tip | ||
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
env: | ||
GO111MODULE: "on" | ||
RUN_BASE_COVERAGE: "on" # Runs test for PR base in case base test coverage is missing. | ||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Go cache | ||
uses: actions/cache@v2 | ||
with: | ||
# In order: | ||
# * Module download cache | ||
# * Build cache (Linux) | ||
path: | | ||
~/go/pkg/mod | ||
~/.cache/go-build | ||
key: ${{ runner.os }}-go-cache-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go-cache | ||
- name: Test | ||
uses: docker://aleksi/golang-tip:master | ||
with: | ||
entrypoint: /bin/sh | ||
args: -c "go version;make test-unit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package usecase | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type IOInteractorOf[i, o interface{}] struct { | ||
IOInteractor | ||
|
||
InteractFunc func(ctx context.Context, input i, output *o) error | ||
} | ||
|
||
func (ioi IOInteractorOf[i, o]) Invoke(ctx context.Context, input i, output *o) error { | ||
return ioi.InteractFunc(ctx, input, output) | ||
} | ||
|
||
// NewInteractor creates generic use case interactor with input and output ports. | ||
// | ||
// It pre-fills name and title with caller function. | ||
// Input is passed by value, while output is passed by pointer to be mutable. | ||
func NewInteractor[i, o any](interact func(ctx context.Context, input i, output *o) error) IOInteractorOf[i, o] { | ||
u := IOInteractorOf[i, o]{} | ||
u.Input = *new(i) | ||
u.Output = new(o) | ||
u.InteractFunc = interact | ||
u.Interactor = Interact(func(ctx context.Context, input, output interface{}) error { | ||
inp, ok := input.(i) | ||
if !ok { | ||
return fmt.Errorf("invalid input type received: %T, expected: %T", input, u.Input) | ||
} | ||
|
||
out, ok := output.(*o) | ||
if !ok { | ||
return fmt.Errorf("invalid output type received: %T, expected: %T", output, u.Output) | ||
} | ||
|
||
return interact(ctx, inp, out) | ||
}) | ||
|
||
u.name, u.title = callerFunc() | ||
u.name = filterName(u.name) | ||
|
||
return u | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package usecase_test | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/swaggest/usecase" | ||
) | ||
|
||
func TestNewIOI_classic(t *testing.T) { | ||
u := usecase.NewIOI(*new(int), new(string), func(ctx context.Context, input, output interface{}) error { | ||
in := input.(int) | ||
out := output.(*string) | ||
|
||
*out = strconv.Itoa(in) | ||
|
||
return nil | ||
}) | ||
|
||
ctx := context.Background() | ||
|
||
var out string | ||
assert.NoError(t, u.Interact(ctx, 123, &out)) | ||
assert.Equal(t, "123", out) | ||
} | ||
|
||
func TestNewInteractor(t *testing.T) { | ||
u := usecase.NewInteractor(func(ctx context.Context, input int, output *string) error { | ||
*output = strconv.Itoa(input) | ||
|
||
return nil | ||
}) | ||
|
||
u.SetDescription("Foo.") | ||
|
||
ctx := context.Background() | ||
|
||
var out string | ||
assert.NoError(t, u.Interact(ctx, 123, &out)) | ||
assert.Equal(t, "123", out) | ||
|
||
out = "" | ||
assert.NoError(t, u.Invoke(ctx, 123, &out)) | ||
assert.Equal(t, "123", out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
module github.com/swaggest/usecase | ||
|
||
go 1.12 | ||
go 1.18 | ||
|
||
require ( | ||
github.com/bool64/dev v0.1.41 | ||
github.com/bool64/dev v0.1.42 | ||
github.com/stretchr/testify v1.4.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.0 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v2 v2.2.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters