-
-
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.
Implement options ApplyOptions helper
- Loading branch information
Showing
8 changed files
with
58 additions
and
40 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
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
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,9 @@ | ||
package helpers | ||
|
||
func ApplyOptions[T any](service *T, options []func(*T)) *T { | ||
for _, option := range options { | ||
option(service) | ||
} | ||
|
||
return service | ||
} |
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,38 @@ | ||
package helpers_test | ||
|
||
import ( | ||
"github.com/evg4b/uncors/internal/helpers" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type myService struct { | ||
service string | ||
} | ||
|
||
func TestApplyOptions(t *testing.T) { | ||
t.Run("ApplyOptions sets service value", func(t *testing.T) { | ||
const testValue = "TestValue" | ||
|
||
service := &myService{} | ||
options := []func(*myService){ | ||
func(s *myService) { | ||
s.service = testValue | ||
}, | ||
} | ||
|
||
result := helpers.ApplyOptions(service, options) | ||
|
||
assert.Equal(t, service, result, "The same service should be returned") | ||
assert.Equal(t, testValue, result.service, "Service option should be applied") | ||
}) | ||
|
||
t.Run("ApplyOptions handles empty options", func(t *testing.T) { | ||
service := &myService{} | ||
|
||
result := helpers.ApplyOptions(service, nil) | ||
|
||
assert.Equal(t, service, result, "The same service should be returned") | ||
assert.Equal(t, "", result.service, "Service value should not be applied") | ||
}) | ||
} |
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