Skip to content

Commit 2dd9beb

Browse files
authored
Migrate Functions Kotlin to strict API (#6386)
1 parent c50a9b5 commit 2dd9beb

12 files changed

+91
-73
lines changed

firebase-functions/api.txt

-4
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,9 @@ package com.google.firebase.functions {
8181
method public boolean getLimitedUseAppCheckTokens();
8282
method public long getTimeout();
8383
method public void setTimeout(long timeout, @NonNull java.util.concurrent.TimeUnit units);
84-
field @NonNull public static final com.google.firebase.functions.HttpsCallOptions.Companion Companion;
8584
field public final boolean limitedUseAppCheckTokens;
8685
}
8786

88-
public static final class HttpsCallOptions.Companion {
89-
}
90-
9187
public final class HttpsCallableOptions {
9288
method public boolean getLimitedUseAppCheckTokens();
9389
field public final boolean limitedUseAppCheckTokens;

firebase-functions/firebase-functions.gradle.kts

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2+
13
// Copyright 2022 Google LLC
24
//
35
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -55,6 +57,20 @@ android {
5557
testOptions.unitTests.isIncludeAndroidResources = true
5658
}
5759

60+
// Enable Kotlin "Explicit API Mode". This causes the Kotlin compiler to fail if any
61+
// classes, methods, or properties have implicit `public` visibility. This check helps
62+
// avoid accidentally leaking elements into the public API, requiring that any public
63+
// element be explicitly declared as `public`.
64+
// https://github.com/Kotlin/KEEP/blob/master/proposals/explicit-api-mode.md
65+
// https://chao2zhang.medium.com/explicit-api-mode-for-kotlin-on-android-b8264fdd76d1
66+
tasks.withType<KotlinCompile>().all {
67+
if (!name.contains("test", ignoreCase = true)) {
68+
if (!kotlinOptions.freeCompilerArgs.contains("-Xexplicit-api=strict")) {
69+
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
70+
}
71+
}
72+
}
73+
5874
dependencies {
5975
javadocClasspath("org.codehaus.mojo:animal-sniffer-annotations:1.21")
6076
javadocClasspath(libs.autovalue.annotations)

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.kt

+15-12
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import org.json.JSONException
4747
import org.json.JSONObject
4848

4949
/** FirebaseFunctions lets you call Cloud Functions for Firebase. */
50-
class FirebaseFunctions
50+
public class FirebaseFunctions
5151
@AssistedInject
5252
internal constructor(
5353
context: Context,
@@ -105,22 +105,25 @@ internal constructor(
105105
}
106106

107107
/** Returns a reference to the callable HTTPS trigger with the given name. */
108-
fun getHttpsCallable(name: String): HttpsCallableReference {
108+
public fun getHttpsCallable(name: String): HttpsCallableReference {
109109
return HttpsCallableReference(this, name, HttpsCallOptions())
110110
}
111111

112112
/** Returns a reference to the callable HTTPS trigger with the provided URL. */
113-
fun getHttpsCallableFromUrl(url: URL): HttpsCallableReference {
113+
public fun getHttpsCallableFromUrl(url: URL): HttpsCallableReference {
114114
return HttpsCallableReference(this, url, HttpsCallOptions())
115115
}
116116

117117
/** Returns a reference to the callable HTTPS trigger with the given name and call options. */
118-
fun getHttpsCallable(name: String, options: HttpsCallableOptions): HttpsCallableReference {
118+
public fun getHttpsCallable(name: String, options: HttpsCallableOptions): HttpsCallableReference {
119119
return HttpsCallableReference(this, name, HttpsCallOptions(options))
120120
}
121121

122122
/** Returns a reference to the callable HTTPS trigger with the provided URL and call options. */
123-
fun getHttpsCallableFromUrl(url: URL, options: HttpsCallableOptions): HttpsCallableReference {
123+
public fun getHttpsCallableFromUrl(
124+
url: URL,
125+
options: HttpsCallableOptions
126+
): HttpsCallableReference {
124127
return HttpsCallableReference(this, url, HttpsCallOptions(options))
125128
}
126129

@@ -149,7 +152,7 @@ internal constructor(
149152
}
150153

151154
@Deprecated("Use {@link #useEmulator(String, int)} to connect to the emulator. ")
152-
fun useFunctionsEmulator(origin: String) {
155+
public fun useFunctionsEmulator(origin: String) {
153156
Preconditions.checkNotNull(origin, "origin cannot be null")
154157
urlFormat = "$origin/%2\$s/%1\$s/%3\$s"
155158
}
@@ -162,7 +165,7 @@ internal constructor(
162165
* @param host the emulator host (for example, 10.0.2.2)
163166
* @param port the emulator port (for example, 5001)
164167
*/
165-
fun useEmulator(host: String, port: Int) {
168+
public fun useEmulator(host: String, port: Int) {
166169
emulatorSettings = EmulatedServiceSettings(host, port)
167170
}
168171

@@ -318,7 +321,7 @@ internal constructor(
318321
return tcs.task
319322
}
320323

321-
companion object {
324+
public companion object {
322325
/** A task that will be resolved once ProviderInstaller has installed what it needs to. */
323326
private val providerInstalled = TaskCompletionSource<Void>()
324327

@@ -370,7 +373,7 @@ internal constructor(
370373
* `"us-central1"` or `"https://mydomain.com"`.
371374
*/
372375
@JvmStatic
373-
fun getInstance(app: FirebaseApp, regionOrCustomDomain: String): FirebaseFunctions {
376+
public fun getInstance(app: FirebaseApp, regionOrCustomDomain: String): FirebaseFunctions {
374377
Preconditions.checkNotNull(app, "You must call FirebaseApp.initializeApp first.")
375378
Preconditions.checkNotNull(regionOrCustomDomain)
376379
val component = app.get(FunctionsMultiResourceComponent::class.java)
@@ -384,7 +387,7 @@ internal constructor(
384387
* @param app The app for the Firebase project.
385388
*/
386389
@JvmStatic
387-
fun getInstance(app: FirebaseApp): FirebaseFunctions {
390+
public fun getInstance(app: FirebaseApp): FirebaseFunctions {
388391
return getInstance(app, "us-central1")
389392
}
390393

@@ -395,13 +398,13 @@ internal constructor(
395398
* `"us-central1"` or `"https://mydomain.com"`.
396399
*/
397400
@JvmStatic
398-
fun getInstance(regionOrCustomDomain: String): FirebaseFunctions {
401+
public fun getInstance(regionOrCustomDomain: String): FirebaseFunctions {
399402
return getInstance(FirebaseApp.getInstance(), regionOrCustomDomain)
400403
}
401404

402405
/** Creates a Cloud Functions client with the default app. */
403406
@JvmStatic
404-
fun getInstance(): FirebaseFunctions {
407+
public fun getInstance(): FirebaseFunctions {
405408
return getInstance(FirebaseApp.getInstance(), "us-central1")
406409
}
407410
}

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctionsException.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ import org.json.JSONObject
2121
// TODO: This is a copy of FirebaseFirestoreException.
2222
// We should investigate whether we can at least share the Code enum.
2323
/** The class for all Exceptions thrown by FirebaseFunctions. */
24-
class FirebaseFunctionsException : FirebaseException {
24+
public class FirebaseFunctionsException : FirebaseException {
2525
/**
2626
* The set of error status codes that can be returned from a Callable HTTPS tigger. These are the
2727
* canonical error codes for Google APIs, as documented here:
2828
* https://github.com/googleapis/googleapis/blob/master/google/rpc/code.proto#L26
2929
*/
30-
enum class Code(private val value: Int) {
30+
public enum class Code(private val value: Int) {
3131
/**
3232
* The operation completed successfully. FirebaseFunctionsException will never have a status of
3333
* OK.
@@ -105,7 +105,7 @@ class FirebaseFunctionsException : FirebaseException {
105105
/** The request does not have valid authentication credentials for the operation. */
106106
UNAUTHENTICATED(16);
107107

108-
companion object {
108+
public companion object {
109109
// Create the canonical list of Status instances indexed by their code values.
110110
private val STATUS_LIST = buildStatusList()
111111
private fun buildStatusList(): SparseArray<Code> {
@@ -121,7 +121,7 @@ class FirebaseFunctionsException : FirebaseException {
121121
}
122122

123123
@JvmStatic
124-
fun fromValue(value: Int): Code {
124+
public fun fromValue(value: Int): Code {
125125
return STATUS_LIST[value, UNKNOWN]
126126
}
127127

@@ -134,7 +134,7 @@ class FirebaseFunctionsException : FirebaseException {
134134
* @return The corresponding Code, or Code.UNKNOWN if none.
135135
*/
136136
@JvmStatic
137-
fun fromHttpStatus(status: Int): Code {
137+
public fun fromHttpStatus(status: Int): Code {
138138
when (status) {
139139
200 -> return OK
140140
400 -> return INVALID_ARGUMENT
@@ -159,14 +159,14 @@ class FirebaseFunctionsException : FirebaseException {
159159
*
160160
* @return the code for the FirebaseFunctionsException
161161
*/
162-
val code: Code
162+
public val code: Code
163163

164164
/**
165165
* Gets the details object, if one was included in the error response.
166166
*
167167
* @return the object included in the "details" field of the response.
168168
*/
169-
val details: Any?
169+
public val details: Any?
170170

171171
internal constructor(message: String, code: Code, details: Any?) : super(message) {
172172
this.code = code
@@ -183,7 +183,7 @@ class FirebaseFunctionsException : FirebaseException {
183183
this.details = details
184184
}
185185

186-
companion object {
186+
public companion object {
187187
/**
188188
* Takes an HTTP response and returns the corresponding Exception if any.
189189
*
@@ -193,7 +193,7 @@ class FirebaseFunctionsException : FirebaseException {
193193
* @return The corresponding Exception, or null if none.
194194
*/
195195
@JvmStatic
196-
fun fromResponse(
196+
public fun fromResponse(
197197
code: Code,
198198
body: String?,
199199
serializer: Serializer

firebase-functions/src/main/java/com/google/firebase/functions/Functions.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ import com.google.firebase.components.ComponentRegistrar
2424
import java.net.URL
2525

2626
/** Returns the [FirebaseFunctions] instance of the default [FirebaseApp]. */
27-
val Firebase.functions: FirebaseFunctions
27+
public val Firebase.functions: FirebaseFunctions
2828
get() = FirebaseFunctions.getInstance()
2929

3030
/** Returns the [FirebaseFunctions] instance of a given [regionOrCustomDomain]. */
31-
fun Firebase.functions(regionOrCustomDomain: String): FirebaseFunctions =
31+
public fun Firebase.functions(regionOrCustomDomain: String): FirebaseFunctions =
3232
FirebaseFunctions.getInstance(regionOrCustomDomain)
3333

3434
/** Returns the [FirebaseFunctions] instance of a given [FirebaseApp]. */
35-
fun Firebase.functions(app: FirebaseApp): FirebaseFunctions = FirebaseFunctions.getInstance(app)
35+
public fun Firebase.functions(app: FirebaseApp): FirebaseFunctions =
36+
FirebaseFunctions.getInstance(app)
3637

3738
/** Returns the [FirebaseFunctions] instance of a given [FirebaseApp] and [regionOrCustomDomain]. */
38-
fun Firebase.functions(app: FirebaseApp, regionOrCustomDomain: String): FirebaseFunctions =
39+
public fun Firebase.functions(app: FirebaseApp, regionOrCustomDomain: String): FirebaseFunctions =
3940
FirebaseFunctions.getInstance(app, regionOrCustomDomain)
4041

4142
/** @suppress */
4243
@Keep
43-
class FirebaseFunctionsKtxRegistrar : ComponentRegistrar {
44+
public class FirebaseFunctionsKtxRegistrar : ComponentRegistrar {
4445
override fun getComponents(): List<Component<*>> = listOf()
4546
}
4647

4748
/** Returns a reference to the Callable HTTPS trigger with the given name and call options. */
48-
fun FirebaseFunctions.getHttpsCallable(
49+
public fun FirebaseFunctions.getHttpsCallable(
4950
name: String,
5051
init: HttpsCallableOptions.Builder.() -> Unit
5152
): HttpsCallableReference {
@@ -55,7 +56,7 @@ fun FirebaseFunctions.getHttpsCallable(
5556
}
5657

5758
/** Returns a reference to the Callable HTTPS trigger with the given URL and call options. */
58-
fun FirebaseFunctions.getHttpsCallableFromUrl(
59+
public fun FirebaseFunctions.getHttpsCallableFromUrl(
5960
url: URL,
6061
init: HttpsCallableOptions.Builder.() -> Unit
6162
): HttpsCallableReference {

firebase-functions/src/main/java/com/google/firebase/functions/FunctionsRegistrar.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import java.util.concurrent.Executor
3636
* @hide
3737
*/
3838
@Keep
39-
class FunctionsRegistrar : ComponentRegistrar {
39+
public class FunctionsRegistrar : ComponentRegistrar {
4040
override fun getComponents(): List<Component<*>> {
4141
val liteExecutor = Qualified.qualified(Lightweight::class.java, Executor::class.java)
4242
val uiExecutor = Qualified.qualified(UiThread::class.java, Executor::class.java)
@@ -67,7 +67,7 @@ class FunctionsRegistrar : ComponentRegistrar {
6767
)
6868
}
6969

70-
companion object {
70+
private companion object {
7171
private const val LIBRARY_NAME = "fire-fn"
7272
}
7373
}

firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallOptions.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ import java.util.concurrent.TimeUnit
1717
import okhttp3.OkHttpClient
1818

1919
/** An internal class for keeping track of options applied to an HttpsCallableReference. */
20-
class HttpsCallOptions {
20+
public class HttpsCallOptions {
2121
// The timeout to use for calls from references created by this Functions.
2222
private var timeout = DEFAULT_TIMEOUT
2323
private var timeoutUnits = DEFAULT_TIMEOUT_UNITS
24-
@JvmField val limitedUseAppCheckTokens: Boolean
24+
@JvmField public val limitedUseAppCheckTokens: Boolean
2525

2626
/** Creates an (internal) HttpsCallOptions from the (external) [HttpsCallableOptions]. */
27-
constructor(publicCallableOptions: HttpsCallableOptions) {
27+
public constructor(publicCallableOptions: HttpsCallableOptions) {
2828
limitedUseAppCheckTokens = publicCallableOptions.limitedUseAppCheckTokens
2929
}
3030

31-
constructor() {
31+
public constructor() {
3232
limitedUseAppCheckTokens = false
3333
}
3434

35-
fun getLimitedUseAppCheckTokens(): Boolean {
35+
public fun getLimitedUseAppCheckTokens(): Boolean {
3636
return limitedUseAppCheckTokens
3737
}
3838

@@ -42,7 +42,7 @@ class HttpsCallOptions {
4242
* @param timeout The length of the timeout, in the given units.
4343
* @param units The units for the specified timeout.
4444
*/
45-
fun setTimeout(timeout: Long, units: TimeUnit) {
45+
public fun setTimeout(timeout: Long, units: TimeUnit) {
4646
this.timeout = timeout
4747
timeoutUnits = units
4848
}
@@ -52,20 +52,20 @@ class HttpsCallOptions {
5252
*
5353
* @return The timeout, in milliseconds.
5454
*/
55-
fun getTimeout(): Long {
55+
public fun getTimeout(): Long {
5656
return timeoutUnits.toMillis(timeout)
5757
}
5858

5959
/** Creates a new OkHttpClient with these options applied to it. */
60-
fun apply(client: OkHttpClient): OkHttpClient {
60+
public fun apply(client: OkHttpClient): OkHttpClient {
6161
return client
6262
.newBuilder()
6363
.callTimeout(timeout, timeoutUnits)
6464
.readTimeout(timeout, timeoutUnits)
6565
.build()
6666
}
6767

68-
companion object {
68+
private companion object {
6969
// The default timeout to use for all calls.
7070
private const val DEFAULT_TIMEOUT: Long = 70
7171
private val DEFAULT_TIMEOUT_UNITS = TimeUnit.SECONDS

firebase-functions/src/main/java/com/google/firebase/functions/HttpsCallableOptions.kt

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,39 @@ package com.google.firebase.functions
1818
*
1919
* These properties are immutable once a callable function reference is instantiated.
2020
*/
21-
class HttpsCallableOptions
21+
public class HttpsCallableOptions
2222
private constructor(
2323
/**
2424
* Returns the setting indicating if limited-use App Check tokens are enforced for this function.
2525
*/
2626
// If true, request a limited-use token from AppCheck.
27-
@JvmField val limitedUseAppCheckTokens: Boolean
27+
@JvmField public val limitedUseAppCheckTokens: Boolean
2828
) {
2929

30-
fun getLimitedUseAppCheckTokens(): Boolean {
30+
public fun getLimitedUseAppCheckTokens(): Boolean {
3131
return limitedUseAppCheckTokens
3232
}
3333

3434
/** Builder class for [com.google.firebase.functions.HttpsCallableOptions] */
35-
class Builder {
36-
@JvmField var limitedUseAppCheckTokens = false
35+
public class Builder {
36+
@JvmField public var limitedUseAppCheckTokens: Boolean = false
3737

3838
/** Returns the setting indicating if limited-use App Check tokens are enforced. */
39-
fun getLimitedUseAppCheckTokens(): Boolean {
39+
public fun getLimitedUseAppCheckTokens(): Boolean {
4040
return limitedUseAppCheckTokens
4141
}
4242

4343
/**
4444
* Sets whether or not to use limited-use App Check tokens when invoking the associated
4545
* function.
4646
*/
47-
fun setLimitedUseAppCheckTokens(limitedUse: Boolean): Builder {
47+
public fun setLimitedUseAppCheckTokens(limitedUse: Boolean): Builder {
4848
limitedUseAppCheckTokens = limitedUse
4949
return this
5050
}
5151

5252
/** Builds a new [com.google.firebase.functions.HttpsCallableOptions]. */
53-
fun build(): HttpsCallableOptions {
53+
public fun build(): HttpsCallableOptions {
5454
return HttpsCallableOptions(limitedUseAppCheckTokens)
5555
}
5656
}

0 commit comments

Comments
 (0)