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

fix: Revert to old behavior of ValueBinder.toProtoBytes #273

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import com.google.cloud.ByteArray
import com.google.cloud.Date
import com.google.cloud.Timestamp
import com.google.cloud.spanner.Mutation
import com.google.cloud.spanner.ValueBinder
import com.google.protobuf.AbstractMessage
import com.google.protobuf.Message
import com.google.protobuf.ProtocolMessageEnum
import org.wfanet.measurement.common.ProtoReflection
import org.wfanet.measurement.common.identity.ExternalId
import org.wfanet.measurement.common.identity.InternalId

Expand Down Expand Up @@ -121,18 +119,13 @@ fun Mutation.WriteBuilder.set(
}

/** Sets the value that should be bound to the specified column. */
@Deprecated(message = "Use ValueBinder directly")
@Deprecated(message = "Use ValueBinder directly to avoid type ambiguity")
@JvmName("setProtoMessageBytes")
inline fun <reified T : AbstractMessage> Mutation.WriteBuilder.set(
columnValuePair: Pair<String, T?>
): Mutation.WriteBuilder {
val (columnName, value) = columnValuePair
val binder: ValueBinder<Mutation.WriteBuilder> = set(columnName)
return if (value == null) {
binder.to(null, ProtoReflection.getDescriptorForType(T::class))
} else {
binder.to(value)
}
return set(columnName).toProtoBytes(value)
}

/** Sets the JSON value that should be bound to the specified string column. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import com.google.cloud.ByteArray
import com.google.cloud.Date
import com.google.cloud.Timestamp
import com.google.cloud.spanner.Statement
import com.google.cloud.spanner.ValueBinder
import com.google.protobuf.AbstractMessage
import com.google.protobuf.ProtocolMessageEnum
import org.wfanet.measurement.common.ProtoReflection
import org.wfanet.measurement.common.identity.ExternalId
import org.wfanet.measurement.common.identity.InternalId

Expand Down Expand Up @@ -118,18 +116,13 @@ fun Statement.Builder.bind(paramValuePair: Pair<String, ProtocolMessageEnum>): S
}

/** Binds the value that should be bound to the specified param. */
@Deprecated(message = "Use ValueBinder directly")
@Deprecated(message = "Use ValueBinder directly to avoid type ambiguity")
@JvmName("bindProtoMessageBytes")
inline fun <reified T : AbstractMessage> Statement.Builder.bind(
paramValuePair: Pair<String, T?>
): Statement.Builder {
val (paramName, value) = paramValuePair
val binder: ValueBinder<Statement.Builder> = bind(paramName)
return if (value == null) {
binder.to(null, ProtoReflection.getDescriptorForType(T::class))
} else {
binder.to(value)
}
return bind(paramName).toProtoBytes(value)
}

/** Binds the JSON value that should be bound to the specified string param. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ fun StructReader.getBytesAsByteString(column: String): ByteString =
@Deprecated(message = "Use `getProtoMessage` overload which takes in a default message instance")
inline fun <reified T : AbstractMessage> StructReader.getProtoMessage(
column: String,
@Suppress("UNUSED_PARAMETER") // For backwards-compatibility.
parser: Parser<T>,
): T = getProtoMessage(column, ProtoReflection.getDefaultInstance(T::class))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
package org.wfanet.measurement.gcloud.spanner

import com.google.cloud.spanner.ValueBinder
import com.google.protobuf.AbstractMessage
import com.google.protobuf.Message
import com.google.protobuf.ProtocolMessageEnum
import org.wfanet.measurement.common.ProtoReflection
import org.wfanet.measurement.common.identity.ExternalId
import org.wfanet.measurement.common.identity.InternalId
import org.wfanet.measurement.common.numberAsLong
import org.wfanet.measurement.common.toJson
import org.wfanet.measurement.gcloud.common.toGcloudByteArray

/** Binds to an [InternalId] value. */
fun <T> ValueBinder<T>.to(value: InternalId): T = to(value.value)
Expand All @@ -36,52 +35,24 @@ fun <T> ValueBinder<T>.to(value: ExternalId): T = to(value.value)
/** Binds to an [ExternalId] value. */
@JvmName("toNullable") fun <T> ValueBinder<T>.to(value: ExternalId?): T = to(value?.value)

