diff options
author | Vlad Uspensky <v.uspenskiy@icloud.com> | 2017-08-28 09:57:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-28 09:57:06 -0700 |
commit | f7621e318de56865b44a6a07ea45e37bbb3f80c1 (patch) | |
tree | ca6d966182e21d072895f7c32bea2fbc35bc2c23 /src/main/scala/xyz/driver/pdsuicommon | |
parent | 4512539b01e6e598d494246426b9eac261d10acf (diff) | |
parent | b249515292bfefbb70d1b5743ca46ced6da22a00 (diff) | |
download | rest-query-f7621e318de56865b44a6a07ea45e37bbb3f80c1.tar.gz rest-query-f7621e318de56865b44a6a07ea45e37bbb3f80c1.tar.bz2 rest-query-f7621e318de56865b44a6a07ea45e37bbb3f80c1.zip |
Merge pull request #21 from drivergroup/queryparsers
Added converting camel case to snake case for filters and sorting AST
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon')
3 files changed, 29 insertions, 3 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala index 768e5f5..7bbcd10 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/SearchFilterParser.scala @@ -5,6 +5,7 @@ import fastparse.all._ import fastparse.core.Parsed import fastparse.parsers.Intrinsics.CharPred import xyz.driver.pdsuicommon.db.{SearchFilterBinaryOperation, SearchFilterExpr, SearchFilterNAryOperation} +import xyz.driver.pdsuicommon.utils.Utils._ import scala.util.Try @@ -56,7 +57,8 @@ object SearchFilterParser { CharPred(c => c.isLetterOrDigit) .rep(min = 1)).!.map(SearchFilterExpr.Dimension(None, _)) val pathParser = P(identParser.! ~ "." ~ identParser.!) map { - case (left, right) => SearchFilterExpr.Dimension(Some(left), right) + case (left, right) => + SearchFilterExpr.Dimension(Some(toSnakeCase(left)), toSnakeCase(right)) } P(pathParser | identParser) } diff --git a/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala b/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala index c1c332f..4bfc669 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/parsers/SortingParser.scala @@ -3,6 +3,7 @@ package xyz.driver.pdsuicommon.parsers import xyz.driver.pdsuicommon.db.{Sorting, SortingOrder} import fastparse.all._ import fastparse.core.Parsed +import xyz.driver.pdsuicommon.utils.Utils._ import scala.util.Try @@ -18,8 +19,11 @@ object SortingParser { case (sortingOrder, field) => val prefixedFields = field.split("\\.", 2) prefixedFields.size match { - case 1 => Sorting.Dimension(None, field, sortingOrder) - case 2 => Sorting.Dimension(Some(prefixedFields.head), prefixedFields.last, sortingOrder) + case 1 => Sorting.Dimension(None, toSnakeCase(field), sortingOrder) + case 2 => + Sorting.Dimension(Some(prefixedFields.head).map(toSnakeCase), + toSnakeCase(prefixedFields.last), + sortingOrder) } } } diff --git a/src/main/scala/xyz/driver/pdsuicommon/utils/Utils.scala b/src/main/scala/xyz/driver/pdsuicommon/utils/Utils.scala index 02c9e28..63b0572 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/utils/Utils.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/utils/Utils.scala @@ -1,6 +1,7 @@ package xyz.driver.pdsuicommon.utils import java.time.LocalDateTime +import java.util.regex.{Matcher, Pattern} object Utils { @@ -20,4 +21,23 @@ object Utils { fullClassName.substring(fullClassName.lastIndexOf("$") + 1) } } + + def toSnakeCase(str: String): String = + str + .replaceAll("([A-Z]+)([A-Z][a-z])", "$1_$2") + .replaceAll("([a-z\\d])([A-Z])", "$1_$2") + .toLowerCase + + def toCamelCase(str: String): String = { + val sb = new StringBuffer() + def loop(m: Matcher): Unit = if (m.find()) { + m.appendReplacement(sb, m.group(1).toUpperCase()) + loop(m) + } + val m: Matcher = Pattern.compile("_(.)").matcher(str) + loop(m) + m.appendTail(sb) + sb.toString + } + } |