Merge pull request #60 from YorikSar/refactor_encode

Return []byte from encode, use it instead of bytes.Buffer
This commit is contained in:
Geoff Hickey 2018-02-27 17:37:25 -05:00 committed by GitHub
commit a7be1ba7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 793 additions and 793 deletions

View File

@ -129,7 +129,7 @@ func decodeTypedParams(dec *xdr.Decoder) ([]TypedParam, error) {
{{range .Procs}}
// {{.Name}} is the go wrapper for {{.LVName}}.
func (l *Libvirt) {{.Name}}({{range $ix, $arg := .Args}}{{if $ix}}, {{end}}{{.Name}} {{.Type}}{{end}}) ({{range .Ret}}r{{.Name}} {{.Type}}, {{end}}err error) {
var buf bytes.Buffer
var buf []byte
{{if .ArgsStruct}}
args := {{.ArgsStruct}} {
{{range .Args}} {{.Name}}: {{.Name}},
@ -141,7 +141,7 @@ func (l *Libvirt) {{.Name}}({{range $ix, $arg := .Args}}{{if $ix}}, {{end}}{{.Na
}
{{end}}
{{if .RetStruct}} var r response{{end}}
{{if .RetStruct}}r{{else}}_{{end}}, err = l.request({{.Num}}, constants.Program, &buf)
{{if .RetStruct}}r{{else}}_{{end}}, err = l.request({{.Num}}, constants.Program, buf)
if err != nil {
return
}

File diff suppressed because it is too large Load Diff

View File

@ -152,7 +152,7 @@ func (l *Libvirt) Events(dom string) (<-chan DomainEvent, error) {
return nil, err
}
res, err := l.request(constants.QEMUConnectDomainMonitorEventRegister, constants.ProgramQEMU, &buf)
res, err := l.request(constants.QEMUConnectDomainMonitorEventRegister, constants.ProgramQEMU, buf)
if err != nil {
if err == ErrUnsupported {
return nil, ErrEventsNotSupported
@ -243,7 +243,7 @@ func (l *Libvirt) Run(dom string, cmd []byte) ([]byte, error) {
return nil, err
}
res, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, &buf)
res, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, buf)
if err != nil {
return nil, err
}

16
rpc.go
View File

@ -131,12 +131,12 @@ func (l *Libvirt) connect() error {
// libvirt requires that we call auth-list prior to connecting,
// event when no authentication is used.
_, err = l.request(constants.ProcAuthList, constants.Program, &buf)
_, err = l.request(constants.ProcAuthList, constants.Program, buf)
if err != nil {
return err
}
_, err = l.request(constants.ProcConnectOpen, constants.Program, &buf)
_, err = l.request(constants.ProcConnectOpen, constants.Program, buf)
if err != nil {
return err
}
@ -262,7 +262,7 @@ func (l *Libvirt) removeStream(id uint32) error {
return err
}
_, err = l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, &buf)
_, err = l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, buf)
if err != nil {
return err
}
@ -292,7 +292,7 @@ func (l *Libvirt) deregister(id uint32) {
// request performs a libvirt RPC request.
// returns response returned by server.
// if response is not OK, decodes error from it and returns it.
func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (response, error) {
func (l *Libvirt) request(proc uint32, program uint32, payload []byte) (response, error) {
serial := l.serial()
c := make(chan response)
@ -300,7 +300,7 @@ func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (r
size := constants.PacketLengthSize + constants.HeaderSize
if payload != nil {
size += payload.Len()
size += len(payload)
}
p := packet{
@ -325,7 +325,7 @@ func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (r
// write payload
if payload != nil {
err = binary.Write(l.w, binary.BigEndian, payload.Bytes())
err = binary.Write(l.w, binary.BigEndian, payload)
if err != nil {
return response{}, err
}
@ -344,11 +344,11 @@ func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (r
}
// encode XDR encodes the provided data.
func encode(data interface{}) (bytes.Buffer, error) {
func encode(data interface{}) ([]byte, error) {
var buf bytes.Buffer
_, err := xdr.Marshal(&buf, data)
return buf, err
return buf.Bytes(), err
}
// decodeError extracts an error message from the provider buffer.

View File

@ -219,7 +219,7 @@ func TestEncode(t *testing.T) {
t.Error(err)
}
dec := xdr.NewDecoder(bytes.NewReader(buf.Bytes()))
dec := xdr.NewDecoder(bytes.NewReader(buf))
res, _, err := dec.DecodeString()
if err != nil {
t.Error(err)