-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #334 from takapi327/refactor/2024-12-Change-use-sc…
…hema Refactor/2024 12 change use schema
- Loading branch information
Showing
20 changed files
with
325 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
module/ldbc-codegen/shared/src/main/scala/ldbc/codegen/formatter/Naming.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) 2023-2024 by Takahiko Tominaga | ||
* This software is licensed under the MIT License (MIT). | ||
* For more information see LICENSE or https://opensource.org/licenses/MIT | ||
*/ | ||
|
||
package ldbc.codegen.formatter | ||
|
||
import scala.annotation.tailrec | ||
|
||
/** | ||
* Enum of naming rules | ||
*/ | ||
enum Naming: | ||
case CAMEL, PASCAL, SNAKE | ||
|
||
object Naming: | ||
|
||
def fromString(str: String): Naming = | ||
str match | ||
case "CAMEL" => CAMEL | ||
case "PASCAL" => PASCAL | ||
case "SNAKE" => SNAKE | ||
case unknown => | ||
throw new IllegalArgumentException( | ||
s"$unknown does not match any of the Naming, it must be CAMEL, PASCAL, SNAKE, or KEBAB." | ||
) | ||
|
||
extension (`case`: Naming) | ||
def format(name: String): String = | ||
`case` match | ||
case CAMEL => toCamel(name) | ||
case PASCAL => toPascal(name) | ||
case SNAKE => toSnake(name) | ||
|
||
/** | ||
* Converts to camelCase e.g.: PascalCase => pascalCase | ||
* | ||
* @param name | ||
* name to be converted to camelCase | ||
* @return | ||
* camelCase version of the string passed | ||
*/ | ||
def toCamel(name: String): String = | ||
toSnake(name).split("_").toList match | ||
case Nil => name | ||
case head :: tail => head + tail.map(v => s"${ v.charAt(0).toUpper }${ v.drop(1) }").mkString | ||
|
||
/** | ||
* Converts to PascalCase e.g.: camelCase => CamelCase | ||
* | ||
* @param name | ||
* name to be converted to PascalCase | ||
* @return | ||
* PascalCase version of the string passed | ||
*/ | ||
def toPascal(name: String): String = | ||
val list = toSnake(name).split("_").toList | ||
if list.nonEmpty && !(list.size == 1 && list.head == "") then | ||
list.map(v => s"${ v.charAt(0).toUpper }${ v.drop(1) }").mkString | ||
else name | ||
|
||
/** | ||
* Converts to snake_case e.g.: camelCase => camel_case | ||
* | ||
* @param name | ||
* name to be converted to snake_case | ||
* @return | ||
* snake_case version of the string passed | ||
*/ | ||
def toSnake(name: String): String = | ||
@tailrec def go(accDone: List[Char], acc: List[Char]): List[Char] = acc match | ||
case Nil => accDone | ||
case a :: b :: c :: tail if a.isUpper && b.isUpper && c.isLower => go(accDone ++ List(a, '_', b, c), tail) | ||
case a :: b :: tail if a.isLower && b.isUpper => go(accDone ++ List(a, '_', b), tail) | ||
case a :: tail => go(accDone :+ a, tail) | ||
go(Nil, name.toList).mkString.toLowerCase.replaceAll("-", "_") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.