← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/gomaasapi/nil-params into lp:gomaasapi

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/gomaasapi/nil-params into lp:gomaasapi.

Commit message:
Allow nil as a valid way of passing no parameters to a request.

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~jtv/gomaasapi/nil-params/+merge/150748

It gets tedious to write "url.Values{}" every time you need to pass a value for "parameters" but you have no parameters to pass.  I got a surprising crash while running tests for some other work I'm doing, so I made the change.  Making Your Day Nicer One Line At A Time™.


Jeroen
-- 
https://code.launchpad.net/~jtv/gomaasapi/nil-params/+merge/150748
Your team MAAS Maintainers is requested to review the proposed merge of lp:~jtv/gomaasapi/nil-params into lp:gomaasapi.
=== modified file 'client.go'
--- client.go	2013-02-18 04:18:02 +0000
+++ client.go	2013-02-27 09:35:24 +0000
@@ -5,7 +5,6 @@
 
 import (
 	"bytes"
-	"errors"
 	"fmt"
 	"io"
 	"io/ioutil"
@@ -64,10 +63,13 @@
 // invocation (if you pass its name in "operation") or plain resource
 // retrieval (if you leave "operation" blank).
 func (client Client) Get(uri *url.URL, operation string, parameters url.Values) ([]byte, error) {
+	if parameters == nil {
+		parameters = make(url.Values)
+	}
 	opParameter := parameters.Get("op")
 	if opParameter != "" {
-		errString := fmt.Sprintf("The parameters contain a value for '%s' which is reserved parameter.")
-		return nil, errors.New(errString)
+		msg := fmt.Errorf("reserved parameter 'op' passed (with value '%s')", opParameter)
+		return nil, msg
 	}
 	if operation != "" {
 		parameters.Set("op", operation)

=== modified file 'client_test.go'
--- client_test.go	2013-02-18 03:37:28 +0000
+++ client_test.go	2013-02-27 09:35:24 +0000
@@ -85,7 +85,7 @@
 	defer server.Close()
 	client, _ := NewAnonymousClient(server.URL)
 
-	result, err := client.Get(URI, "list", url.Values{})
+	result, err := client.Get(URI, "list", nil)
 
 	c.Check(err, IsNil)
 	c.Check(string(result), Equals, expectedResult)
@@ -144,7 +144,7 @@
 	fileContent := []byte("content")
 	files := map[string][]byte{"testfile": fileContent}
 
-	result, err := client.Post(URI, "add", url.Values{}, files)
+	result, err := client.Post(URI, "add", nil, files)
 
 	c.Check(err, IsNil)
 	c.Check(string(result), Equals, expectedResult)