-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CodecXXX capable of updating map, list, set types in statement in…
…stead of replacing it (#263) * Add CodecXXX capable of updating map, list, set types in statement instead of replacing it * drop Codec and impl Update instead --------- Co-authored-by: Denys Fakhritdinov <dfakhritdinov@evolution.com>
- Loading branch information
1 parent
f10aca7
commit aa8356b
Showing
8 changed files
with
273 additions
and
13 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
scassandra/src/main/scala/com/evolutiongaming/scassandra/UpdateByIdx.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,40 @@ | ||
package com.evolutiongaming.scassandra | ||
|
||
import com.datastax.driver.core.{GettableByIndexData, SettableData} | ||
|
||
trait UpdateByIdx[-A] { | ||
|
||
def apply[D <: GettableByIndexData & SettableData[D]]( | ||
data: D, | ||
idx: Int, | ||
value: A | ||
): D | ||
|
||
} | ||
|
||
object UpdateByIdx { | ||
|
||
def apply[A: UpdateByIdx]: UpdateByIdx[A] = implicitly | ||
|
||
implicit def fromEncodeByIdx[A: EncodeByIdx]: UpdateByIdx[A] = | ||
new UpdateByIdx[A] { | ||
def apply[D <: GettableByIndexData & SettableData[D]]( | ||
data: D, | ||
idx: Int, | ||
value: A | ||
): D = EncodeByIdx[A].apply(data, idx, value) | ||
} | ||
|
||
implicit final class Syntax[A](val self: UpdateByIdx[A]) extends AnyVal { | ||
|
||
def contramap[B](f: B => A): UpdateByIdx[B] = new UpdateByIdx[B] { | ||
def apply[D <: GettableByIndexData & SettableData[D]]( | ||
data: D, | ||
idx: Int, | ||
value: B | ||
): D = self(data, idx, f(value)) | ||
} | ||
|
||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
scassandra/src/main/scala/com/evolutiongaming/scassandra/UpdateByName.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,39 @@ | ||
package com.evolutiongaming.scassandra | ||
|
||
import com.datastax.driver.core.{GettableByNameData, SettableData} | ||
|
||
trait UpdateByName[-A] { | ||
|
||
def apply[D <: GettableByNameData & SettableData[D]]( | ||
data: D, | ||
name: String, | ||
value: A | ||
): D | ||
|
||
} | ||
|
||
object UpdateByName { | ||
|
||
def apply[A: UpdateByName]: UpdateByName[A] = implicitly | ||
|
||
implicit def fromEncodeByName[A: EncodeByName]: UpdateByName[A] = | ||
new UpdateByName[A] { | ||
def apply[D <: GettableByNameData & SettableData[D]]( | ||
data: D, | ||
name: String, | ||
value: A | ||
): D = EncodeByName[A].apply(data, name, value) | ||
} | ||
|
||
implicit final class Syntax[A](val self: UpdateByName[A]) extends AnyVal { | ||
|
||
def contramap[B](f: B => A): UpdateByName[B] = new UpdateByName[B] { | ||
def apply[D <: GettableByNameData & SettableData[D]]( | ||
data: D, | ||
name: String, | ||
value: B | ||
): D = self(data, name, f(value)) | ||
} | ||
|
||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
scassandra/src/main/scala/com/evolutiongaming/scassandra/UpdateRow.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,36 @@ | ||
package com.evolutiongaming.scassandra | ||
|
||
import com.datastax.driver.core.{GettableData, SettableData} | ||
|
||
trait UpdateRow[-A] { | ||
|
||
def apply[D <: GettableData & SettableData[D]]( | ||
data: D, | ||
value: A | ||
): D | ||
|
||
} | ||
|
||
object UpdateRow { | ||
|
||
def apply[A: UpdateRow]: UpdateRow[A] = implicitly | ||
|
||
implicit def fromEncodeRow[A: EncodeRow]: UpdateRow[A] = | ||
new UpdateRow[A] { | ||
def apply[D <: GettableData & SettableData[D]]( | ||
data: D, | ||
value: A | ||
): D = EncodeRow[A].apply(data, value) | ||
} | ||
|
||
implicit final class Syntax[A](val self: UpdateRow[A]) extends AnyVal { | ||
|
||
def contramap[B](f: B => A): UpdateRow[B] = new UpdateRow[B] { | ||
def apply[D <: GettableData & SettableData[D]]( | ||
data: D, | ||
value: B | ||
): D = self(data, f(value)) | ||
} | ||
|
||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
scassandra/src/test/scala/com/evolutiongaming/scassandra/UpdateByIdxSpec.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,56 @@ | ||
package com.evolutiongaming.scassandra | ||
|
||
import java.time.{Instant, LocalDate => LocalDateJ} | ||
import java.time.temporal.ChronoUnit | ||
|
||
import com.datastax.driver.core.{Duration, LocalDate} | ||
import com.evolutiongaming.scassandra.syntax._ | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class UpdateByIdxSpec extends AnyWordSpec with Matchers { | ||
|
||
def of[A](expected: A)(implicit | ||
q: UpdateByIdx[A], | ||
e: UpdateByIdx[Option[A]], | ||
r: DecodeByIdx[A], | ||
t: DecodeByIdx[Option[A]] | ||
) = { () => | ||
{ | ||
val data = DataMock() | ||
|
||
data | ||
.updateAt[A](0, expected) | ||
.decodeAt[A](0) shouldEqual expected | ||
|
||
data | ||
.updateAt[Option[A]](1, Some(expected)) | ||
.decodeAt[Option[A]](1) shouldEqual Some(expected) | ||
|
||
data | ||
.updateAt[Option[A]](2, None) | ||
.decodeAt[Option[A]](2) shouldEqual None | ||
} | ||
} | ||
|
||
"CodecByIdx" should { | ||
|
||
for { | ||
(name, test) <- List( | ||
("String", of("string")), | ||
("Int", of(0)), | ||
("Long", of(0L)), | ||
("BigDecimal", of(BigDecimal(0))), | ||
("Double", of(0d)), | ||
("Float", of(0f)), | ||
("Instant", of(Instant.now().truncatedTo(ChronoUnit.MILLIS))), | ||
("Set", of(Set("str"))), | ||
("Duration", of(Duration.newInstance(1, 1, 1))), | ||
("LocalDate", of(LocalDate.fromYearMonthDay(2019, 10, 4))), | ||
("LocalDateJ", of(LocalDateJ.of(2019, 10, 4))) | ||
) | ||
} { | ||
s"encode & decode $name" in test() | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
scassandra/src/test/scala/com/evolutiongaming/scassandra/UpdateByNameSpec.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,56 @@ | ||
package com.evolutiongaming.scassandra | ||
|
||
import java.time.{Instant, LocalDate => LocalDateJ} | ||
import java.time.temporal.ChronoUnit | ||
|
||
import com.datastax.driver.core.{Duration, LocalDate} | ||
import com.evolutiongaming.scassandra.syntax._ | ||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class UpdateByNameSpec extends AnyWordSpec with Matchers { | ||
|
||
def of[A](expected: A)(implicit | ||
a: UpdateByName[A], | ||
b: UpdateByName[Option[A]], | ||
c: DecodeByName[A], | ||
d: DecodeByName[Option[A]] | ||
) = { () => | ||
{ | ||
val data = DataMock() | ||
|
||
data | ||
.update[A]("0", expected) | ||
.decode[A]("0") shouldEqual expected | ||
|
||
data | ||
.update[Option[A]]("1", Some(expected)) | ||
.decode[Option[A]]("1") shouldEqual Some(expected) | ||
|
||
data | ||
.update[Option[A]]("2", None) | ||
.decode[Option[A]]("2") shouldEqual None | ||
} | ||
} | ||
|
||
"CodecByName" should { | ||
|
||
for { | ||
(name, test) <- List( | ||
("String", of("string")), | ||
("Int", of(0)), | ||
("Long", of(0L)), | ||
("BigDecimal", of(BigDecimal(0))), | ||
("Double", of(0d)), | ||
("Float", of(0f)), | ||
("Instant", of(Instant.now().truncatedTo(ChronoUnit.MILLIS))), | ||
("Set", of(Set("str"))), | ||
("Duration", of(Duration.newInstance(1, 1, 1))), | ||
("LocalDate", of(LocalDate.fromYearMonthDay(2019, 10, 4))), | ||
("LocalDateJ", of(LocalDateJ.of(2019, 10, 4))) | ||
) | ||
} { | ||
s"encode & decode $name" in test() | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
ThisBuild / version := "5.2.2-SNAPSHOT" | ||
ThisBuild / version := "5.3.0" |