Skip to content

Remove Desugaring Requirement By Removing Instant.now() #154

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 6 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -6,6 +6,7 @@ import com.segment.analytics.kotlin.core.TrackEvent
import com.segment.analytics.kotlin.core.emptyJsonObject
import com.segment.analytics.kotlin.core.utilities.EncodeDefaultsJson
import com.segment.analytics.kotlin.core.utilities.EventsFileManager
import com.segment.analytics.kotlin.core.utilities.dateTimeNowString
import io.mockk.every
import io.mockk.mockkStatic
import kotlinx.coroutines.test.runTest
Expand All @@ -32,6 +33,8 @@ class EventsFileTests {

init {
mockkStatic(Instant::class)
mockkStatic(::dateTimeNowString)
every { dateTimeNowString() } returns Date(0).toInstant().toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to replace toInstant too? looks like it's returning an Instant value. would that require desugaring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in our tests. I let all of those keep the use of the Instant classes. Instant was only removed from code that runs on the device.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. I wonder if it's still available after we removed all the desugaring setup and the dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It is still available because the test run locally using java 11. I have updated the code to remove desugaring from the android build.gradle and re-run the test and they still all pass.

every { Instant.now() } returns Date(0).toInstant()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ open class Analytics protected constructor(
Analytics.segmentLog(
"Caught Exception in Analytics Scope: ${t}"
)
t.printStackTrace()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm....No. I will add that and probably more like it in a follow up PR. It's a nice to have.

}
override val analyticsScope = CoroutineScope(SupervisorJob() + exceptionHandler)
override val analyticsDispatcher : CloseableCoroutineDispatcher =
Expand Down
21 changes: 2 additions & 19 deletions core/src/main/java/com/segment/analytics/kotlin/core/Events.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package com.segment.analytics.kotlin.core

import com.segment.analytics.kotlin.core.utilities.dateTimeNowString
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
import sovran.kotlin.Store
import java.time.Instant
import java.util.*

typealias AnalyticsContext = JsonObject
Expand All @@ -21,18 +16,6 @@ typealias Traits = JsonObject
val emptyJsonObject = JsonObject(emptyMap())
val emptyJsonArray = JsonArray(emptyList())

class DateSerializer : KSerializer<Instant> {
override val descriptor = PrimitiveSerialDescriptor("Instant", PrimitiveKind.STRING)

override fun deserialize(decoder: Decoder): Instant {
return Instant.parse(decoder.decodeString())
}

override fun serialize(encoder: Encoder, value: Instant) {
encoder.encodeString(value.toString())
}
}

@Serializable
data class DestinationMetadata(
var bundled: List<String>? = emptyList(),
Expand Down Expand Up @@ -97,7 +80,7 @@ sealed class BaseEvent {
}

internal fun applyBaseData() {
this.timestamp = Instant.now().toString()
this.timestamp = dateTimeNowString()
this.context = emptyJsonObject
this.messageId = UUID.randomUUID().toString()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.segment.analytics.kotlin.core.utilities

import java.text.SimpleDateFormat
import java.util.*

/**
* This function is a replacement for Instant.now().toString(). It produces strings in a
* compatible format.
*
* Ex:
* Instant.now(): 2023-04-19T04:03:46.880969Z
* dateTimeNowString(): 2023-04-19T04:03:46.880Z
*/
fun dateTimeNowString(): String {
val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'Szzz")
val utc = TimeZone.getTimeZone("UTC");
sdf.timeZone = utc;
return sdf.format(Date()).replace("UTC", "Z")
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.segment.analytics.kotlin.core.utilities
import kotlinx.coroutines.sync.Semaphore
import java.io.File
import java.io.FileOutputStream
import java.time.Instant

/**
* Responsible for storing events in a batch payload style
Expand Down Expand Up @@ -134,7 +133,9 @@ class EventsFileManager(
return
}
// close events array and batch object
val contents = """],"sentAt":"${Instant.now()}","writeKey":"$writeKey"}"""


val contents = """],"sentAt":"${dateTimeNowString()}","writeKey":"$writeKey"}"""
writeToFile(contents.toByteArray(), file)
file.renameTo(File(directory, file.nameWithoutExtension))
os?.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.segment.analytics.kotlin.core
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
import com.segment.analytics.kotlin.core.utilities.dateTimeNowString
import com.segment.analytics.kotlin.core.utils.StubPlugin
import com.segment.analytics.kotlin.core.utils.TestRunPlugin
import com.segment.analytics.kotlin.core.utils.clearPersistentStorage
Expand Down Expand Up @@ -56,6 +57,8 @@ class AnalyticsTests {
init {
mockkStatic(Instant::class)
every { Instant.now() } returns Date(0).toInstant()
mockkStatic(::dateTimeNowString)
every { dateTimeNowString() } returns Date(0).toInstant().toString()
mockkStatic(UUID::class)
every { UUID.randomUUID().toString() } returns "qwerty-qwerty-123"
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.segment.analytics.kotlin.core.emptyJsonObject
import com.segment.analytics.kotlin.core.platform.DestinationPlugin
import com.segment.analytics.kotlin.core.platform.Plugin
import com.segment.analytics.kotlin.core.platform.plugins.ContextPlugin
import com.segment.analytics.kotlin.core.utilities.dateTimeNowString
import com.segment.analytics.kotlin.core.utils.StubPlugin
import com.segment.analytics.kotlin.core.utils.TestRunPlugin
import com.segment.analytics.kotlin.core.utils.mockHTTPClient
Expand Down Expand Up @@ -62,6 +63,8 @@ internal class JavaAnalyticsTest {
init {
mockkStatic(Instant::class)
every { Instant.now() } returns Date(0).toInstant()
mockkStatic(::dateTimeNowString)
every { dateTimeNowString() } returns Date(0).toInstant().toString()
mockkStatic(UUID::class)
every { UUID.randomUUID().toString() } returns "qwerty-qwerty-123"
mockHTTPClient()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ internal class EventsFileManagerTest{
init {
mockkStatic(Instant::class)
every { Instant.now() } returns Date(0).toInstant()

mockkStatic(::dateTimeNowString)
every { dateTimeNowString() } returns Date(0).toInstant().toString()
}

@BeforeEach
Expand Down