Skip to content

Commit 67b7989

Browse files
ulucinarsergenyalcin
authored andcommitted
Use error string constants consistently in structtag/value.go
Signed-off-by: Alper Rifat Ulucinar <ulucinar@users.noreply.github.com>
1 parent 03f5f5c commit 67b7989

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

pkg/types/structtag/value.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import (
1414

1515
const (
1616
errFmtInvalidJSONTagName = "invalid JSON struct tag name: %q (must match %s)"
17+
errFmtInlineWithName = "invalid struct tag: cannot set the name %q with inline"
18+
errFmtInlineWithOmit = "invalid struct tag: cannot set inline with omit option %q"
19+
errFmtInvalidOptions = "invalid struct tag format: %q (cannot combine %q with other options)"
20+
errFmtInvalidCombination = "invalid struct tag format: %q (cannot combine %s with %s)"
1721
)
1822

1923
// reJSONName matches valid Kubernetes CRD field names.
@@ -105,11 +109,11 @@ func build(key Key, opts ...Option) *Value {
105109
}
106110
// make sure name and inline are not both set.
107111
if v.inline && v.name != "" {
108-
panic(fmt.Sprintf("invalid struct tag: cannot set the name %q with inline", v.name))
112+
panic(fmt.Sprintf(errFmtInlineWithName, v.name))
109113
}
110114
// make sure omit and inline are not both set.
111115
if v.inline && v.omit != NotOmitted {
112-
panic(fmt.Sprintf("invalid struct tag: cannot set inline with omit option %q", v.omit))
116+
panic(fmt.Sprintf(errFmtInlineWithOmit, v.omit))
113117
}
114118
return v
115119
}
@@ -138,7 +142,7 @@ func parse(key Key, value string) (*Value, error) { //nolint:gocyclo // easier t
138142
name := strings.TrimSpace(parts[0])
139143
if name == string(OmitAlways) {
140144
if len(parts) > 1 {
141-
return nil, errors.Errorf("invalid struct tag format: %q (cannot combine %q with other options)", value, OmitAlways)
145+
return nil, errors.Errorf(errFmtInvalidOptions, value, OmitAlways)
142146
}
143147
// no name expected for "-" (OmitAlways).
144148
v.omit = OmitAlways
@@ -166,12 +170,12 @@ func parse(key Key, value string) (*Value, error) { //nolint:gocyclo // easier t
166170

167171
// inline cannot be combined with a name.
168172
if v.inline && v.name != "" {
169-
return nil, errors.Errorf("invalid struct tag format: %q (cannot combine name with inline)", value)
173+
return nil, errors.Errorf(errFmtInvalidCombination, value, "name", "inline")
170174
}
171175

172176
// inline cannot be combined with an omit.
173177
if v.inline && v.omit != NotOmitted {
174-
return nil, errors.Errorf("invalid struct tag format: %q (cannot combine inline with omitempty)", value)
178+
return nil, errors.Errorf(errFmtInvalidCombination, value, "inline", "omitempty")
175179
}
176180

177181
return v, nil

0 commit comments

Comments
 (0)