generated from ZEISS/template-go
-
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.
- Loading branch information
1 parent
d09b973
commit 54f4387
Showing
2 changed files
with
53 additions
and
0 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,13 @@ | ||
package reflectx | ||
|
||
import "strings" | ||
|
||
// TagOptions ... | ||
type TagOptions string | ||
|
||
// ParseTag splits a struct field's json tag into its name and | ||
// comma-separated options. | ||
func ParseTag(tag string) (string, TagOptions) { | ||
tag, opt, _ := strings.Cut(tag, ",") | ||
return tag, TagOptions(opt) | ||
} |
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,40 @@ | ||
package reflectx_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zeiss/pkg/reflectx" | ||
) | ||
|
||
func TestParseTag(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
tag string | ||
expected string | ||
expectedOptions reflectx.TagOptions | ||
}{ | ||
{ | ||
tag: "name,omitempty", | ||
expected: "name", | ||
expectedOptions: "omitempty", | ||
}, | ||
{ | ||
tag: "name,omitempty,required", | ||
expected: "name", | ||
expectedOptions: "omitempty,required", | ||
}, | ||
{ | ||
tag: "name,omitempty,required,truncate", | ||
expected: "name", | ||
expectedOptions: "omitempty,required,truncate", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
tag, opt := reflectx.ParseTag(test.tag) | ||
assert.Equal(t, test.expected, tag) | ||
assert.Equal(t, test.expectedOptions, opt) | ||
} | ||
} |