]> git.r.bdr.sh - rbdr/mobius/commitdiff
Implement io.Reader interface for account
authorJeff Halter <redacted>
Fri, 24 Jun 2022 20:15:33 +0000 (13:15 -0700)
committerJeff Halter <redacted>
Fri, 24 Jun 2022 20:15:33 +0000 (13:15 -0700)
hotline/account.go
hotline/transaction_handlers.go

index 3da818f1ddf5a59e1312ed5ea1c7697e53c6dd27..2c400aceafb56e28c6746f4f582d8347f69ddeab 100644 (file)
@@ -2,7 +2,6 @@ package hotline
 
 import (
        "encoding/binary"
-       "github.com/jhalter/mobius/concat"
        "golang.org/x/crypto/bcrypt"
 )
 
@@ -15,8 +14,8 @@ type Account struct {
        Access   *[]byte `yaml:"Access"` // 8 byte bitmap
 }
 
-// MarshalBinary marshals an Account to byte slice
-func (a *Account) MarshalBinary() (out []byte) {
+// Read implements io.Reader interface for Account
+func (a *Account) Read(p []byte) (n int, err error) {
        fields := []Field{
                NewField(fieldUserName, []byte(a.Name)),
                NewField(fieldUserLogin, negateString([]byte(a.Login))),
@@ -30,13 +29,11 @@ func (a *Account) MarshalBinary() (out []byte) {
        fieldCount := make([]byte, 2)
        binary.BigEndian.PutUint16(fieldCount, uint16(len(fields)))
 
-       var fieldPayload []byte
+       p = append(p, fieldCount...)
+
        for _, field := range fields {
-               fieldPayload = append(fieldPayload, field.Payload()...)
+               p = append(p, field.Payload()...)
        }
 
-       return concat.Slices(
-               fieldCount,
-               fieldPayload,
-       )
+       return len(p), nil
 }
index 26eee12ef38a47931be98d034389f8269e48a50f..650aae069f3365e3b36f8d5583531c4b4b13b1a5 100644 (file)
@@ -698,8 +698,13 @@ func HandleListUsers(cc *ClientConn, t *Transaction) (res []Transaction, err err
 
        var userFields []Field
        for _, acc := range cc.Server.Accounts {
-               userField := acc.MarshalBinary()
-               userFields = append(userFields, NewField(fieldData, userField))
+               b := make([]byte, 0, 100)
+               n, err := acc.Read(b)
+               if err != nil {
+                       return res, err
+               }
+
+               userFields = append(userFields, NewField(fieldData, b[:n]))
        }
 
        res = append(res, cc.NewReply(t, userFields...))