Skip to content

Commit

Permalink
Implement Stringer on TLSVersion (#405)
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>

Signed-off-by: Julien Pivotto <roidelapluie@o11y.eu>
  • Loading branch information
roidelapluie committed Nov 2, 2022
1 parent c206bfc commit 54e041d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
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())
}
}

0 comments on commit 54e041d

Please sign in to comment.