Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
lornaluo authored May 2, 2024
2 parents f935682 + 20568a0 commit 458e99f
Show file tree
Hide file tree
Showing 17 changed files with 2,021 additions and 60 deletions.
31 changes: 31 additions & 0 deletions mmv1/google/string_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,34 @@ func Camelize(term string, firstLetter string) string {
})
return res
}

/*
Transforms a format string with field markers to a regex string with capture groups.
For instance,
projects/{{project}}/global/networks/{{name}}
is transformed to
projects/(?P<project>[^/]+)/global/networks/(?P<name>[^/]+)
Values marked with % are URL-encoded, and will match any number of /'s.
Note: ?P indicates a Python-compatible named capture group. Named groups
aren't common in JS-based regex flavours, but are in Perl-based ones
*/
func Format2Regex(format string) string {
re := regexp.MustCompile(`\{\{%([[:word:]]+)\}\}`)
result := re.ReplaceAllStringFunc(format, func(match string) string {
// TODO: the trims may not be needed with more effecient regex
word := strings.TrimPrefix(match, "{{")
word = strings.TrimSuffix(word, "}}")
return fmt.Sprintf("(?P<%s>.+)", word)
})
re = regexp.MustCompile(`\{\{([[:word:]]+)\}\}`)
result = re.ReplaceAllStringFunc(result, func(match string) string {
word := strings.TrimPrefix(match, "{{")
word = strings.TrimSuffix(word, "}}")
return fmt.Sprintf("(?P<%s>[^/]+)", word)
})
return result
}
21 changes: 11 additions & 10 deletions mmv1/provider/template_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,17 @@ func wrapMultipleParams(params ...interface{}) (map[string]interface{}, error) {
}

var TemplateFunctions = template.FuncMap{
"title": google.SpaceSeparatedTitle,
"replace": strings.Replace,
"camelize": google.Camelize,
"underscore": google.Underscore,
"plural": google.Plural,
"contains": strings.Contains,
"join": strings.Join,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"dict": wrapMultipleParams,
"title": google.SpaceSeparatedTitle,
"replace": strings.Replace,
"camelize": google.Camelize,
"underscore": google.Underscore,
"plural": google.Plural,
"contains": strings.Contains,
"join": strings.Join,
"lower": strings.ToLower,
"upper": strings.ToUpper,
"dict": wrapMultipleParams,
"format2regex": google.Format2Regex,
}

var GA_VERSION = "ga"
Expand Down
20 changes: 0 additions & 20 deletions mmv1/provider/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,26 +864,6 @@ func (t Terraform) replaceImportPath(outputFolder, target string) {
//
// end
//
// # Transforms a format string with field markers to a regex string with
// # capture groups.
// #
// # For instance,
// # projects/{{project}}/global/networks/{{name}}
// # is transformed to
// # projects/(?P<project>[^/]+)/global/networks/(?P<name>[^/]+)
// #
// # Values marked with % are URL-encoded, and will match any number of /'s.
// #
// # Note: ?P indicates a Python-compatible named capture group. Named groups
// # aren't common in JS-based regex flavours, but are in Perl-based ones
// def format2regex(format)
//
// format
// .gsub(/\{\{%([[:word:]]+)\}\}/, '(?P<\1>.+)')
// .gsub(/\{\{([[:word:]]+)\}\}/, '(?P<\1>[^/]+)')
//
// end
//
// # Capitalize the first letter of a property name.
// # E.g. "creationTimestamp" becomes "CreationTimestamp".
// def titlelize_property(property)
Expand Down
Loading

0 comments on commit 458e99f

Please sign in to comment.