Skip to content

Commit 54e041d

Browse files
author
Julien Pivotto
authored
Implement Stringer on TLSVersion (#405)
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu> Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
1 parent c206bfc commit 54e041d

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

config/http_config.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (tv *TLSVersion) UnmarshalYAML(unmarshal func(interface{}) error) error {
8080
}
8181

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

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

120+
// String implements the fmt.Stringer interface for TLSVersion.
121+
func (tv *TLSVersion) String() string {
122+
if tv == nil || *tv == 0 {
123+
return ""
124+
}
125+
for s, v := range TLSVersions {
126+
if *tv == v {
127+
return s
128+
}
129+
}
130+
return fmt.Sprintf("%d", tv)
131+
}
132+
120133
// BasicAuth contains basic HTTP authentication credentials.
121134
type BasicAuth struct {
122135
Username string `yaml:"username" json:"username"`

config/tls_config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,9 @@ func TestValidTLSConfig(t *testing.T) {
9090
}
9191
}
9292
}
93+
94+
func TestStringer(t *testing.T) {
95+
if s := (TLSVersion)(tls.VersionTLS13); s.String() != "TLS13" {
96+
t.Fatalf("tls.VersionTLS13 string should be TLS13, got %s", s.String())
97+
}
98+
}

0 commit comments

Comments
 (0)