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

Ref: use explicit api & consistency #63

Merged
merged 4 commits into from
Mar 30, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Improve Objc/Swift experience with @HiddenFromObjc ([#62](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/62))

### Fixes

- ref: use explicit api & add code consistency ([#63](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/63))

## 0.0.3

### Fixes
Expand Down
2 changes: 2 additions & 0 deletions sentry-kotlin-multiplatform/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ android {
}

kotlin {
explicitApi()

android {
publishLibraryVariants("release")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ internal actual fun initSentry(context: Context?, configuration: OptionsConfigur
}
}

actual typealias Context = Context
public actual typealias Context = Context
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,47 @@ package io.sentry.kotlin.multiplatform
import io.sentry.kotlin.multiplatform.extensions.toByteArray
import io.sentry.kotlin.multiplatform.extensions.toNSData

actual class Attachment : IAttachment {
public actual class Attachment {

lateinit var cocoaAttachment: CocoaAttachment
internal lateinit var cocoaAttachment: CocoaAttachment

actual override val filename: String
public actual val filename: String
get() = cocoaAttachment.filename

actual override val pathname: String?
public actual val pathname: String?
get() = cocoaAttachment.path

actual override val bytes: ByteArray?
public actual val bytes: ByteArray?
get() = cocoaAttachment.data?.toByteArray()

actual override val contentType: String?
public actual val contentType: String?
get() = cocoaAttachment.contentType

actual companion object {
actual fun fromScreenshot(screenshotBytes: ByteArray): Attachment {
val data = screenshotBytes.toNSData()
public actual companion object {
public actual fun fromScreenshot(screenshotBytes: ByteArray): Attachment {
return Attachment(screenshotBytes, "screenshot.png", "image/png")
}
}

actual constructor(pathname: String) {
public actual constructor(pathname: String) {
cocoaAttachment = CocoaAttachment(pathname)
}

actual constructor(pathname: String, filename: String) {
public actual constructor(pathname: String, filename: String) {
cocoaAttachment = CocoaAttachment(pathname, filename)
}

actual constructor(pathname: String, filename: String, contentType: String?) {
public actual constructor(pathname: String, filename: String, contentType: String?) {
contentType?.let { cocoaAttachment = CocoaAttachment(pathname, filename, it) } ?: run {
cocoaAttachment = CocoaAttachment(pathname, filename)
}
}

actual constructor(bytes: ByteArray, filename: String) {
public actual constructor(bytes: ByteArray, filename: String) {
cocoaAttachment = CocoaAttachment(bytes.toNSData(), filename)
}

actual constructor(bytes: ByteArray, filename: String, contentType: String?) {
public actual constructor(bytes: ByteArray, filename: String, contentType: String?) {
contentType?.let { cocoaAttachment = CocoaAttachment(bytes.toNSData(), filename, it) } ?: run {
cocoaAttachment = CocoaAttachment(bytes.toNSData(), filename)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.protocol.User
import Scope.Sentry.SentryScope as PrivateCocoaScope

internal class ScopeCocoaImpl(private val scope: CocoaScope) : ISentryScope {
internal class CocoaScopeProvider(private val scope: CocoaScope) : ScopeProvider {

/*
This bridge exposes private Cocoa SDK API to fetch internal properties such as user, level, etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.sentry.kotlin.multiplatform.protocol.UserFeedback
import platform.Foundation.NSError
import platform.Foundation.NSException

actual abstract class Context
public actual abstract class Context

internal expect fun initSentry(configuration: OptionsConfiguration)

Expand Down Expand Up @@ -68,21 +68,21 @@ internal actual object SentryBridge {

private fun configureScopeCallback(scopeCallback: ScopeCallback): (CocoaScope?) -> Unit {
return { cocoaScope ->
val cocoaScopeImpl = cocoaScope?.let {
ScopeCocoaImpl(it)
val cocoaScopeProvider = cocoaScope?.let {
CocoaScopeProvider(it)
}
cocoaScopeImpl?.let {
cocoaScopeProvider?.let {
val scope = Scope(it)
scopeCallback.invoke(scope)
}
}
}
}

fun Sentry.captureError(error: NSError) {
public fun captureError(error: NSError) {
SentrySDK.captureError(error)
}

fun Sentry.captureException(exception: NSException) {
public fun captureException(exception: NSException) {
SentrySDK.captureException(exception)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,24 @@ package io.sentry.kotlin.multiplatform.extensions

import io.sentry.kotlin.multiplatform.CocoaBreadcrumb
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.protocol.ISentryBreadcrumb

internal fun ISentryBreadcrumb.toCocoaBreadcrumb(): CocoaBreadcrumb {
val outerScope = this
return CocoaBreadcrumb().apply {
setMessage(outerScope.message)
setType(outerScope.type)
outerScope.category?.let { setCategory(it) }
outerScope.level?.let { setLevel(it.toCocoaSentryLevel()) }
setData(outerScope.getData()?.toMap())
}
internal fun Breadcrumb.toCocoaBreadcrumb() = CocoaBreadcrumb().apply {
val scope = this@toCocoaBreadcrumb
setMessage(scope.message)
setType(scope.type)
scope.category?.let { setCategory(it) }
scope.level?.let { setLevel(it.toCocoaSentryLevel()) }
setData(scope.getData()?.toMap())
}

internal fun CocoaBreadcrumb.toKmpBreadcrumb(): Breadcrumb {
return Breadcrumb().apply {
val funScope = this@toKmpBreadcrumb
message = funScope.message
type = funScope.type
category = funScope.category
val map = funScope.data as? Map<String, Any>
map?.let {
this.setData(it.toMutableMap())
}
level = funScope.level?.toKmpSentryLevel()
internal fun CocoaBreadcrumb.toKmpBreadcrumb() = Breadcrumb().apply {
val scope = this@toKmpBreadcrumb
message = scope.message
type = scope.type
category = scope.category
val map = scope.data as? Map<String, Any>
map?.let {
this.setData(it.toMutableMap())
}
level = scope.level?.toKmpSentryLevel()
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import platform.Foundation.allKeys
import platform.Foundation.create
import platform.posix.memcpy

fun <K, V> NSMutableDictionary.toMutableMap(): MutableMap<K, V> {
internal fun <K, V> NSMutableDictionary.toMutableMap(): MutableMap<K, V> {
val keys = this.allKeys
val map = mutableMapOf<K, V>()
for (key in keys) {
Expand All @@ -20,13 +20,13 @@ fun <K, V> NSMutableDictionary.toMutableMap(): MutableMap<K, V> {
return map
}

fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply {
internal fun NSData.toByteArray(): ByteArray = ByteArray(this@toByteArray.length.toInt()).apply {
usePinned {
memcpy(it.addressOf(0), this@toByteArray.bytes, this@toByteArray.length)
}
}

fun ByteArray.toNSData(): NSData = memScoped {
internal fun ByteArray.toNSData(): NSData = memScoped {
NSData.create(
bytes = allocArrayOf(this@toNSData),
length = this@toNSData.size.toULong().convert()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package io.sentry.kotlin.multiplatform.extensions
import io.sentry.kotlin.multiplatform.CocoaUser
import io.sentry.kotlin.multiplatform.protocol.User

internal fun User.toCocoaUser(): CocoaUser {
val outerScope = this
return CocoaUser().apply {
userId = outerScope.id
username = outerScope.username
email = outerScope.email
ipAddress = outerScope.ipAddress
}
internal fun User.toCocoaUser() = CocoaUser().apply {
val scope = this@toCocoaUser
userId = scope.id
username = scope.username
email = scope.email
ipAddress = scope.ipAddress
}

internal fun CocoaUser.toKmpUser(): User {
val outerScope = this
return User().apply {
id = outerScope.userId.toString()
username = outerScope.username.toString()
email = outerScope.email.toString()
ipAddress = outerScope.ipAddress.toString()
setData(outerScope.data)
}
internal fun CocoaUser.toKmpUser() = User().apply {
val scope = this@toKmpUser
id = scope.userId.toString()
username = scope.username.toString()
email = scope.email.toString()
ipAddress = scope.ipAddress.toString()
setData(scope.data)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.sentry.kotlin.multiplatform.CocoaSentryId
import io.sentry.kotlin.multiplatform.CocoaUserFeedback
import io.sentry.kotlin.multiplatform.protocol.UserFeedback

fun UserFeedback.toCocoaUserFeedback(): CocoaUserFeedback {
internal fun UserFeedback.toCocoaUserFeedback(): CocoaUserFeedback {
val sentryId = CocoaSentryId(sentryId.toString())
return CocoaUserFeedback(sentryId).apply {
comments = this@toCocoaUserFeedback.comments.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import kotlin.reflect.KClass
* If [appendCausedBy] is `true` then the name, message and stack trace
* of the [causes][Throwable.cause] will be appended, else causes are ignored.
*/
fun Throwable.asNSException(appendCausedBy: Boolean = false): NSException {
public fun Throwable.asNSException(appendCausedBy: Boolean = false): NSException {
val returnAddresses = getFilteredStackTraceAddresses().let { addresses ->
if (!appendCausedBy) return@let addresses
addresses.toMutableList().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io.sentry.kotlin.multiplatform.nsexception
* The first element will be the cause, the second the cause of the cause, etc.
* This function stops once a reference cycles is detected.
*/
val Throwable.causes: List<Throwable> get() = buildList {
internal val Throwable.causes: List<Throwable> get() = buildList {
val causes = mutableSetOf<Throwable>()
var cause = cause
while (cause != null && cause !in causes) {
Expand All @@ -36,7 +36,7 @@ val Throwable.causes: List<Throwable> get() = buildList {
* @param commonAddresses a list of addresses used to drop the last common addresses.
* @see getStackTraceAddresses
*/
fun Throwable.getFilteredStackTraceAddresses(
internal fun Throwable.getFilteredStackTraceAddresses(
keepLastInit: Boolean = false,
commonAddresses: List<Long> = emptyList()
): List<Long> = getStackTraceAddresses().dropInitAddresses(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package io.sentry.kotlin.multiplatform.protocol

import io.sentry.kotlin.multiplatform.CocoaSentryId

actual data class SentryId actual constructor(val sentryIdString: String) : ISentryId {
public actual data class SentryId actual constructor(val sentryIdString: String) {

actual companion object {
actual val EMPTY_ID: SentryId = SentryId("")
public actual companion object {
public actual val EMPTY_ID: SentryId = SentryId("")
}

private var cocoaSentryId: CocoaSentryId? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import cocoapods.Sentry.SentryScope as CocoaScope
actual abstract class BaseSentryScopeTest {
actual fun initializeScope(): Scope {
val cocoaScope = CocoaScope()
val scopeCocoaImpl = ScopeCocoaImpl(cocoaScope)
return Scope(scopeCocoaImpl)
val cocoaScopeProvider = CocoaScopeProvider(cocoaScope)
return Scope(cocoaScopeProvider)
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package io.sentry.kotlin.multiplatform

actual class Attachment : IAttachment {
public actual class Attachment {

var jvmAttachment: JvmAttachment
internal var jvmAttachment: JvmAttachment

actual override val filename: String
public actual val filename: String
get() = jvmAttachment.filename

actual override val pathname: String?
public actual val pathname: String?
get() = jvmAttachment.pathname

actual override val bytes: ByteArray?
public actual val bytes: ByteArray?
get() = jvmAttachment.bytes

actual override val contentType: String?
public actual val contentType: String?
get() = jvmAttachment.contentType

actual companion object {
actual fun fromScreenshot(screenshotBytes: ByteArray): Attachment {
public actual companion object {
public actual fun fromScreenshot(screenshotBytes: ByteArray): Attachment {
return Attachment(screenshotBytes, "screenshot.png", "image/png")
}
}

actual constructor(pathname: String) {
public actual constructor(pathname: String) {
jvmAttachment = JvmAttachment(pathname)
}

actual constructor(pathname: String, filename: String) {
public actual constructor(pathname: String, filename: String) {
jvmAttachment = JvmAttachment(pathname, filename)
}

actual constructor(pathname: String, filename: String, contentType: String?) {
public actual constructor(pathname: String, filename: String, contentType: String?) {
jvmAttachment = JvmAttachment(pathname, filename, contentType)
}

actual constructor(bytes: ByteArray, filename: String) {
public actual constructor(bytes: ByteArray, filename: String) {
jvmAttachment = JvmAttachment(bytes, filename)
}

actual constructor(bytes: ByteArray, filename: String, contentType: String?) {
public actual constructor(bytes: ByteArray, filename: String, contentType: String?) {
jvmAttachment = JvmAttachment(bytes, filename, contentType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.sentry.kotlin.multiplatform.extensions.toKmpUser
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.protocol.User

internal class ScopeJvmImpl(private val scope: JvmScope) : ISentryScope {
internal class JvmScopeProvider(private val scope: JvmScope) : ScopeProvider {

override var level: SentryLevel?
set(value) {
Expand Down
Loading