-
Notifications
You must be signed in to change notification settings - Fork 625
Add JSON encoder for SessionEvents #4778
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
67 changes: 67 additions & 0 deletions
67
firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionEvents.kt
This file contains hidden or 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,67 @@ | ||
/* | ||
* Copyright 2023 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.sessions | ||
|
||
import com.google.firebase.encoders.DataEncoder | ||
import com.google.firebase.encoders.FieldDescriptor | ||
import com.google.firebase.encoders.ObjectEncoderContext | ||
import com.google.firebase.encoders.json.JsonDataEncoderBuilder | ||
|
||
/** | ||
* Contains functions for [SessionEvent]s. | ||
* | ||
* @hide | ||
*/ | ||
internal object SessionEvents { | ||
/** JSON [DataEncoder] for [SessionEvent]s. */ | ||
// TODO(mrober): Replace with firebase-encoders-processor when it can encode Kotlin data classes. | ||
internal val SESSION_EVENT_ENCODER: DataEncoder = | ||
JsonDataEncoderBuilder() | ||
.configureWith { | ||
visumickey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it.registerEncoder(SessionEvent::class.java) { | ||
sessionEvent: SessionEvent, | ||
ctx: ObjectEncoderContext -> | ||
run { | ||
ctx.add(FieldDescriptor.of("event_type"), sessionEvent.eventType) | ||
ctx.add(FieldDescriptor.of("session_data"), sessionEvent.sessionData) | ||
} | ||
} | ||
|
||
it.registerEncoder(SessionInfo::class.java) { | ||
sessionInfo: SessionInfo, | ||
ctx: ObjectEncoderContext -> | ||
run { | ||
ctx.add(FieldDescriptor.of("session_id"), sessionInfo.sessionId) | ||
ctx.add(FieldDescriptor.of("first_session_id"), sessionInfo.firstSessionId) | ||
ctx.add(FieldDescriptor.of("session_index"), sessionInfo.sessionIndex) | ||
} | ||
} | ||
} | ||
.build() | ||
|
||
/** Construct a Session Start event */ | ||
fun startSession(sessionDetails: SessionDetails) = | ||
SessionEvent( | ||
eventType = EventType.SESSION_START, | ||
sessionData = | ||
SessionInfo( | ||
sessionDetails.sessionId, | ||
sessionDetails.firstSessionId, | ||
sessionDetails.sessionIndex, | ||
), | ||
) | ||
} |
105 changes: 105 additions & 0 deletions
105
firebase-sessions/src/test/kotlin/com/google/firebase/sessions/SessionEventEncoderTest.kt
This file contains hidden or 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,105 @@ | ||
/* | ||
* Copyright 2023 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.sessions | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth.assertThat | ||
import com.google.firebase.sessions.SessionEvents.SESSION_EVENT_ENCODER | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class SessionEventEncoderTest { | ||
mrober marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Test | ||
fun sessionEvent_encodesToJson() { | ||
val sessionEvent = | ||
SessionEvent( | ||
eventType = EventType.SESSION_START, | ||
sessionData = | ||
SessionInfo( | ||
sessionId = "id", | ||
firstSessionId = "first", | ||
sessionIndex = 9, | ||
), | ||
) | ||
|
||
val json = SESSION_EVENT_ENCODER.encode(sessionEvent) | ||
|
||
assertThat(json) | ||
.isEqualTo( | ||
""" | ||
{ | ||
"event_type":1, | ||
"session_data":{ | ||
"session_id":"id", | ||
"first_session_id":"first", | ||
"session_index":9 | ||
} | ||
} | ||
""" | ||
.lines() | ||
.joinToString("") { it.trim() } | ||
) | ||
} | ||
|
||
@Test | ||
fun sessionEvent_emptyValues_encodesToJson() { | ||
val sessionEvent = | ||
SessionEvent( | ||
eventType = EventType.EVENT_TYPE_UNKNOWN, | ||
sessionData = | ||
SessionInfo( | ||
sessionId = "", | ||
firstSessionId = "", | ||
sessionIndex = 0, | ||
), | ||
) | ||
|
||
val json = SESSION_EVENT_ENCODER.encode(sessionEvent) | ||
|
||
assertThat(json) | ||
.isEqualTo( | ||
""" | ||
{ | ||
"event_type":0, | ||
"session_data":{ | ||
"session_id":"", | ||
"first_session_id":"", | ||
"session_index":0 | ||
} | ||
} | ||
""" | ||
.lines() | ||
.joinToString("") { it.trim() } | ||
) | ||
} | ||
|
||
@Test | ||
fun eventType_numberedEnum_encodesToJson() { | ||
val json = | ||
SESSION_EVENT_ENCODER.encode( | ||
arrayOf( | ||
EventType.SESSION_START, | ||
EventType.EVENT_TYPE_UNKNOWN, | ||
EventType.SESSION_START, | ||
EventType.SESSION_START | ||
) | ||
) | ||
|
||
assertThat(json).isEqualTo("[1,0,1,1]") | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.