Skip to content

Commit

Permalink
reformat sbt and scala files
Browse files Browse the repository at this point in the history
  • Loading branch information
ttim committed Sep 24, 2021
1 parent 6dd0477 commit b1c5ec7
Show file tree
Hide file tree
Showing 450 changed files with 25,546 additions and 15,932 deletions.
820 changes: 422 additions & 398 deletions build.sbt

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
resolvers ++= Seq(
"jgit-repo" at "https://download.eclipse.org/jgit/maven",
"sonatype-releases" at "https://oss.sonatype.org/content/repositories/releases",
"Twitter Maven" at "https://maven.twttr.com"
"jgit-repo".at("https://download.eclipse.org/jgit/maven"),
"sonatype-releases".at("https://oss.sonatype.org/content/repositories/releases"),
"Twitter Maven".at("https://maven.twttr.com")
)

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
addSbtPlugin("com.eed3si9n" % "sbt-unidoc" % "0.4.3")
addSbtPlugin("com.47deg" % "sbt-microsites" % "1.3.4")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.twitter" %% "scrooge-sbt-plugin" % "18.9.0")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.14")
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.8.2")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.7")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.16")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.22")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.6")
addSbtPlugin("com.eed3si9n" % "sbt-unidoc" % "0.4.3")
addSbtPlugin("com.47deg" % "sbt-microsites" % "1.3.4")
addSbtPlugin("com.github.sbt" % "sbt-release" % "1.1.0")
addSbtPlugin("com.github.sbt" % "sbt-pgp" % "2.1.2")
addSbtPlugin("com.twitter" %% "scrooge-sbt-plugin" % "18.9.0")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.14")
addSbtPlugin("com.typesafe.sbt" % "sbt-ghpages" % "0.6.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.8.2")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.7")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.16")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.22")
139 changes: 77 additions & 62 deletions scalding-args/src/main/scala/com/twitter/scalding/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
*/
package com.twitter.scalding

import scala.util.control.NonFatal

case class ArgsException(message: String) extends RuntimeException(message)

/**
* The args class does a simple command line parsing. The rules are:
* keys start with one or more "-". Each key has zero or more values
* following.
* The args class does a simple command line parsing. The rules are: keys start with one or more "-". Each key
* has zero or more values following.
*/
object Args {
val empty: Args = new Args(Map.empty)
Expand All @@ -33,36 +32,35 @@ object Args {
def apply(argString: String): Args = Args(argString.split("\\s+"))

/**
* parses keys as starting with a dash, except single dashed digits.
* All following non-dashed args are a list of values.
* If the list starts with non-dashed args, these are associated with the
* empty string: ""
* parses keys as starting with a dash, except single dashed digits. All following non-dashed args are a
* list of values. If the list starts with non-dashed args, these are associated with the empty string: ""
*/
def apply(args: Iterable[String]): Args = {
def startingDashes(word: String) = word.takeWhile { _ == '-' }.length
def startingDashes(word: String) = word.takeWhile(_ == '-').length
new Args(
//Fold into a list of (arg -> List[values])
args
.filter{ a => !a.matches("\\s*") }
.filter(a => !a.matches("\\s*"))
.foldLeft(List("" -> List[String]())) { (acc, arg) =>
val noDashes = arg.dropWhile{ _ == '-' }
val noDashes = arg.dropWhile(_ == '-')
if (arg == noDashes || isNumber(arg))
(acc.head._1 -> (arg :: acc.head._2)) :: acc.tail
else
(noDashes -> List()) :: acc
}
//Now reverse the values to keep the same order
.map { case (key, value) => key -> value.reverse }.toMap)
.map { case (key, value) => key -> value.reverse }
.toMap
)
}

def isNumber(arg: String): Boolean = {
def isNumber(arg: String): Boolean =
try {
arg.toDouble
true
} catch {
case e: NumberFormatException => false
}
}

/**
* By default, scalding will use reflection to try and identify classes to tokenize. Set to false to disable
Expand All @@ -81,10 +79,9 @@ class Args(val m: Map[String, List[String]]) extends java.io.Serializable {
def boolean(key: String): Boolean = m.contains(key)

/**
* Get the list of values associated with a given key.
* if the key is absent, return the empty list. NOTE: empty
* does not mean the key is absent, it could be a key without
* a value. Use boolean() to check existence.
* Get the list of values associated with a given key. if the key is absent, return the empty list. NOTE:
* empty does not mean the key is absent, it could be a key without a value. Use boolean() to check
* existence.
*/
def list(key: String): List[String] = m.get(key).getOrElse(List())

Expand All @@ -111,13 +108,12 @@ class Args(val m: Map[String, List[String]]) extends java.io.Serializable {
*/
def apply(position: Int): String = required(position)

override def equals(other: Any): Boolean = {
override def equals(other: Any): Boolean =
if (other.isInstanceOf[Args]) {
other.asInstanceOf[Args].m.equals(m)
} else {
false
}
}

override def hashCode(): Int = m.hashCode()

Expand All @@ -127,100 +123,119 @@ class Args(val m: Map[String, List[String]]) extends java.io.Serializable {
def getOrElse(key: String, default: String): String = optional(key).getOrElse(default)

/**
* return exactly one value for a given key.
* If there is more than one value, you get an exception
* return exactly one value for a given key. If there is more than one value, you get an exception
*/
def required(key: String): String = list(key) match {
case List() => throw ArgsException("Please provide a value for --" + key)
case List() => throw ArgsException("Please provide a value for --" + key)
case List(a) => a
case _ => throw ArgsException("Please only provide a single value for --" + key)
case _ => throw ArgsException("Please only provide a single value for --" + key)
}

def toList: List[String] = {
def toList: List[String] =
m.foldLeft(List[String]()) { (args, kvlist) =>
val k = kvlist._1
val values = kvlist._2
if (k != "") {
//Make sure positional args are first
args ++ ((("--" + k) :: values))
args ++ (("--" + k) :: values)
} else {
// These are positional args (no key), put them first:
values ++ args
}
}
}

/**
* Asserts whether all the args belong to the given set of accepted arguments.
* If an arg does not belong to the given set, you get an error.
* Asserts whether all the args belong to the given set of accepted arguments. If an arg does not belong to
* the given set, you get an error.
*/
def restrictTo(acceptedArgs: Set[String]): Unit = {
val invalidArgs = m.keySet.filter(!_.startsWith("scalding.")) -- (acceptedArgs + "" + "tool.graph" + "hdfs" + "local")
val invalidArgs =
m.keySet.filter(!_.startsWith("scalding.")) -- (acceptedArgs + "" + "tool.graph" + "hdfs" + "local")
if (!invalidArgs.isEmpty) throw ArgsException("Invalid args: " + invalidArgs.map("--" + _).mkString(", "))
}

// TODO: if there are spaces in the keys or values, this will not round-trip
override def toString: String = toList.mkString(" ")

/**
* If there is zero or one element, return it as an Option.
* If there is a list of more than one item, you get an error
* If there is zero or one element, return it as an Option. If there is a list of more than one item, you
* get an error
*/
def optional(key: String): Option[String] = list(key) match {
case List() => None
case List() => None
case List(a) => Some(a)
case _ => throw ArgsException("Please provide at most one value for --" + key)
case _ => throw ArgsException("Please provide at most one value for --" + key)
}

def int(key: String, default: Int): Int = {
optional(key).map(value => try value.toInt catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
}).getOrElse(default)
}
def int(key: String, default: Int): Int =
optional(key)
.map(value =>
try value.toInt
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
)
.getOrElse(default)

def int(key: String): Int = {
val value = required(key)
try value.toInt catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
try value.toInt
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
}

def long(key: String, default: Long): Long = {
optional(key).map(value => try value.toLong catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
}).getOrElse(default)
}
def long(key: String, default: Long): Long =
optional(key)
.map(value =>
try value.toLong
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
)
.getOrElse(default)

def long(key: String): Long = {
val value = required(key)
try value.toLong catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
try value.toLong
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
}

def float(key: String, default: Float): Float = {
optional(key).map(value => try value.toFloat catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
}).getOrElse(default)
}
def float(key: String, default: Float): Float =
optional(key)
.map(value =>
try value.toFloat
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
)
.getOrElse(default)

