Explorar o código

Merge pull request #13879 from eolamey/13878-nil-ip-opt-as-empty-string

Display empty string instead of <nil> when IP opt is nil.
Doug Davis %!s(int64=10) %!d(string=hai) anos
pai
achega
b27f960504
Modificáronse 2 ficheiros con 16 adicións e 0 borrados
  1. 3 0
      opts/ip.go
  2. 13 0
      opts/opts_test.go

+ 3 - 0
opts/ip.go

@@ -27,5 +27,8 @@ func (o *IpOpt) Set(val string) error {
 }
 
 func (o *IpOpt) String() string {
+	if *o.IP == nil {
+		return ""
+	}
 	return o.IP.String()
 }

+ 13 - 0
opts/opts_test.go

@@ -2,6 +2,7 @@ package opts
 
 import (
 	"fmt"
+	"net"
 	"strings"
 	"testing"
 )
@@ -179,6 +180,18 @@ func TestValidateExtraHosts(t *testing.T) {
 	}
 }
 
+func TestIpOptString(t *testing.T) {
+	addresses := []string{"", "0.0.0.0"}
+	var ip net.IP
+
+	for _, address := range addresses {
+		stringAddress := NewIpOpt(&ip, address).String()
+		if stringAddress != address {
+			t.Fatalf("IpOpt string should be `%s`, not `%s`", address, stringAddress)
+		}
+	}
+}
+
 func logOptsValidator(val string) (string, error) {
 	allowedKeys := map[string]string{"max-size": "1", "max-file": "2"}
 	vals := strings.Split(val, "=")