aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Halter <868228+jhalter@users.noreply.github.com>2022-06-24 13:15:33 -0700
committerJeff Halter <868228+jhalter@users.noreply.github.com>2022-06-24 13:15:33 -0700
commit926c7f5505456a0900e9c49f6c08efebe9a53505 (patch)
tree9a568d2a838d1d05043a02b553ed5ddd029aff50
parent3178ae580a3fe97d6a1167b4346d209f04e9b7e3 (diff)
Implement io.Reader interface for account
-rw-r--r--hotline/account.go15
-rw-r--r--hotline/transaction_handlers.go9
2 files changed, 13 insertions, 11 deletions
diff --git a/hotline/account.go b/hotline/account.go
index 3da818f..2c400ac 100644
--- a/hotline/account.go
+++ b/hotline/account.go
@@ -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
}
diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go
index 26eee12..650aae0 100644
--- a/hotline/transaction_handlers.go
+++ b/hotline/transaction_handlers.go
@@ -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...))