changeset 340:4c211ad5349e

Embed Reader and Writer in BinReader and BinWriter to make API more distinct.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 05 Aug 2018 15:48:36 +0200
parents 33b59c848771
children 889517f254f5
files auth/connection.go auth/session.go misc/encode.go
diffstat 3 files changed, 40 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/auth/connection.go	Sun Aug 05 15:35:29 2018 +0200
+++ b/auth/connection.go	Sun Aug 05 15:48:36 2018 +0200
@@ -36,9 +36,9 @@
 	if err != nil {
 		return err
 	}
-	wr := misc.BinWriter{W: w}
-	wr.Write(uint32(len(access)))
-	wr.Write(access)
+	wr := misc.BinWriter{w, nil}
+	wr.WriteBin(uint32(len(access)))
+	wr.WriteBin(access)
 	return wr.Err
 }
 
@@ -48,11 +48,11 @@
 		return err
 	}
 
-	rd := misc.BinReader{R: r}
+	rd := misc.BinReader{r, nil}
 	var l uint32
-	rd.Read(&l)
+	rd.ReadBin(&l)
 	access := make([]byte, l)
-	rd.Read(access)
+	rd.ReadBin(access)
 
 	if rd.Err != nil {
 		return rd.Err
--- a/auth/session.go	Sun Aug 05 15:35:29 2018 +0200
+++ b/auth/session.go	Sun Aug 05 15:48:36 2018 +0200
@@ -43,11 +43,11 @@
 }
 
 func (s *Session) serialize(w io.Writer) error {
-	wr := misc.BinWriter{W: w}
-	wr.Write(s.ExpiresAt)
+	wr := misc.BinWriter{w, nil}
+	wr.WriteBin(s.ExpiresAt)
 	wr.WriteString(s.User)
 	wr.WriteString(s.Password)
-	wr.Write(uint32(len(s.Roles)))
+	wr.WriteBin(uint32(len(s.Roles)))
 	for _, role := range s.Roles {
 		wr.WriteString(role)
 	}
@@ -57,11 +57,11 @@
 func (s *Session) deserialize(r io.Reader) error {
 	var x Session
 	var n uint32
-	rd := misc.BinReader{R: r}
-	rd.Read(&x.ExpiresAt)
+	rd := misc.BinReader{r, nil}
+	rd.ReadBin(&x.ExpiresAt)
 	rd.ReadString(&x.User)
 	rd.ReadString(&x.Password)
-	rd.Read(&n)
+	rd.ReadBin(&n)
 	x.Roles = make(Roles, n)
 	for i := uint32(0); n > 0 && i < n; i++ {
 		rd.ReadString(&x.Roles[i])
--- a/misc/encode.go	Sun Aug 05 15:35:29 2018 +0200
+++ b/misc/encode.go	Sun Aug 05 15:48:36 2018 +0200
@@ -6,13 +6,22 @@
 )
 
 type BinReader struct {
-	R   io.Reader
+	io.Reader
 	Err error
 }
 
-func (r *BinReader) Read(x interface{}) {
+func (r *BinReader) Read(buf []byte) (int, error) {
+	if r.Err != nil {
+		return 0, r.Err
+	}
+	var n int
+	n, r.Err = r.Read(buf)
+	return n, r.Err
+}
+
+func (r *BinReader) ReadBin(x interface{}) {
 	if r.Err == nil {
-		r.Err = binary.Read(r.R, binary.BigEndian, x)
+		r.Err = binary.Read(r.Reader, binary.BigEndian, x)
 	}
 }
 
@@ -21,32 +30,41 @@
 		return
 	}
 	var l uint32
-	if r.Err = binary.Read(r.R, binary.BigEndian, &l); r.Err != nil {
+	if r.Err = binary.Read(r.Reader, binary.BigEndian, &l); r.Err != nil {
 		return
 	}
 	b := make([]byte, l)
-	if r.Err = binary.Read(r.R, binary.BigEndian, b); r.Err != nil {
+	if r.Err = binary.Read(r.Reader, binary.BigEndian, b); r.Err != nil {
 		return
 	}
 	*s = string(b)
 }
 
 type BinWriter struct {
-	W   io.Writer
+	io.Writer
 	Err error
 }
 
-func (w *BinWriter) Write(x interface{}) {
+func (w *BinWriter) Write(buf []byte) (int, error) {
+	if w.Err != nil {
+		return 0, w.Err
+	}
+	var n int
+	n, w.Err = w.Writer.Write(buf)
+	return n, w.Err
+}
+
+func (w *BinWriter) WriteBin(x interface{}) {
 	if w.Err == nil {
-		w.Err = binary.Write(w.W, binary.BigEndian, x)
+		w.Err = binary.Write(w.Writer, binary.BigEndian, x)
 	}
 }
 
 func (w *BinWriter) WriteString(s string) {
 	if w.Err == nil {
-		w.Err = binary.Write(w.W, binary.BigEndian, uint32(len(s)))
+		w.Err = binary.Write(w.Writer, binary.BigEndian, uint32(len(s)))
 	}
 	if w.Err == nil {
-		w.Err = binary.Write(w.W, binary.BigEndian, []byte(s))
+		w.Err = binary.Write(w.Writer, binary.BigEndian, []byte(s))
 	}
 }