From a1c265b0c5bea2c1c4a62acac0045fe93cd691cd Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 22 Dec 2020 10:08:25 -0800 Subject: device: simplify UAPI helper methods bufio is not required. strings.Builder is cheaper than bytes.Buffer for constructing strings. io.Writer is more flexible than io.StringWriter, and just as cheap (when used with io.WriteString). Run gofmt. Signed-off-by: Josh Bleecher Snyder --- device/uapi.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/device/uapi.go b/device/uapi.go index 2843a4c..370ad4e 100644 --- a/device/uapi.go +++ b/device/uapi.go @@ -7,7 +7,6 @@ package device import ( "bufio" - "bytes" "errors" "fmt" "io" @@ -33,7 +32,7 @@ func (s IPCError) ErrorCode() int64 { return s.int64 } -func (device *Device) IpcGetOperation(w io.StringWriter) error { +func (device *Device) IpcGetOperation(w io.Writer) error { lines := make([]string, 0, 100) send := func(line string) { lines = append(lines, line) @@ -99,7 +98,7 @@ func (device *Device) IpcGetOperation(w io.StringWriter) error { // send lines (does not require resource locks) for _, line := range lines { - _, err := w.WriteString(line + "\n") + _, err := io.WriteString(w, line+"\n") if err != nil { return &IPCError{ipc.IpcErrorIO} } @@ -397,22 +396,16 @@ func (device *Device) IpcSetOperation(r io.Reader) error { return nil } - - func (device *Device) IpcGet() (string, error) { - buf := new(bytes.Buffer) - writer := bufio.NewWriter(buf) - if err := device.IpcGetOperation(writer); err != nil { - return "", err - } - if err := writer.Flush(); err != nil { + buf := new(strings.Builder) + if err := device.IpcGetOperation(buf); err != nil { return "", err } return buf.String(), nil } func (device *Device) IpcSet(uapiConf string) error { - return device.IpcSetOperation(bufio.NewReader(strings.NewReader(uapiConf))) + return device.IpcSetOperation(strings.NewReader(uapiConf)) } func (device *Device) IpcHandle(socket net.Conn) { -- cgit v1.2.3