Skip to content

Commit

Permalink
abstract the logic to convert from snake_case to camelCase to a strin…
Browse files Browse the repository at this point in the history
…g helper method
  • Loading branch information
MrPowers committed Nov 29, 2018
1 parent d6363ac commit ac0b222
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package com.github.mrpowers.spark.daria.sql

import com.github.mrpowers.spark.daria.sql.DataFrameExt._
import com.github.mrpowers.spark.daria.sql.functions.truncate
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types.{StructField, StructType}
import org.apache.spark.sql.{Column, DataFrame, Row, SparkSession}
import org.apache.spark.sql.{Column, DataFrame}

case class InvalidColumnSortOrderException(smth: String) extends Exception(smth)

Expand Down Expand Up @@ -88,7 +86,7 @@ object transformations {
df.columns.foldLeft(df) { (memoDF, colName) =>
memoDF.withColumnRenamed(
colName,
toSnakeCase(colName)
com.github.mrpowers.spark.daria.utils.StringHelpers.toSnakeCase(colName)
)
}
}
Expand All @@ -99,21 +97,9 @@ object transformations {
*/
def camelCaseToSnakeCaseColumns()(df: DataFrame): DataFrame =
df.renameColumns(
_.replaceAll(
"([A-Z]+)",
"_$1"
).toLowerCase.stripPrefix("_")
com.github.mrpowers.spark.daria.utils.StringHelpers.camelCaseToSnakeCase
)

private def toSnakeCase(str: String): String = {
str
.replaceAll(
"\\s+",
"_"
)
.toLowerCase
}

/**
* Title Cases all the columns of a DataFrame
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,23 @@ object StringHelpers {
})
}

def toSnakeCase(str: String): String = {
str
.replaceAll(
"\\s+",
"_"
)
.toLowerCase
}

def camelCaseToSnakeCase(str: String): String = {
str
.replaceAll(
"([A-Z]+)",
"_$1"
)
.toLowerCase
.stripPrefix("_")
}

}

0 comments on commit ac0b222

Please sign in to comment.