Skip to content

Commit

Permalink
drop Codec and impl Update instead
Browse files Browse the repository at this point in the history
  • Loading branch information
Denys Fakhritdinov committed Sep 6, 2024
1 parent 1fec425 commit f26a2c0
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 239 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

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))
}

}

}
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))
}

}
}
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))
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,18 @@ object syntax {
}
}

implicit class ScassandraCodexPutSyntax[D <: GettableData & SettableData[D]](
val data: D
) extends AnyVal {
implicit class ScassandraUpdateSyntax[D <: GettableData & SettableData[D]](val data: D) extends AnyVal {

def put[A](value: A)(implicit codec: CodecRow[A]): D = {
codec.encode(data, value)
def update[A](value: A)(implicit update: UpdateRow[A]): D = {
update(data, value)
}

def put[A](name: String, value: A)(implicit codec: CodecByName[A]): D = {
codec.encode(data, name, value)
def update[A](name: String, value: A)(implicit update: UpdateByName[A]): D = {
update(data, name, value)
}

def putAt[A](idx: Int, value: A)(implicit codec: CodecByIdx[A]): D = {
codec.encode(data, idx, value)
}

}

implicit class ScassandraCodexTakeSyntax[D <: GettableData](val data: D)
extends AnyVal {

def take[A](implicit codec: CodecRow[A]): A = {
codec.decode(data)
}

def take[A](name: String)(implicit codec: CodecByName[A]): A = {
codec.decode(data, name)
}

def takeAt[A](idx: Int)(implicit codec: CodecByIdx[A]): A = {
codec.decode(data, idx)
def updateAt[A](idx: Int, value: A)(implicit update: UpdateByIdx[A]): D = {
update(data, idx, value)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,15 @@ class SyntaxSpec extends AnyWordSpec with Matchers {
data1.byIdx.get(0) shouldEqual Some("str")
}

"take by name" in {
val data = DataMock(byName = Map(("key", "value")))
data.take[String]("key") shouldEqual "value"
}

"take by idx" in {
val data = DataMock(byIdx = Map((0, "value")))
data.takeAt[String](0) shouldEqual "value"
}

"put by name" in {
"update by name" in {
val data = DataMock()
val data1 = data.put("name", "str")
val data1 = data.update("name", "str")
data1.byName.get("name") shouldEqual Some("str")
}

"put by idx" in {
"update by idx" in {
val data = DataMock()
val data1 = data.putAt(0, "str")
val data1 = data.updateAt(0, "str")
data1.byIdx.get(0) shouldEqual Some("str")
}
}
Expand Down
Loading

0 comments on commit f26a2c0

Please sign in to comment.