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}} {{range .Procs}}
// {{.Name}} is the go wrapper for {{.LVName}}. // {{.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) { 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}} {{if .ArgsStruct}}
args := {{.ArgsStruct}} { args := {{.ArgsStruct}} {
{{range .Args}} {{.Name}}: {{.Name}}, {{range .Args}} {{.Name}}: {{.Name}},
@ -141,7 +141,7 @@ func (l *Libvirt) {{.Name}}({{range $ix, $arg := .Args}}{{if $ix}}, {{end}}{{.Na
} }
{{end}} {{end}}
{{if .RetStruct}} var r response{{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 { if err != nil {
return 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 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 != nil {
if err == ErrUnsupported { if err == ErrUnsupported {
return nil, ErrEventsNotSupported return nil, ErrEventsNotSupported
@ -243,7 +243,7 @@ func (l *Libvirt) Run(dom string, cmd []byte) ([]byte, error) {
return nil, err return nil, err
} }
res, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, &buf) res, err := l.request(constants.QEMUDomainMonitor, constants.ProgramQEMU, buf)
if err != nil { if err != nil {
return nil, err 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, // libvirt requires that we call auth-list prior to connecting,
// event when no authentication is used. // 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 { if err != nil {
return err return err
} }
_, err = l.request(constants.ProcConnectOpen, constants.Program, &buf) _, err = l.request(constants.ProcConnectOpen, constants.Program, buf)
if err != nil { if err != nil {
return err return err
} }
@ -262,7 +262,7 @@ func (l *Libvirt) removeStream(id uint32) error {
return err return err
} }
_, err = l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, &buf) _, err = l.request(constants.QEMUConnectDomainMonitorEventDeregister, constants.ProgramQEMU, buf)
if err != nil { if err != nil {
return err return err
} }
@ -292,7 +292,7 @@ func (l *Libvirt) deregister(id uint32) {
// request performs a libvirt RPC request. // request performs a libvirt RPC request.
// returns response returned by server. // returns response returned by server.
// if response is not OK, decodes error from it and returns it. // 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() serial := l.serial()
c := make(chan response) 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 size := constants.PacketLengthSize + constants.HeaderSize
if payload != nil { if payload != nil {
size += payload.Len() size += len(payload)
} }
p := packet{ p := packet{
@ -325,7 +325,7 @@ func (l *Libvirt) request(proc uint32, program uint32, payload *bytes.Buffer) (r
// write payload // write payload
if payload != nil { if payload != nil {
err = binary.Write(l.w, binary.BigEndian, payload.Bytes()) err = binary.Write(l.w, binary.BigEndian, payload)
if err != nil { if err != nil {
return response{}, err 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. // encode XDR encodes the provided data.
func encode(data interface{}) (bytes.Buffer, error) { func encode(data interface{}) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
_, err := xdr.Marshal(&buf, data) _, err := xdr.Marshal(&buf, data)
return buf, err return buf.Bytes(), err
} }
// decodeError extracts an error message from the provider buffer. // decodeError extracts an error message from the provider buffer.

View File

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