diff --git a/config/http_config.go b/config/http_config.go index c771b2ef..e0959e47 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -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 { @@ -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 { @@ -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"` diff --git a/config/tls_config_test.go b/config/tls_config_test.go index 662c7103..8799d7eb 100644 --- a/config/tls_config_test.go +++ b/config/tls_config_test.go @@ -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()) + } +}