/** Bind to a protobuf message value. */
@Deprecated(
message = "Use `to` overload that takes an `AbstractMessage`",
replaceWith = ReplaceWith("to(value)"),
)
fun <T> ValueBinder<T>.toProtoBytes(value: AbstractMessage): T = to(value)
/** Bind to a protobuf message as `BYTES`. */
fun <T> ValueBinder<T>.toProtoBytes(value: Message?): T = to(value?.toGcloudByteArray())

/** Bind to a protobuf message value. */
@Suppress("DeprecatedCallableAddReplaceWith") // Should use manual replacement to avoid reflection.
@Deprecated(
message =
"Use `to` overload that takes an `AbstractMessage` and a `Descriptors.Descriptor` for " +
"nullable values"
)
@JvmName("toProtoBytesNullable")
inline fun <T, reified P : AbstractMessage> ValueBinder<T>.toProtoBytes(
value: AbstractMessage?
): T {
return if (value == null) {
to(null, ProtoReflection.getDescriptorForType(P::class))
} else {
to(value)
}
}
/** Binds to a protobuf message JSON value as a `STRING`. */
fun <T> ValueBinder<T>.toProtoJson(value: Message?): T = to(value?.toJson())

/** Bind a protobuf [Message] as a JSON string representation. */
fun <T> ValueBinder<T>.toProtoJson(value: Message?): T {
return if (value == null) {
to(null as String?)
} else {
to(value.toJson())
}
}

/** Bind a protobuf enum value as an INT64 value. */
/** Binds to a protobuf enum value as an `INT64`. */
@Deprecated(message = "Use `toInt64`", replaceWith = ReplaceWith("toInt64(value)"))
fun <T> ValueBinder<T>.toProtoEnum(value: ProtocolMessageEnum): T = toInt64(value)

/** Bind a protobuf enum value as an INT64 value. */
/** Binds to a protobuf enum value as an `INT64`. */
fun <T> ValueBinder<T>.toInt64(value: ProtocolMessageEnum): T = to(value.numberAsLong)

/** Binds a collection of protobuf enum values as an ARRAY<INT64> value. */
/** Binds to a collection of protobuf enum values as an `ARRAY<INT64>`. */
@Deprecated(message = "Use `toInt64Array`", replaceWith = ReplaceWith("toInt64Array(values)"))
fun <T> ValueBinder<T>.toProtoEnumArray(values: Iterable<ProtocolMessageEnum>): T =
toInt64Array(values)

/** Binds a collection of protobuf enum values as an ARRAY<INT64> value. */
/** Binds to a collection of protobuf enum values as an `ARRAY<INT64>`. */
fun <T> ValueBinder<T>.toInt64Array(values: Iterable<ProtocolMessageEnum>): T =
toInt64Array(values.map(ProtocolMessageEnum::numberAsLong))
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.wfanet.measurement.common.numberAsLong
import org.wfanet.measurement.common.toJson
import org.wfanet.measurement.gcloud.common.toGcloudByteArray

@RunWith(JUnit4::class)
class MutationsTest {
Expand All @@ -46,7 +47,8 @@ class MutationsTest {
set("TimestampColumn" to timestamp)
set("EnumColumn").to(cardinality)
set("EnumInt64Column").toInt64(cardinality)
set("ProtoBytesColumn").to(field)
set("ProtoColumn").to(field)
set("ProtoBytesColumn").toProtoBytes(field)
setJson("ProtoJsonColumn" to field)
}

Expand All @@ -68,8 +70,10 @@ class MutationsTest {
Value.protoEnum(cardinality),
"EnumInt64Column",
Value.int64(cardinality.numberAsLong),
"ProtoBytesColumn",
"ProtoColumn",
Value.protoMessage(field),
"ProtoBytesColumn",
Value.bytes(field.toGcloudByteArray()),
"ProtoJsonColumn",
Value.string(field.toJson()),
)
Expand Down
Loading