Skip to content
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
2 changes: 2 additions & 0 deletions firebase-dataconnect/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
([#7910](https://github.com/firebase/firebase-android-sdk/pull/7910))
- [changed] Internal refactor to use immutable byte arrays.
([#7957](https://github.com/firebase/firebase-android-sdk/pull/7957))
- [changed] Internal refactor for calculating debug logging strings.
([#8024](https://github.com/firebase/firebase-android-sdk/pull/8024))

# 17.2.0

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.firebase.dataconnect.util

import com.google.firebase.dataconnect.DataConnectPathSegment
import com.google.firebase.dataconnect.core.DataConnectOperationFailureResponseImpl
import com.google.firebase.dataconnect.util.ProtoUtil.toCompactString
import com.google.protobuf.ListValue
import com.google.protobuf.Value
import google.firebase.dataconnect.proto.GraphqlError

internal object DeserializeUtils {

fun GraphqlError.toErrorInfoImpl() =
DataConnectOperationFailureResponseImpl.ErrorInfoImpl(
message = message,
path = path.toPathSegment(),
)

private fun ListValue.toPathSegment() =
valuesList.map {
when (it.kindCase) {
Value.KindCase.STRING_VALUE -> DataConnectPathSegment.Field(it.stringValue)
Value.KindCase.NUMBER_VALUE -> DataConnectPathSegment.ListIndex(it.numberValue.toInt())
// The other cases are expected to never occur; however, implement some logic for them
// to avoid things like throwing exceptions in those cases.
else -> DataConnectPathSegment.Field(it.toCompactString())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package com.google.firebase.dataconnect.util

import com.google.firebase.dataconnect.DataConnectPath
import com.google.firebase.dataconnect.DataConnectPathSegment
import com.google.firebase.dataconnect.core.DataConnectGrpcClientGlobals.toErrorInfoImpl
import com.google.firebase.dataconnect.toPathString
import com.google.firebase.dataconnect.util.DeserializeUtils.toErrorInfoImpl
import com.google.firebase.dataconnect.util.ProtoUtil.nullProtoValue
import com.google.firebase.dataconnect.util.ProtoUtil.toValueProto
import com.google.protobuf.Duration
Expand All @@ -37,6 +37,8 @@ import google.firebase.dataconnect.proto.ExecuteMutationRequest
import google.firebase.dataconnect.proto.ExecuteMutationResponse
import google.firebase.dataconnect.proto.ExecuteQueryRequest
import google.firebase.dataconnect.proto.ExecuteQueryResponse
import google.firebase.dataconnect.proto.GraphqlError
import google.firebase.dataconnect.proto.GraphqlResponseExtensions
import google.firebase.dataconnect.proto.ServiceInfo
import java.io.BufferedWriter
import java.io.CharArrayWriter
Expand Down Expand Up @@ -223,32 +225,39 @@ internal object ProtoUtil {

fun ExecuteQueryResponse.toStructProto(): Struct = buildStructProto {
if (hasData()) put("data", data)
putList("errors") { errorsList.forEach { add(it.toErrorInfoImpl().toString()) } }

if (errorsCount > 0) {
put("errors", errorsList)
}
if (hasExtensions()) {
putStruct("extensions") {
putList("data_connect") {
extensions.dataConnectList.forEach { dataConnectProperties ->
addStruct {
if (dataConnectProperties.hasPath()) {
put("path", dataConnectProperties.path.toDataConnectPath().toPathString())
}
dataConnectProperties.entityId
.takeIf { it.isNotEmpty() }
?.let { put("entity_id", it) }
dataConnectProperties.entityIdsList
.takeIf { it.isNotEmpty() }
?.let { putList("entity_ids") { it.forEach(::add) } }
if (dataConnectProperties.hasMaxAge()) {
put("max_age", dataConnectProperties.maxAge.toHumanFriendlyString())
}
put("extensions", extensions)
}
}

fun StructProtoBuilder.put(key: String, extensions: GraphqlResponseExtensions) {
putStruct(key) {
putList("data_connect") {
extensions.dataConnectList.forEach { dataConnectProperties ->
addStruct {
if (dataConnectProperties.hasPath()) {
put("path", dataConnectProperties.path.toDataConnectPath().toPathString())
}
dataConnectProperties.entityId.takeIf { it.isNotEmpty() }?.let { put("entity_id", it) }
dataConnectProperties.entityIdsList
.takeIf { it.isNotEmpty() }
?.let { putList("entity_ids") { it.forEach(::add) } }
if (dataConnectProperties.hasMaxAge()) {
put("max_age", dataConnectProperties.maxAge.toHumanFriendlyString())
}
}
}
}
}
}

fun StructProtoBuilder.put(key: String, errors: Collection<GraphqlError>) {
putList(key) { errors.forEach { add(it.toErrorInfoImpl().toString()) } }
}

fun Duration.toHumanFriendlyString(): String = humanFriendlyStringForDuration(seconds, nanos)

private fun humanFriendlyStringForDuration(seconds: Long, nanos: Int): String = buildString {
Expand Down
Loading