Skip to content

Commit

Permalink
quotedOrList implementation (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegIlyenko committed Apr 30, 2016
1 parent 308031e commit 81a3797
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class DeriveEnumTypeMacro(context: blackbox.Context) extends {
val configName = config.collect{case MacroRenameValue(`name`, tree, _) tree}.lastOption
val actualName =
if (config.exists(_.isInstanceOf[MacroUppercaseValues]))
q"sangria.util.StringUtil.camelCaseToUpperCase(${configName orElse annotationName getOrElse q"$name"})"
q"sangria.util.StringUtil.camelCaseToUnderscore(${configName orElse annotationName getOrElse q"$name"}).toUpperCase"
else
q"${configName orElse annotationName getOrElse q"$name"}"

Expand Down
19 changes: 17 additions & 2 deletions src/main/scala/sangria/util/StringUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@ package sangria.util
object StringUtil {
private val camelToUpper = "_*([A-Z][a-z\\d]+)".r

def camelCaseToUpperCase(name: String) =
camelToUpper.findAllMatchIn(name).map(_.group(1).toUpperCase).mkString("_")
def camelCaseToUnderscore(name: String) =
camelToUpper findAllMatchIn name map (_.group(1).toLowerCase) mkString "_"

/**
* Given [ A, B, C ] return '"A", "B" or "C"'.
*/
def quotedOrList(items: Seq[String], limit: Int = 5): String =
if (items.isEmpty)
throw new IllegalArgumentException("List is empty")
else {
val quoted = items map ("\"" + _ + "\"") take limit
val start = quoted dropRight 1
val last = quoted.last

if (start.nonEmpty) s"${start mkString ", "} or $last"
else last
}
}
37 changes: 37 additions & 0 deletions src/test/scala/sangria/util/StringUtilSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sangria.util

import org.scalatest.{Matchers, WordSpec}
import sangria.util.StringUtil._

class StringUtilSpec extends WordSpec with Matchers {
"camelCaseToUnderscore" should {
"convert camel-case identifiers to underscore-based one" in {
camelCaseToUnderscore("FooBar") should be ("foo_bar")
camelCaseToUnderscore("Foo1Bar") should be ("foo1_bar")
camelCaseToUnderscore("FooBar_Baz") should be ("foo_bar_baz")
camelCaseToUnderscore("_Foo_Bar_") should be ("foo_bar")
}
}

"quotedOrList" should {
"Does not accept an empty list" in {
an [IllegalArgumentException] should be thrownBy quotedOrList(Nil)
}

"Returns single quoted item" in {
quotedOrList(Seq("A")) should be ("\"A\"")
}

"Returns two item list" in {
quotedOrList(Seq("A", "B")) should be ("\"A\" or \"B\"")
}

"Returns comma separated many item list" in {
quotedOrList(Seq("A", "B", "C")) should be ("\"A\", \"B\" or \"C\"")
}

"Limits to five items" in {
quotedOrList(Seq("A", "B", "C", "D", "E", "F")) should be ("\"A\", \"B\", \"C\", \"D\" or \"E\"")
}
}
}

0 comments on commit 81a3797

Please sign in to comment.