-
Notifications
You must be signed in to change notification settings - Fork 361
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
Changes from 1 commit
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. | ||||||||
|
||||||||
| // Don't change the value. | |
| // Set to UnsetValidation for empty string input. | |
| *s = UnsetValidation |
Uh oh!
There was an error while loading. Please reload this page.