Skip to content

Commit 8eeec84

Browse files
chore(internal): use getOrNull instead of orElse(null) (#287)
1 parent 46c9b3a commit 8eeec84

File tree

77 files changed

+475
-548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+475
-548
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.time.Clock
1616
import java.time.Duration
1717
import java.util.Optional
1818
import java.util.concurrent.Executor
19+
import kotlin.jvm.optionals.getOrNull
1920

2021
class OpenAIOkHttpClient private constructor() {
2122

@@ -160,11 +161,11 @@ class OpenAIOkHttpClient private constructor() {
160161

161162
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
162163

163-
fun organization(organization: Optional<String>) = organization(organization.orElse(null))
164+
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
164165

165166
fun project(project: String?) = apply { clientOptions.project(project) }
166167

167-
fun project(project: Optional<String>) = project(project.orElse(null))
168+
fun project(project: Optional<String>) = project(project.getOrNull())
168169

169170
fun fromEnv() = apply { clientOptions.fromEnv() }
170171

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import java.time.Clock
1616
import java.time.Duration
1717
import java.util.Optional
1818
import java.util.concurrent.Executor
19+
import kotlin.jvm.optionals.getOrNull
1920

2021
class OpenAIOkHttpClientAsync private constructor() {
2122

@@ -160,11 +161,11 @@ class OpenAIOkHttpClientAsync private constructor() {
160161

161162
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
162163

163-
fun organization(organization: Optional<String>) = organization(organization.orElse(null))
164+
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
164165

165166
fun project(project: String?) = apply { clientOptions.project(project) }
166167

167-
fun project(project: Optional<String>) = project(project.orElse(null))
168+
fun project(project: Optional<String>) = project(project.getOrNull())
168169

169170
fun fromEnv() = apply { clientOptions.fromEnv() }
170171

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.util.concurrent.Executor
1818
import java.util.concurrent.Executors
1919
import java.util.concurrent.ThreadFactory
2020
import java.util.concurrent.atomic.AtomicLong
21+
import kotlin.jvm.optionals.getOrNull
2122

2223
class ClientOptions
2324
private constructor(
@@ -128,11 +129,11 @@ private constructor(
128129

129130
fun organization(organization: String?) = apply { this.organization = organization }
130131

131-
fun organization(organization: Optional<String>) = organization(organization.orElse(null))
132+
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
132133

133134
fun project(project: String?) = apply { this.project = project }
134135

135-
fun project(project: Optional<String>) = project(project.orElse(null))
136+
fun project(project: Optional<String>) = project(project.getOrNull())
136137

137138
fun headers(headers: Headers) = apply {
138139
this.headers.clear()

openai-java-core/src/main/kotlin/com/openai/core/Timeout.kt

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.openai.core
55
import java.time.Duration
66
import java.util.Objects
77
import java.util.Optional
8+
import kotlin.jvm.optionals.getOrNull
89

910
/** A class containing timeouts for various processing phases of a request. */
1011
class Timeout
@@ -96,7 +97,7 @@ private constructor(
9697
*
9798
* Defaults to `Duration.ofMinutes(1)`.
9899
*/
99-
fun connect(connect: Optional<Duration>) = connect(connect.orElse(null))
100+
fun connect(connect: Optional<Duration>) = connect(connect.getOrNull())
100101

101102
/**
102103
* The maximum time allowed between two data packets when waiting for the server’s response.
@@ -114,7 +115,7 @@ private constructor(
114115
*
115116
* Defaults to `request()`.
116117
*/
117-
fun read(read: Optional<Duration>) = read(read.orElse(null))
118+
fun read(read: Optional<Duration>) = read(read.getOrNull())
118119

119120
/**
120121
* The maximum time allowed between two data packets when sending the request to the server.
@@ -132,7 +133,7 @@ private constructor(
132133
*
133134
* Defaults to `request()`.
134135
*/
135-
fun write(write: Optional<Duration>) = write(write.orElse(null))
136+
fun write(write: Optional<Duration>) = write(write.getOrNull())
136137

137138
/**
138139
* The maximum time allowed for a complete HTTP call, not including retries.
@@ -156,7 +157,7 @@ private constructor(
156157
*
157158
* Defaults to `Duration.ofMinutes(10)`.
158159
*/
159-
fun request(request: Optional<Duration>) = request(request.orElse(null))
160+
fun request(request: Optional<Duration>) = request(request.getOrNull())
160161

161162
fun build(): Timeout = Timeout(connect, read, write, request)
162163
}

openai-java-core/src/main/kotlin/com/openai/models/Assistant.kt

+9-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.openai.core.toImmutable
1818
import com.openai.errors.OpenAIInvalidDataException
1919
import java.util.Objects
2020
import java.util.Optional
21+
import kotlin.jvm.optionals.getOrNull
2122

2223
/** Represents an `assistant` that can call the model and use tools. */
2324
@NoAutoDetect
@@ -345,7 +346,7 @@ private constructor(
345346
fun description(description: String?) = description(JsonField.ofNullable(description))
346347

347348
/** The description of the assistant. The maximum length is 512 characters. */
348-
fun description(description: Optional<String>) = description(description.orElse(null))
349+
fun description(description: Optional<String>) = description(description.getOrNull())
349350

350351
/** The description of the assistant. The maximum length is 512 characters. */
351352
fun description(description: JsonField<String>) = apply { this.description = description }
@@ -360,7 +361,7 @@ private constructor(
360361
* The system instructions that the assistant uses. The maximum length is 256,000
361362
* characters.
362363
*/
363-
fun instructions(instructions: Optional<String>) = instructions(instructions.orElse(null))
364+
fun instructions(instructions: Optional<String>) = instructions(instructions.getOrNull())
364365

365366
/**
366367
* The system instructions that the assistant uses. The maximum length is 256,000
@@ -388,7 +389,7 @@ private constructor(
388389
* Keys are strings with a maximum length of 64 characters. Values are strings with a
389390
* maximum length of 512 characters.
390391
*/
391-
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.orElse(null))
392+
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.getOrNull())
392393

393394
/**
394395
* Set of 16 key-value pairs that can be attached to an object. This can be useful for
@@ -420,7 +421,7 @@ private constructor(
420421
fun name(name: String?) = name(JsonField.ofNullable(name))
421422

422423
/** The name of the assistant. The maximum length is 256 characters. */
423-
fun name(name: Optional<String>) = name(name.orElse(null))
424+
fun name(name: Optional<String>) = name(name.getOrNull())
424425

425426
/** The name of the assistant. The maximum length is 256 characters. */
426427
fun name(name: JsonField<String>) = apply { this.name = name }
@@ -521,7 +522,7 @@ private constructor(
521522
* `max_tokens` or the conversation exceeded the max context length.
522523
*/
523524
fun responseFormat(responseFormat: Optional<AssistantResponseFormatOption>) =
524-
responseFormat(responseFormat.orElse(null))
525+
responseFormat(responseFormat.getOrNull())
525526

526527
/**
527528
* Specifies the format that the model must output. Compatible with
@@ -642,9 +643,7 @@ private constructor(
642643
* output more random, while lower values like 0.2 will make it more focused and
643644
* deterministic.
644645
*/
645-
@Suppress("USELESS_CAST") // See https://youtrack.jetbrains.com/issue/KT-74228
646-
fun temperature(temperature: Optional<Double>) =
647-
temperature(temperature.orElse(null) as Double?)
646+
fun temperature(temperature: Optional<Double>) = temperature(temperature.getOrNull())
648647

649648
/**
650649
* What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the
@@ -667,7 +666,7 @@ private constructor(
667666
* while the `file_search` tool requires a list of vector store IDs.
668667
*/
669668
fun toolResources(toolResources: Optional<ToolResources>) =
670-
toolResources(toolResources.orElse(null))
669+
toolResources(toolResources.getOrNull())
671670

672671
/**
673672
* A set of resources that are used by the assistant's tools. The resources are specific to
@@ -703,8 +702,7 @@ private constructor(
703702
*
704703
* We generally recommend altering this or temperature but not both.
705704
*/
706-
@Suppress("USELESS_CAST") // See https://youtrack.jetbrains.com/issue/KT-74228
707-
fun topP(topP: Optional<Double>) = topP(topP.orElse(null) as Double?)
705+
fun topP(topP: Optional<Double>) = topP(topP.getOrNull())
708706

709707
/**
710708
* An alternative to sampling with temperature, called nucleus sampling, where the model

openai-java-core/src/main/kotlin/com/openai/models/Batch.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.openai.core.toImmutable
1919
import com.openai.errors.OpenAIInvalidDataException
2020
import java.util.Objects
2121
import java.util.Optional
22+
import kotlin.jvm.optionals.getOrNull
2223

2324
@NoAutoDetect
2425
class Batch
@@ -456,7 +457,7 @@ private constructor(
456457
* Keys are strings with a maximum length of 64 characters. Values are strings with a
457458
* maximum length of 512 characters.
458459
*/
459-
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.orElse(null))
460+
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.getOrNull())
460461

461462
/**
462463
* Set of 16 key-value pairs that can be attached to an object. This can be useful for

openai-java-core/src/main/kotlin/com/openai/models/BatchCreateParams.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.openai.core.toImmutable
2121
import com.openai.errors.OpenAIInvalidDataException
2222
import java.util.Objects
2323
import java.util.Optional
24+
import kotlin.jvm.optionals.getOrNull
2425

2526
/** Creates and executes a batch from an uploaded file of requests */
2627
class BatchCreateParams
@@ -340,7 +341,7 @@ private constructor(
340341
* Keys are strings with a maximum length of 64 characters. Values are strings with a
341342
* maximum length of 512 characters.
342343
*/
343-
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.orElse(null))
344+
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.getOrNull())
344345

345346
/**
346347
* Set of 16 key-value pairs that can be attached to an object. This can be useful for
@@ -507,7 +508,7 @@ private constructor(
507508
* Keys are strings with a maximum length of 64 characters. Values are strings with a
508509
* maximum length of 512 characters.
509510
*/
510-
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.orElse(null))
511+
fun metadata(metadata: Optional<Metadata>) = metadata(metadata.getOrNull())
511512

512513
/**
513514
* Set of 16 key-value pairs that can be attached to an object. This can be useful for

openai-java-core/src/main/kotlin/com/openai/models/BatchError.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.openai.core.immutableEmptyMap
1515
import com.openai.core.toImmutable
1616
import java.util.Objects
1717
import java.util.Optional
18+
import kotlin.jvm.optionals.getOrNull
1819

1920
@NoAutoDetect
2021
class BatchError
@@ -110,8 +111,7 @@ private constructor(
110111
fun line(line: Long) = line(line as Long?)
111112

112113
/** The line number of the input file where the error occurred, if applicable. */
113-
@Suppress("USELESS_CAST") // See https://youtrack.jetbrains.com/issue/KT-74228
114-
fun line(line: Optional<Long>) = line(line.orElse(null) as Long?)
114+
fun line(line: Optional<Long>) = line(line.getOrNull())
115115

116116
/** The line number of the input file where the error occurred, if applicable. */
117117
fun line(line: JsonField<Long>) = apply { this.line = line }
@@ -126,7 +126,7 @@ private constructor(
126126
fun param(param: String?) = param(JsonField.ofNullable(param))
127127

128128
/** The name of the parameter that caused the error, if applicable. */
129-
fun param(param: Optional<String>) = param(param.orElse(null))
129+
fun param(param: Optional<String>) = param(param.getOrNull())
130130

131131
/** The name of the parameter that caused the error, if applicable. */
132132
fun param(param: JsonField<String>) = apply { this.param = param }

openai-java-core/src/main/kotlin/com/openai/models/BatchListPage.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import java.util.Objects
1818
import java.util.Optional
1919
import java.util.stream.Stream
2020
import java.util.stream.StreamSupport
21+
import kotlin.jvm.optionals.getOrNull
2122

2223
/** List your organization's batches. */
2324
class BatchListPage
@@ -166,7 +167,7 @@ private constructor(
166167
while (index < page.data().size) {
167168
yield(page.data()[index++])
168169
}
169-
page = page.getNextPage().orElse(null) ?: break
170+
page = page.getNextPage().getOrNull() ?: break
170171
index = 0
171172
}
172173
}

openai-java-core/src/main/kotlin/com/openai/models/BatchListParams.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.openai.core.http.Headers
88
import com.openai.core.http.QueryParams
99
import java.util.Objects
1010
import java.util.Optional
11+
import kotlin.jvm.optionals.getOrNull
1112

1213
/** List your organization's batches. */
1314
class BatchListParams
@@ -86,7 +87,7 @@ private constructor(
8687
* obj_foo, your subsequent call can include after=obj_foo in order to fetch the next page
8788
* of the list.
8889
*/
89-
fun after(after: Optional<String>) = after(after.orElse(null))
90+
fun after(after: Optional<String>) = after(after.getOrNull())
9091

9192
/**
9293
* A limit on the number of objects to be returned. Limit can range between 1 and 100, and
@@ -104,8 +105,7 @@ private constructor(
104105
* A limit on the number of objects to be returned. Limit can range between 1 and 100, and
105106
* the default is 20.
106107
*/
107-
@Suppress("USELESS_CAST") // See https://youtrack.jetbrains.com/issue/KT-74228
108-
fun limit(limit: Optional<Long>) = limit(limit.orElse(null) as Long?)
108+
fun limit(limit: Optional<Long>) = limit(limit.getOrNull())
109109

110110
fun additionalHeaders(additionalHeaders: Headers) = apply {
111111
this.additionalHeaders.clear()

0 commit comments

Comments
 (0)