Change the way parameter lists are handled internally
authorMikael Berthe <mikael@lilotux.net>
Fri, 07 Sep 2018 19:17:12 +0200
changeset 243 7386c6a454a8
parent 242 2ec8b4cdf94e
child 244 4508376c8fcb
Change the way parameter lists are handled internally Instead of trying to guess if a query key is a list (to strip the index number, since Rails expects list IDs without index number), we prefix the key name with the index when dealing with lists. E.g.: [0]ids: "one" [1]ids: "two" will be sent as ids[]=one&ids[]=two It makes it more reliable and let us differenciate between arrays and objects (objects are untouched and sent as-is).
account.go
api.go
lists.go
notifications.go
report.go
status.go
--- a/account.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/account.go	Fri Sep 07 19:17:12 2018 +0200
@@ -362,7 +362,7 @@
 		if id < 1 {
 			return nil, ErrInvalidID
 		}
-		qID := fmt.Sprintf("id[%d]", i)
+		qID := fmt.Sprintf("[%d]id", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 
--- a/api.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/api.go	Fri Sep 07 19:17:12 2018 +0200
@@ -91,6 +91,7 @@
 		// Add parameters to the URL
 		request.BaseURL += "?"
 		urlp := url.Values{}
+		arrayRe := regexp.MustCompile(`^\[\d+\](.*)$`)
 		for key, value := range request.QueryParams {
 			// It seems Mastodon doesn't like parameters with index
 			// numbers, but it needs the brackets.
@@ -100,10 +101,9 @@
 			if klen == 0 {
 				continue
 			}
-			i := strings.Index(key, "[")
-			if i > 0 && key[klen-1] == ']' && strings.Index(key[i+1:], "[") < 0 {
+			if m := arrayRe.FindStringSubmatch(key); len(m) > 0 {
 				// This is an array, let's remove the index number
-				key = key[:i] + "[]"
+				key = m[1] + "[]"
 			}
 			urlp.Add(key, value)
 		}
--- a/lists.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/lists.go	Fri Sep 07 19:17:12 2018 +0200
@@ -100,7 +100,7 @@
 		if id < 1 {
 			return ErrInvalidID
 		}
-		qID := fmt.Sprintf("account_ids[%d]", i)
+		qID := fmt.Sprintf("[%d]account_ids", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
@@ -115,7 +115,7 @@
 		if id < 1 {
 			return ErrInvalidID
 		}
-		qID := fmt.Sprintf("account_ids[%d]", i)
+		qID := fmt.Sprintf("[%d]account_ids", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 	return mc.apiCall("v1/"+endPoint, method, params, nil, nil, nil)
--- a/notifications.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/notifications.go	Fri Sep 07 19:17:12 2018 +0200
@@ -28,7 +28,7 @@
 	if len(excludeTypes) > 0 {
 		params = make(apiCallParams)
 		for i, eType := range excludeTypes {
-			qID := fmt.Sprintf("exclude_types[%d]", i)
+			qID := fmt.Sprintf("[%d]exclude_types", i)
 			params[qID] = eType
 		}
 	}
--- a/report.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/report.go	Fri Sep 07 19:17:12 2018 +0200
@@ -36,7 +36,7 @@
 		if id < 1 {
 			return nil, ErrInvalidID
 		}
-		qID := fmt.Sprintf("status_ids[%d]", i)
+		qID := fmt.Sprintf("[%d]status_ids", i)
 		params[qID] = strconv.FormatInt(id, 10)
 	}
 
--- a/status.go	Fri Sep 07 19:17:12 2018 +0200
+++ b/status.go	Fri Sep 07 19:17:12 2018 +0200
@@ -141,7 +141,7 @@
 			if id < 1 {
 				return ErrInvalidID
 			}
-			qID := fmt.Sprintf("media_ids[%d]", i)
+			qID := fmt.Sprintf("[%d]media_ids", i)
 			params[qID] = strconv.FormatInt(id, 10)
 		}
 		if opts.Sensitive {