2
0

Add pgproto3.Backend

This commit is contained in:
Jack Christensen
2017-05-01 18:11:55 -05:00
parent eff55451cf
commit d25abf5674
8 changed files with 545 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
package pgproto3
import (
"bytes"
"encoding/json"
)
type PasswordMessage struct {
Password string
}
func (*PasswordMessage) Frontend() {}
func (dst *PasswordMessage) Decode(src []byte) error {
buf := bytes.NewBuffer(src)
b, err := buf.ReadBytes(0)
if err != nil {
return err
}
dst.Password = string(b[:len(b)-1])
return nil
}
func (src *PasswordMessage) MarshalBinary() ([]byte, error) {
var bigEndian BigEndianBuf
buf := &bytes.Buffer{}
buf.WriteByte('p')
buf.Write(bigEndian.Uint32(uint32(4 + len(src.Password) + 1)))
buf.WriteString(src.Password)
buf.WriteByte(0)
return buf.Bytes(), nil
}
func (src *PasswordMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string
Password string
}{
Type: "PasswordMessage",
Password: src.Password,
})
}