def float(key: String): Float = {
val value = required(key)
try value.toFloat catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
try value.toFloat
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
}

def double(key: String, default: Double): Double = {
optional(key).map(value => try value.toDouble catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
}).getOrElse(default)
}
def double(key: String, default: Double): Double =
optional(key)
.map(value =>
try value.toDouble
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
)
.getOrElse(default)

def double(key: String): Double = {
val value = required(key)
try value.toDouble catch {
case NonFatal(_) => throw ArgsException(s"Invalid value ${value} for -- ${key}")
try value.toDouble
catch {
case NonFatal(_) => throw ArgsException(s"Invalid value $value for -- $key")
}
}
}
28 changes: 13 additions & 15 deletions scalding-args/src/main/scala/com/twitter/scalding/RangedArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
*/

package com.twitter.scalding

Expand All @@ -23,35 +23,33 @@ object RangedArgs {
case class Range[T](lower: T, upper: T)(implicit ord: Ordering[T]) {
assert(ord.lteq(lower, upper), "Bad range: " + lower + " > " + upper)

def assertLowerBound(min: T): Unit = {
def assertLowerBound(min: T): Unit =
assert(ord.lteq(min, lower), "Range out of bounds: " + lower + " < " + min)
}

def assertUpperBound(max: T): Unit = {
def assertUpperBound(max: T): Unit =
assert(ord.gteq(max, upper), "Range out of bounds: " + upper + " > " + max)
}

def assertBounds(min: T, max: T): Unit = {
assertLowerBound(min)
assertUpperBound(max)
}

def mkString(sep: String) = {
def mkString(sep: String) =
if (ord.equiv(lower, upper)) {
lower.toString
} else {
lower.toString + sep + upper.toString
}
}
}

class RangedArgs(args: Args) {
def range[T](argName: String)(cnv: String => T)(implicit ord: Ordering[T]): Range[T] = args.list(argName) match {
case List(v) =>
Range(cnv(v), cnv(v))
case List(v1, v2) =>
Range(cnv(v1), cnv(v2))
case _ =>
throw new IllegalArgumentException(argName + " must have either 1 or 2 values specified")
}
def range[T](argName: String)(cnv: String => T)(implicit ord: Ordering[T]): Range[T] =
args.list(argName) match {
case List(v) =>
Range(cnv(v), cnv(v))
case List(v1, v2) =>
Range(cnv(v1), cnv(v2))
case _ =>
throw new IllegalArgumentException(argName + " must have either 1 or 2 values specified")
}
}

0 comments on commit b1c5ec7

Please sign in to comment.