Skip to content
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

Implement Stringer on TLSVersion #405

Merged
merged 1 commit into from Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 15 additions & 2 deletions config/http_config.go
Expand Up @@ -80,7 +80,7 @@ func (tv *TLSVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
}

func (tv *TLSVersion) MarshalYAML() (interface{}, error) {
if tv != nil || *tv == 0 {
if tv == nil || *tv == 0 {
return []byte("null"), nil
}
for s, v := range TLSVersions {
Expand All @@ -106,7 +106,7 @@ func (tv *TLSVersion) UnmarshalJSON(data []byte) error {

// MarshalJSON implements the json.Marshaler interface for TLSVersion.
func (tv *TLSVersion) MarshalJSON() ([]byte, error) {
if tv != nil || *tv == 0 {
if tv == nil || *tv == 0 {
return []byte("null"), nil
}
for s, v := range TLSVersions {
Expand All @@ -117,6 +117,19 @@ func (tv *TLSVersion) MarshalJSON() ([]byte, error) {
return nil, fmt.Errorf("unknown TLS version: %d", tv)
}

// String implements the fmt.Stringer interface for TLSVersion.
func (tv *TLSVersion) String() string {
if tv == nil || *tv == 0 {
return ""
}
for s, v := range TLSVersions {
if *tv == v {
return s
}
}
return fmt.Sprintf("%d", tv)
}

// BasicAuth contains basic HTTP authentication credentials.
type BasicAuth struct {
Username string `yaml:"username" json:"username"`
Expand Down
6 changes: 6 additions & 0 deletions config/tls_config_test.go
Expand Up @@ -90,3 +90,9 @@ func TestValidTLSConfig(t *testing.T) {
}
}
}

func TestStringer(t *testing.T) {
if s := (TLSVersion)(tls.VersionTLS13); s.String() != "TLS13" {
t.Fatalf("tls.VersionTLS13 string should be TLS13, got %s", s.String())
}
}