Browse code

pkg/plugins/client.go: don't try to encode os decode if it's nil

When user call the `Call()` method, they don't always want to sent
some args or get the return value, so they use `nil` when call `Call()`
method and this will casue an error. It's better to not trying to
encode or decode if it's nil.

Signed-off-by: Lei Jitang <leijitang@huawei.com>

Lei Jitang authored on 2015/12/05 16:55:50
Showing 2 changed files
... ...
@@ -60,17 +60,21 @@ type Client struct {
60 60
 // It will retry for 30 seconds if a failure occurs when calling.
61 61
 func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
62 62
 	var buf bytes.Buffer
63
-	if err := json.NewEncoder(&buf).Encode(args); err != nil {
64
-		return err
63
+	if args != nil {
64
+		if err := json.NewEncoder(&buf).Encode(args); err != nil {
65
+			return err
66
+		}
65 67
 	}
66 68
 	body, err := c.callWithRetry(serviceMethod, &buf, true)
67 69
 	if err != nil {
68 70
 		return err
69 71
 	}
70 72
 	defer body.Close()
71
-	if err := json.NewDecoder(body).Decode(&ret); err != nil {
72
-		logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
73
-		return err
73
+	if ret != nil {
74
+		if err := json.NewDecoder(body).Decode(&ret); err != nil {
75
+			logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
76
+			return err
77
+		}
74 78
 	}
75 79
 	return nil
76 80
 }
... ...
@@ -63,6 +63,10 @@ func TestEchoInputOutput(t *testing.T) {
63 63
 	if !reflect.DeepEqual(output, m) {
64 64
 		t.Fatalf("Expected %v, was %v\n", m, output)
65 65
 	}
66
+	err = c.Call("Test.Echo", nil, nil)
67
+	if err != nil {
68
+		t.Fatal(err)
69
+	}
66 70
 }
67 71
 
68 72
 func TestBackoff(t *testing.T) {