-
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.
Reduce cognitive complexity of router builder code
- Loading branch information
Showing
3 changed files
with
48 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
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 utils | ||
|
||
// GetOptionalOrFallback returns pointer to value if value is not empty or fallback pointer otherwise | ||
func GetOptionalOrFallback[T any](value *T, fallback *T) *T { | ||
if value == nil { | ||
return fallback | ||
} | ||
return value | ||
} |
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,34 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetOptionalOrFallback(t *testing.T) { | ||
someValue, someFallback := PointerTo("someValue"), PointerTo("someFallback") | ||
tests := map[string]struct { | ||
value *string | ||
fallback *string | ||
expectedValue *string | ||
}{ | ||
"value is not nil": { | ||
value: someValue, | ||
fallback: someFallback, | ||
expectedValue: someValue, | ||
}, | ||
"value is nil": { | ||
value: nil, | ||
fallback: someFallback, | ||
expectedValue: someFallback, | ||
}, | ||
} | ||
|
||
for name, tc := range tests { | ||
name, tc := name, tc | ||
t.Run(name, func(t *testing.T) { | ||
require.Equal(t, tc.expectedValue, GetOptionalOrFallback(tc.value, tc.fallback)) | ||
}) | ||
} | ||
} |