-
Notifications
You must be signed in to change notification settings - Fork 8
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
e5f753e
commit f103366
Showing
6 changed files
with
86 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package qstring | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// parseTag splits a struct field's qstring tag into its name and, if an | ||
// optional omitempty option was provided, a boolean indicating this is | ||
// returned | ||
func parseTag(tag string) (string, bool) { | ||
if idx := strings.Index(tag, ","); idx != -1 { | ||
if tag[idx+1:] == "omitempty" { | ||
return tag[:idx], true | ||
} | ||
return tag[:idx], false | ||
} | ||
return tag, false | ||
} |
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 qstring | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTagParsing(t *testing.T) { | ||
testio := []struct { | ||
inp string | ||
output string | ||
omit bool | ||
}{ | ||
{inp: "name,omitempty", output: "name", omit: true}, | ||
{inp: "name", output: "name", omit: false}, | ||
{inp: "name,", output: "name", omit: false}, | ||
{inp: "name", output: "name", omit: false}, | ||
{inp: "", output: "", omit: false}, | ||
{inp: ",omitempty", output: "", omit: true}, | ||
{inp: "-", output: "-", omit: false}, | ||
} | ||
|
||
var name string | ||
var omit bool | ||
for _, test := range testio { | ||
name, omit = parseTag(test.inp) | ||
if name != test.output { | ||
t.Errorf("Expected tag name to be %q, got %q instead", test.output, name) | ||
} | ||
|
||
if omit != test.omit { | ||
t.Errorf("Expected omitempty to be %t, got %t instead", test.omit, omit) | ||
} | ||
} | ||
} | ||
|
||
func TestOmitEmpty(t *testing.T) { | ||
vis := 5 | ||
testio := []struct { | ||
Visible int | ||
Conditional int `qstring:"conditional,omitempty"` | ||
omit bool | ||
}{ | ||
{Visible: vis, Conditional: 5, omit: false}, | ||
{Visible: vis, Conditional: 0, omit: true}, | ||
} | ||
|
||
for _, test := range testio { | ||
values, _ := MarshalValues(&test) | ||
|
||
_, found := values["conditional"] | ||
if found && test.omit { | ||
t.Errorf("%d was unexpectedly marshaled", test.Conditional) | ||
} else if !found && !test.omit { | ||
t.Errorf("%d was not marshaled, but should have been", test.Conditional) | ||
} | ||
} | ||
} |
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