Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CodecXXX capable of updating map, list, set types in statement instead of replacing it #263

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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] {
dfakhritdinov marked this conversation as resolved.
Show resolved Hide resolved

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
Loading