Browse Source

changed marshalling of inet addresses to restrict them to either 4 or 16 byte representations to remove extraneous bytes

dankennedy 11 years ago
parent
commit
edc5a1e8e8
2 changed files with 29 additions and 11 deletions
  1. 23 5
      marshal.go
  2. 6 6
      marshal_test.go

+ 23 - 5
marshal.go

@@ -1044,15 +1044,24 @@ func unmarshalTimeUUID(info *TypeInfo, data []byte, value interface{}) error {
 }
 
 func marshalInet(info *TypeInfo, value interface{}) ([]byte, error) {
+	// we return either the 4 or 16 byte representation of an
+	// ip address here otherwise the db value will be prefixed
+	// with the remaining byte values e.g. ::ffff:127.0.0.1 and not 127.0.0.1
 	switch val := value.(type) {
 	case net.IP:
-		return val, nil
-	case []byte:
-		return val, nil
+		t := val.To4()
+		if t == nil {
+			return val.To16(), nil
+		}
+		return t, nil
 	case string:
 		b := net.ParseIP(val)
 		if b != nil {
-			return b[:], nil
+			t := b.To4()
+			if t == nil {
+				return b.To16(), nil
+			}
+			return t, nil
 		}
 		return nil, marshalErrorf("cannot marshal. invalid ip string %s", val)
 	}
@@ -1064,7 +1073,12 @@ func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {
 	case Unmarshaler:
 		return v.UnmarshalCQL(info, data)
 	case *net.IP:
-		*v = net.IP(data)
+		ip := net.IP(data)
+		if v4 := ip.To4(); v4 != nil {
+			*v = v4
+			return nil
+		}
+		*v = ip
 		return nil
 	case *string:
 		if len(data) == 0 {
@@ -1072,6 +1086,10 @@ func unmarshalInet(info *TypeInfo, data []byte, value interface{}) error {
 			return nil
 		}
 		ip := net.IP(data)
+		if v4 := ip.To4(); v4 != nil {
+			*v = v4.String()
+			return nil
+		}
 		*v = ip.String()
 		return nil
 	}

+ 6 - 6
marshal_test.go

@@ -275,22 +275,22 @@ var marshalTests = []struct {
 	},
 	{
 		&TypeInfo{Type: TypeInet},
-		[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\x7F\x00\x00\x01"),
-		net.ParseIP("127.0.0.1"),
+		[]byte("\x7F\x00\x00\x01"),
+		net.ParseIP("127.0.0.1").To4(),
 	},
 	{
 		&TypeInfo{Type: TypeInet},
-		[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"),
-		net.ParseIP("255.255.255.255"),
+		[]byte("\xFF\xFF\xFF\xFF"),
+		net.ParseIP("255.255.255.255").To4(),
 	},
 	{
 		&TypeInfo{Type: TypeInet},
-		[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\x7F\x00\x00\x01"),
+		[]byte("\x7F\x00\x00\x01"),
 		"127.0.0.1",
 	},
 	{
 		&TypeInfo{Type: TypeInet},
-		[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"),
+		[]byte("\xFF\xFF\xFF\xFF"),
 		"255.255.255.255",
 	},
 	{