From: Jeff Halter Date: Fri, 24 Jun 2022 20:15:33 +0000 (-0700) Subject: Implement io.Reader interface for account X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/926c7f5505456a0900e9c49f6c08efebe9a53505?ds=sidebyside;hp=3178ae580a3fe97d6a1167b4346d209f04e9b7e3 Implement io.Reader interface for account --- 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...))