Skip to content

Commit 3254522

Browse files
committed
Merge pull request #174 from twitter/jco/anyrefmap
Add ScalaAnyRefMapConfig, deals with non-string keys in cascading
2 parents a56d565 + a1998c0 commit 3254522

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2012 Twitter, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package com.twitter.chill.config
18+
19+
object ScalaAnyRefMapConfig {
20+
def apply(m: Map[_ <: AnyRef, _ <: AnyRef]): ScalaAnyRefMapConfig =
21+
new ScalaAnyRefMapConfig(m.map { case (k, v) => (k.asInstanceOf[AnyRef], v.asInstanceOf[AnyRef]) })
22+
def empty: ScalaAnyRefMapConfig = new ScalaAnyRefMapConfig(Map.empty)
23+
}
24+
25+
/** A simple config backed by an immutable Map. Note that this replaces ScalaMapConfig because
26+
* in cascading non-string values are perfectly legal.
27+
*/
28+
class ScalaAnyRefMapConfig(in: Map[AnyRef, AnyRef]) extends Config {
29+
private var conf = in
30+
31+
def toMap: Map[AnyRef, AnyRef] = conf
32+
33+
override def get(k: String) = conf.get(k).map { _.toString }.getOrElse(null)
34+
override def set(k: String, v: String) {
35+
Option(v) match {
36+
case Some(r) => conf += k -> v
37+
case None => conf -= k
38+
}
39+
}
40+
}

chill-scala/src/main/scala/com/twitter/chill/config/ScalaMapConfig.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ object ScalaMapConfig {
2323

2424
/** A simple config backed by an immutable Map
2525
*/
26+
@deprecated("Class potentially unsafe, use ScalaAnyRefMapConfig", "0.3.6")
2627
class ScalaMapConfig(in: Map[String, String]) extends Config {
2728
private var conf = in
2829

chill-scala/src/test/scala/com/twitter/chill/config/ReflectingInstantiatorTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,18 @@ class ReflectingInstantiatorTest extends Specification {
3535
skipMissing = true,
3636
registrationRequired = true).build
3737

38-
val conf = ScalaMapConfig.empty
38+
val conf = ScalaAnyRefMapConfig.empty
3939
ri.set(conf)
4040

4141
conf.toMap(ReflectingInstantiator.KRYO_CLASS) must be_==(classOf[Kryo].getName)
4242
conf.toMap(ReflectingInstantiator.INSTANTIATOR_STRATEGY_CLASS) must be_==(classOf[InstantiatorStrategy].getName)
4343
conf.toMap(ReflectingInstantiator.REGISTRATION_REQUIRED) must be_==("true")
4444
conf.toMap(ReflectingInstantiator.SKIP_MISSING) must be_==("true")
45-
conf.toMap(ReflectingInstantiator.REGISTRATIONS).split(":").toSet must be_== (
45+
conf.toMap(ReflectingInstantiator.REGISTRATIONS).asInstanceOf[String].split(":").toSet must be_== (
4646
Set("scala.collection.immutable.List",
4747
"scala.collection.immutable.List,com.esotericsoftware.kryo.serializers.JavaSerializer")
4848
)
49-
conf.toMap(ReflectingInstantiator.DEFAULT_REGISTRATIONS).split(":").toSet must be_== (
49+
conf.toMap(ReflectingInstantiator.DEFAULT_REGISTRATIONS).asInstanceOf[String].split(":").toSet must be_== (
5050
Set("scala.collection.immutable.List,com.esotericsoftware.kryo.serializers.JavaSerializer")
5151
)
5252
}
@@ -58,7 +58,7 @@ class ReflectingInstantiatorTest extends Specification {
5858
skipMissing = true,
5959
registrationRequired = true).build
6060

61-
val conf = ScalaMapConfig.empty
61+
val conf = ScalaAnyRefMapConfig.empty
6262
ri.set(conf)
6363
val ri2 = new ReflectingInstantiator(conf)
6464
ri.equals(ri2) must be_==(true)

0 commit comments

Comments
 (0)