Skip to content

Commit

Permalink
parse duplicate values in/out of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
drernie committed Jan 27, 2024
1 parent 656e6f5 commit a1fe762
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
47 changes: 40 additions & 7 deletions plugins/nf-quilt/src/main/nextflow/quilt/jep/QuiltParser.groovy
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* groovylint-disable Instanceof */
/*
* Copyright 2022, Quilt Data Inc
*
Expand Down Expand Up @@ -75,18 +76,50 @@ class QuiltParser {
return new QuiltParser(uri.authority, pkg, path, options, metadata)
}

static Map<String,Object> parseQuery(String query) {
static String decode(String str) {
return URLDecoder.decode(str, StandardCharsets.UTF_8)
}

static String encode(String str) {
return URLEncoder.encode(str, StandardCharsets.UTF_8)
}

static Map<String, Object> parseQuery(String query) {
if (!query) { return [:] } // skip for urls without query params
final queryParams = query.split('&')
return queryParams.collectEntries { params -> params.split('=').collect { param -> URLDecoder.decode(param) } }
def params = query.split('&')
def result = [:]
params.each { param ->
def keyValue = param.split('=')
if (keyValue.size() == 2) {
String key = decode(keyValue[0])
String value = decode(keyValue[1])
if (result.containsKey(key)) {
if (result[key] instanceof List) {
result[key].add(value)
} else {
result[key] = [result[key], value]
}
} else {
result[key] = value
}
}

}
return result
}

static String encodePair(String key, String value) {
return "${QuiltParser.encode(key)}=${QuiltParser.encode(value)}"
}

static String unparseQuery(Map<String,Object> query) {
if (!query) { return '' } // skip for urls without query params
List<String> params = query.collect { key, val ->
String k = URLEncoder.encode(key, StandardCharsets.UTF_8)
String v = URLEncoder.encode(val.toString(), StandardCharsets.UTF_8)
"${k}=${v}".toString()
List<String> params = query.collect { key, value ->
if (value instanceof List) {
value.collect { QuiltParser.encodePair(key, it.toString()) }.join('&')
} else {
QuiltParser.encodePair(key, value.toString())
}
}
return params.join('&')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,17 @@ class QuiltParserTest extends QuiltSpecification {
unparsed.replace('%2f', '/') == fullURL
}

void 'should collect array parameters from query string'() {
when:
String query = 'key=val1,val2&quay=vale1&quay=vale2'
Map<String,Object> result = QuiltParser.parseQuery(query)
println "QuiltParserTest[$query] -> ${result}"
String unparsed = QuiltParser.unparseQuery(result)

then:
result['key'] == 'val1,val2'
result['quay'] == ['vale1', 'vale2']
unparsed == query.replace(',', '%2C')
}

}

0 comments on commit a1fe762

Please sign in to comment.