chore: implement wildcard style matching for param values

This commit is contained in:
Steven Masley 2024-03-20 15:11:41 -05:00
parent 2bcdbd93ec
commit 9618597965
No known key found for this signature in database
3 changed files with 14 additions and 3 deletions

View File

@ -11990,7 +11990,7 @@ WHERE
build_params
ON
LOWER(workspace_build_parameters.name) = build_params.name AND
LOWER(workspace_build_parameters.value) = build_params.value AND
LOWER(workspace_build_parameters.value) LIKE build_params.value AND
workspace_build_parameters.workspace_build_id = latest_build.id
)
ELSE true

View File

@ -222,7 +222,7 @@ WHERE
build_params
ON
LOWER(workspace_build_parameters.name) = build_params.name AND
LOWER(workspace_build_parameters.value) = build_params.value AND
LOWER(workspace_build_parameters.value) LIKE build_params.value AND
workspace_build_parameters.workspace_build_id = latest_build.id
)
ELSE true

View File

@ -151,8 +151,19 @@ func Workspaces(query string, page codersdk.Pagination, agentInactiveDisconnectT
filter.HasParam = append(filter.HasParam, p.name)
continue
}
// In postgres, '%' is the wildcard character for matching. But * is a more commonly
// used character.
// First escape any '%' characters in the value to treat them as literals.
valueMatch := strings.ReplaceAll(*p.value, "%", `\%`)
// Then replace wildcards with the '%' character. If someone wants the
// literal '*' character... then we will have to implement something.
// If we have to implement escaping '*', then we have to implement escaping the
// escape character as well, which might actually be used in a value.
// Let's avoid the complexity until we need it.
valueMatch = strings.ReplaceAll(valueMatch, "*", "%")
filter.ParamNames = append(filter.ParamNames, p.name)
filter.ParamValues = append(filter.ParamValues, *p.value)
filter.ParamValues = append(filter.ParamValues, valueMatch)
}
parser.ErrorExcessParams(values)