Skip to content

Commit c23a908

Browse files
authored
Merge pull request #249 from Olshansk/tuple1_bijection
Add support for Tuple1 bijections
2 parents 029e439 + 164fdd2 commit c23a908

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

bijection-macros/src/main/scala/com/twitter/bijection/macros/impl/CaseClassToTuple.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ private[bijection] object CaseClassToTuple {
4343

4444
//TODO can make error handling better
4545
val companion = T.tpe.typeSymbol.companionSymbol
46+
val tuple = Tup.tpe.typeSymbol.companionSymbol
47+
4648
val getPutConv = T.tpe
4749
.declarations
4850
.collect { case m: MethodSymbol if m.isCaseAccessor => m }
@@ -73,7 +75,7 @@ private[bijection] object CaseClassToTuple {
7375
new Bijection[$T,$Tup] with MacroGenerated {
7476
override def apply(t: $T): $Tup = {
7577
..$converters
76-
(..$putters)
78+
$tuple(..$putters)
7779
}
7880
override def invert(tup: $Tup): $T = {
7981
..$converters

project/BufferableGenerator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object BufferableGenerator {
2929
def typeList(cnt: Int) =
3030
upperLetters.slice(0, cnt) map { _.toString } mkString(",")
3131

32-
def parensTypeList(cnt: Int) = "(" + typeList(cnt) + ")"
32+
def tupleTypeList(cnt: Int) = "Tuple" + cnt + "[" + typeList(cnt) + "]"
3333

3434
def reallocatingPut(idx: Int) =
3535
"nextBb = reallocatingPut(nextBb) { b" + lowerLetters(idx) + ".put(_, tup._" + (idx+1) +") }"
@@ -46,15 +46,15 @@ object BufferableGenerator {
4646
def implicitTuple(cnt: Int): String =
4747
" implicit def tuple" + cnt + "[" + typeList(cnt) + "](implicit " +
4848
((0 until cnt) map { bufferableParam(_) } mkString(", ") ) + "):\n" +
49-
" Bufferable[" + parensTypeList(cnt) + "] = new AbstractBufferable[" + parensTypeList(cnt) +"] {\n" +
50-
" def put(bytebuf: ByteBuffer, tup: "+ parensTypeList(cnt) +") = {\n" +
49+
" Bufferable[" + tupleTypeList(cnt) + "] = new AbstractBufferable[" + tupleTypeList(cnt) +"] {\n" +
50+
" def put(bytebuf: ByteBuffer, tup: "+ tupleTypeList(cnt) +") = {\n" +
5151
" var nextBb = bytebuf\n" +
5252
" " + ((0 until cnt) map { reallocatingPut(_) }).mkString("","\n ","\n") +
5353
" nextBb\n" +
5454
" }\n"+
5555
" def get(bytebuf: ByteBuffer) = attempt(bytebuf) { bytebuf =>\n" +
5656
" " + ((0 until cnt) map { bufferGet(_) }).mkString("","\n ","\n") +
57-
" val res = " +(0 until cnt).map { lowerLetters(_) }.mkString("(",", ",")") + "\n" +
57+
" val res = Tuple" + cnt +(0 until cnt).map { lowerLetters(_) }.mkString("(",", ",")") + "\n" +
5858
" (buf" + lowerLetters(cnt - 1) +", res)\n" +
5959
" }\n" +
6060
" }"
@@ -69,7 +69,7 @@ object BufferableGenerator {
6969
b.append("import com.twitter.bijection.Inversion.attempt\n")
7070

7171
b.append("\ntrait GeneratedTupleBufferable {\n")
72-
(2 to 22).foreach { cnt => b.append(implicitTuple(cnt)).append("\n") }
72+
(1 to 22).foreach { cnt => b.append(implicitTuple(cnt)).append("\n") }
7373
b.append("}")
7474

7575
b.toString

0 commit comments

Comments
 (0)