Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
dbed8eed
Unverified
Commit
dbed8eed
authored
Aug 21, 2023
by
Carter
Committed by
GitHub
Aug 21, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cleanup stringer types with ~string generic (#6920)
* Cleanup stringer types with ~string generic * Fix linter error
parent
4af15d02
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
5 additions
and
38 deletions
+5
-38
enum.go
op-service/enum/enum.go
+2
-20
enum_test.go
op-service/enum/enum_test.go
+3
-18
No files found.
op-service/enum/enum.go
View file @
dbed8eed
package
enum
package
enum
import
(
import
(
"fmt"
"strings"
"strings"
)
)
// Stringered wraps the string type to implement the fmt.Stringer interface.
type
Stringered
string
// String returns the string value.
func
(
s
Stringered
)
String
()
string
{
return
string
(
s
)
}
// StringeredList converts a list of strings to a list of Stringered.
func
StringeredList
(
values
[]
string
)
[]
Stringered
{
var
out
[]
Stringered
for
_
,
v
:=
range
values
{
out
=
append
(
out
,
Stringered
(
v
))
}
return
out
}
// EnumString returns a comma-separated string of the enum values.
// EnumString returns a comma-separated string of the enum values.
// This is primarily used to generate a cli flag.
// This is primarily used to generate a cli flag.
func
EnumString
[
T
fmt
.
Stringer
](
values
[]
T
)
string
{
func
EnumString
[
T
~
string
](
values
[]
T
)
string
{
var
out
strings
.
Builder
var
out
strings
.
Builder
for
i
,
v
:=
range
values
{
for
i
,
v
:=
range
values
{
out
.
WriteString
(
v
.
String
(
))
out
.
WriteString
(
string
(
v
))
if
i
+
1
<
len
(
values
)
{
if
i
+
1
<
len
(
values
)
{
out
.
WriteString
(
", "
)
out
.
WriteString
(
", "
)
}
}
...
...
op-service/enum/enum_test.go
View file @
dbed8eed
...
@@ -8,30 +8,15 @@ import (
...
@@ -8,30 +8,15 @@ import (
// TestEnumString_MultipleInputs tests the EnumString function with multiple inputs.
// TestEnumString_MultipleInputs tests the EnumString function with multiple inputs.
func
TestEnumString_MultipleInputs
(
t
*
testing
.
T
)
{
func
TestEnumString_MultipleInputs
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"a, b, c"
,
EnumString
([]
Stringered
{
"a"
,
"b"
,
"c"
}))
require
.
Equal
(
t
,
"a, b, c"
,
EnumString
([]
string
{
"a"
,
"b"
,
"c"
}))
}
}
// TestEnumString_SingleString tests the EnumString function with a single input.
// TestEnumString_SingleString tests the EnumString function with a single input.
func
TestEnumString_SingleString
(
t
*
testing
.
T
)
{
func
TestEnumString_SingleString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
"a"
,
EnumString
([]
Stringered
{
"a"
}))
require
.
Equal
(
t
,
"a"
,
EnumString
([]
string
{
"a"
}))
}
}
// TestEnumString_EmptyString tests the EnumString function with no inputs.
// TestEnumString_EmptyString tests the EnumString function with no inputs.
func
TestEnumString_EmptyString
(
t
*
testing
.
T
)
{
func
TestEnumString_EmptyString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
""
,
EnumString
([]
Stringered
{}))
require
.
Equal
(
t
,
""
,
EnumString
([]
string
{}))
}
// TestStringeredList_MultipleInputs tests the StringeredList function with multiple inputs.
func
TestStringeredList_MultipleInputs
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
{
"a"
,
"b"
,
"c"
},
StringeredList
([]
string
{
"a"
,
"b"
,
"c"
}))
}
// TestStringeredList_SingleString tests the StringeredList function with a single input.
func
TestStringeredList_SingleString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
{
"a"
},
StringeredList
([]
string
{
"a"
}))
}
// TestStringeredList_EmptyString tests the StringeredList function with no inputs.
func
TestStringeredList_EmptyString
(
t
*
testing
.
T
)
{
require
.
Equal
(
t
,
[]
Stringered
(
nil
),
StringeredList
([]
string
{}))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment