-
Notifications
You must be signed in to change notification settings - Fork 329
ValidationScheme implements pflag.Value and json.Marshaler/Unmarshaler interfaces #807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -14,6 +14,7 @@ | |||||||
package model | ||||||||
|
||||||||
import ( | ||||||||
"encoding/json" | ||||||||
"errors" | ||||||||
"fmt" | ||||||||
"regexp" | ||||||||
|
@@ -77,10 +78,13 @@ const ( | |||||||
UTF8Validation | ||||||||
) | ||||||||
|
||||||||
var ( | ||||||||
_ yaml.Marshaler = UnsetValidation | ||||||||
_ fmt.Stringer = UnsetValidation | ||||||||
) | ||||||||
var _ interface { | ||||||||
yaml.Marshaler | ||||||||
yaml.Unmarshaler | ||||||||
json.Marshaler | ||||||||
json.Unmarshaler | ||||||||
fmt.Stringer | ||||||||
} = new(ValidationScheme) | ||||||||
|
||||||||
// String returns the string representation of s. | ||||||||
func (s ValidationScheme) String() string { | ||||||||
|
@@ -114,15 +118,41 @@ func (s *ValidationScheme) UnmarshalYAML(unmarshal func(any) error) error { | |||||||
if err := unmarshal(&scheme); err != nil { | ||||||||
return err | ||||||||
} | ||||||||
switch scheme { | ||||||||
return s.Set(scheme) | ||||||||
} | ||||||||
|
||||||||
// MarshalJSON implements the json.Marshaler interface. | ||||||||
func (s ValidationScheme) MarshalJSON() ([]byte, error) { | ||||||||
switch s { | ||||||||
case UnsetValidation: | ||||||||
return json.Marshal("") | ||||||||
case UTF8Validation, LegacyValidation: | ||||||||
return json.Marshal(s.String()) | ||||||||
default: | ||||||||
return nil, fmt.Errorf("unhandled ValidationScheme: %d", s) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
// UnmarshalJSON implements the json.Unmarshaler interface. | ||||||||
func (s *ValidationScheme) UnmarshalJSON(bytes []byte) error { | ||||||||
var repr string | ||||||||
if err := json.Unmarshal(bytes, &repr); err != nil { | ||||||||
return err | ||||||||
} | ||||||||
return s.Set(repr) | ||||||||
} | ||||||||
|
||||||||
// Set implements the pflag.Value interface. | ||||||||
func (s *ValidationScheme) Set(text string) error { | ||||||||
switch text { | ||||||||
case "": | ||||||||
// Don't change the value. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment 'Don't change the value' is misleading for an empty string input. When Set() is called with an empty string, it should set the value to UnsetValidation, not leave it unchanged. Either update the logic to set *s = UnsetValidation or clarify the comment to explain why empty strings preserve the current value.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
case "legacy": | ||||||||
case LegacyValidation.String(): | ||||||||
*s = LegacyValidation | ||||||||
case "utf8": | ||||||||
case UTF8Validation.String(): | ||||||||
*s = UTF8Validation | ||||||||
default: | ||||||||
return fmt.Errorf("unrecognized ValidationScheme: %q", scheme) | ||||||||
return fmt.Errorf("unrecognized ValidationScheme: %q", text) | ||||||||
} | ||||||||
return nil | ||||||||
} | ||||||||
|
@@ -174,6 +204,11 @@ func (s ValidationScheme) IsValidLabelName(labelName string) bool { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
// Type implements the pflag.Value interface. | ||||||||
func (s ValidationScheme) Type() string { | ||||||||
return "validationScheme" | ||||||||
} | ||||||||
|
||||||||
type EscapingScheme int | ||||||||
|
||||||||
const ( | ||||||||
|
Uh oh!
There was an error while loading. Please reload this page.