Skip to content

Commit

Permalink
Remove curly braces from transformed directory pattern (closes #137)
Browse files Browse the repository at this point in the history
  • Loading branch information
arihantsurana authored and BenFradet committed Jul 24, 2018
1 parent f0a5ba8 commit 3ad0d10
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ object DynamicPath {
(str, Try(DateTimeFormat.forPattern(str).withZone(DateTimeZone.UTC).print(decoratorDateTime)).getOrElse(str))
}
.toList
val directory = replacements.foldLeft(pattern) {
val directoryWithBraces = replacements.foldLeft(pattern) {
(dir, replacement) => dir.replace(replacement._1, replacement._2)
}
// Remove braces from the final pure directory path
val pure = """\{([^}]*)\}""".r
val directory = pure.replaceAllIn(directoryWithBraces, "$1")
// ensure trailing slash in the directory only if a pattern was supplied
val finalPath = if (directory.endsWith("/") || directory.isEmpty) s"$directory$fileName"
else s"$directory/$fileName"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ class DynamicPathSpec extends Specification {
"correctly decorate Time formats with one time pattern" in {
val directoryPattern = Some("something/{YYYY}")
val actual = DynamicPath.decorateDirectoryWithTime(directoryPattern, file, time)
actual must_== "something/{1970}/bar.gz"
actual must_== "something/1970/bar.gz"
}

"correctly decorate Time formats with multiple time patterns" in {
val directoryPattern = Some("something/{YYYY}/{mm}dy={dd}")
val actual = DynamicPath.decorateDirectoryWithTime(directoryPattern, file, time)
actual must_== "something/{1970}/{01}dy={01}/bar.gz"
actual must_== "something/1970/01dy=01/bar.gz"
}

"correctly decorate Time formats with extra trailing slash" in {
val directoryPattern = Some("something/{YYYY}/{mm}dy={dd}/")
val actual = DynamicPath.decorateDirectoryWithTime(directoryPattern, file, time)
actual must_== "something/{1970}/{01}dy={01}/bar.gz"
actual must_== "something/1970/01dy=01/bar.gz"
}

"correctly decorate Time formats with invalid time format" in {
val directoryPattern = Some("something/{YYYY}/{mm}dy={dd}/{foo}")
val actual = DynamicPath.decorateDirectoryWithTime(directoryPattern, file, time)
actual must_== "something/{1970}/{01}dy={01}/{foo}/bar.gz"
actual must_== "something/1970/01dy=01/foo/bar.gz"
}

"correctly handle no format" in {
Expand Down

0 comments on commit 3ad0d10

Please sign in to comment.