-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(initializer): universial initializer
defines universial initializer interfaces and universal entry for invoke initializer's Init method
- Loading branch information
Showing
3 changed files
with
92 additions
and
8 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,31 @@ | ||
package initializer | ||
|
||
import "context" | ||
|
||
type ( | ||
_Initializer interface{ Init() } | ||
_WithError interface{ Init() error } | ||
_ByContext interface{ Init(context.Context) } | ||
_ByContextWithError interface{ Init(context.Context) error } | ||
) | ||
|
||
func InitByContext(ctx context.Context, initializer any) error { | ||
switch v := initializer.(type) { | ||
case _Initializer: | ||
v.Init() | ||
return nil | ||
case _WithError: | ||
return v.Init() | ||
case _ByContext: | ||
v.Init(ctx) | ||
return nil | ||
case _ByContextWithError: | ||
return v.Init(ctx) | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func Init(initializer any) error { | ||
return InitByContext(context.Background(), initializer) | ||
} |
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,57 @@ | ||
package initializer_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/sincospro/datatypes/initializer" | ||
) | ||
|
||
var ( | ||
errInitializerWithError = errors.New("initialize with error") | ||
errInitializerByContextWithError = errors.New("initialize by context with error") | ||
) | ||
|
||
type Initializer struct{} | ||
|
||
func (i *Initializer) Init() {} | ||
|
||
type InitializerWithError struct{} | ||
|
||
func (i *InitializerWithError) Init() error { | ||
return errInitializerWithError | ||
} | ||
|
||
type InitializerByContext struct{} | ||
|
||
func (i *InitializerByContext) Init(_ context.Context) {} | ||
|
||
type InitializerByContextWithError struct{} | ||
|
||
func (i *InitializerByContextWithError) Init(_ context.Context) error { | ||
return errInitializerByContextWithError | ||
} | ||
|
||
func TestInit(t *testing.T) { | ||
results := []error{ | ||
nil, | ||
errInitializerWithError, | ||
nil, | ||
errInitializerByContextWithError, | ||
} | ||
for i, v := range []any{ | ||
&Initializer{}, | ||
&InitializerWithError{}, | ||
&InitializerByContext{}, | ||
&InitializerByContextWithError{}, | ||
} { | ||
if results[i] == nil { | ||
NewWithT(t).Expect(initializer.Init(v)).To(BeNil()) | ||
} else { | ||
NewWithT(t).Expect(initializer.Init(v)).To(Equal(results[i])) | ||
} | ||
} | ||
} |
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