Explorar o código

Slices can already be nil

So there's no need to take pointers to them everywhere to get that behaviour
(which is what we have to do with kafka strings).
Evan Huus %!s(int64=12) %!d(string=hai) anos
pai
achega
0c69a97459
Modificáronse 7 ficheiros con 21 adicións e 24 borrados
  1. 1 1
      broker.go
  2. 7 9
      message.go
  3. 1 1
      packet_decoder.go
  4. 3 3
      packet_encoder.go
  5. 3 3
      prep_encoder.go
  6. 3 4
      real_decoder.go
  7. 3 3
      real_encoder.go

+ 1 - 1
broker.go

@@ -61,7 +61,7 @@ func (b *Broker) Send(clientID *string, req requestEncoder) (decoder, error) {
 	sendRequest := requestToSend{responsePromise{b.correlation_id, make(chan []byte), make(chan error)}, response != nil}
 
 	b.requests <- sendRequest
-	sendRequest.response.packets <- *packet // we cheat to avoid poofing up more channels than necessary
+	sendRequest.response.packets <- packet // we cheat to avoid poofing up more channels than necessary
 	b.correlation_id++
 
 	select {

+ 7 - 9
message.go

@@ -20,8 +20,8 @@ const message_format int8 = 0
 
 type message struct {
 	codec compressionCodec
-	key   *[]byte
-	value *[]byte
+	key   []byte
+	value []byte
 }
 
 func (m *message) encode(pe packetEncoder) {
@@ -35,7 +35,7 @@ func (m *message) encode(pe packetEncoder) {
 
 	pe.putBytes(m.key)
 
-	var body *[]byte
+	var body []byte
 	switch m.codec {
 	case COMPRESSION_NONE:
 		body = m.value
@@ -43,10 +43,9 @@ func (m *message) encode(pe packetEncoder) {
 		if m.value != nil {
 			var buf bytes.Buffer
 			writer := gzip.NewWriter(&buf)
-			writer.Write(*m.value)
+			writer.Write(m.value)
 			writer.Close()
-			tmp := buf.Bytes()
-			body = &tmp
+			body = buf.Bytes()
 		}
 	case COMPRESSION_SNAPPY:
 		// TODO
@@ -93,15 +92,14 @@ func (m *message) decode(pd packetDecoder) (err error) {
 		if m.value == nil {
 			return DecodingError("Nil contents cannot be compressed.")
 		}
-		reader, err := gzip.NewReader(bytes.NewReader(*m.value))
+		reader, err := gzip.NewReader(bytes.NewReader(m.value))
 		if err != nil {
 			return err
 		}
-		tmp, err := ioutil.ReadAll(reader)
+		m.value, err = ioutil.ReadAll(reader)
 		if err != nil {
 			return err
 		}
-		m.value = &tmp
 	case COMPRESSION_SNAPPY:
 		// TODO
 	default:

+ 1 - 1
packet_decoder.go

@@ -16,7 +16,7 @@ type packetDecoder interface {
 	// misc
 	getError() (KError, error)
 	getString() (*string, error)
-	getBytes() (*[]byte, error)
+	getBytes() ([]byte, error)
 	getSubset(length int) (packetDecoder, error)
 
 	// stackable

+ 3 - 3
packet_encoder.go

@@ -14,7 +14,7 @@ type packetEncoder interface {
 	// misc
 	putError(in KError)
 	putString(in *string)
-	putBytes(in *[]byte)
+	putBytes(in []byte)
 	putRaw(in []byte)
 
 	// stackable
@@ -30,7 +30,7 @@ type pushEncoder interface {
 	run(curOffset int, buf []byte)
 }
 
-func buildBytes(in encoder) (*[]byte, error) {
+func buildBytes(in encoder) ([]byte, error) {
 	if in == nil {
 		return nil, nil
 	}
@@ -46,5 +46,5 @@ func buildBytes(in encoder) (*[]byte, error) {
 	realEnc.raw = make([]byte, prepEnc.length)
 	in.encode(&realEnc)
 
-	return &(realEnc.raw), nil
+	return realEnc.raw, nil
 }

+ 3 - 3
prep_encoder.go

@@ -54,15 +54,15 @@ func (pe *prepEncoder) putString(in *string) {
 	}
 }
 
-func (pe *prepEncoder) putBytes(in *[]byte) {
+func (pe *prepEncoder) putBytes(in []byte) {
 	pe.length += 4
 	if in == nil {
 		return
 	}
-	if len(*in) > math.MaxInt32 {
+	if len(in) > math.MaxInt32 {
 		pe.err = EncodingError("Bytes too long.")
 	} else {
-		pe.length += len(*in)
+		pe.length += len(in)
 	}
 }
 

+ 3 - 4
real_decoder.go

@@ -120,7 +120,7 @@ func (rd *realDecoder) getString() (*string, error) {
 	}
 }
 
-func (rd *realDecoder) getBytes() (*[]byte, error) {
+func (rd *realDecoder) getBytes() ([]byte, error) {
 	tmp, err := rd.getInt32()
 
 	if err != nil {
@@ -135,14 +135,13 @@ func (rd *realDecoder) getBytes() (*[]byte, error) {
 	case n == -1:
 		return nil, nil
 	case n == 0:
-		tmp := make([]byte, 0)
-		return &tmp, nil
+		return make([]byte, 0), nil
 	case n > rd.remaining():
 		return nil, DecodingError("Bytes too long in getBytes.")
 	default:
 		tmp := rd.raw[rd.off : rd.off+n]
 		rd.off += n
-		return &tmp, nil
+		return tmp, nil
 	}
 }
 

+ 3 - 3
real_encoder.go

@@ -59,13 +59,13 @@ func (re *realEncoder) putString(in *string) {
 	re.off += len(*in)
 }
 
-func (re *realEncoder) putBytes(in *[]byte) {
+func (re *realEncoder) putBytes(in []byte) {
 	if in == nil {
 		re.putInt32(-1)
 		return
 	}
-	re.putInt32(int32(len(*in)))
-	re.putRaw(*in)
+	re.putInt32(int32(len(in)))
+	re.putRaw(in)
 }
 
 func (re *realEncoder) putRaw(in []byte) {