Skip to content

Commit 95c7068

Browse files
chore(internal): swap from getNullable to getOptional (#406)
1 parent a4853ff commit 95c7068

File tree

157 files changed

+694
-868
lines changed

Some content is hidden

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

157 files changed

+694
-868
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ sealed class JsonField<out T : Any> {
132132
}
133133

134134
@JvmSynthetic
135-
internal fun getNullable(name: String): T? =
135+
internal fun getOptional(name: String): Optional<@UnsafeVariance T> =
136136
when (this) {
137-
is KnownValue -> value
138-
is JsonMissing -> null
139-
is JsonNull -> null
137+
is KnownValue -> Optional.of(value)
138+
is JsonMissing,
139+
is JsonNull -> Optional.empty()
140140
else -> throw OpenAIInvalidDataException("`$name` is invalid, received $this")
141141
}
142142

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private constructor(
3838
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
3939
* server responded with an unexpected value).
4040
*/
41-
fun code(): Optional<String> = Optional.ofNullable(code.getNullable("code"))
41+
fun code(): Optional<String> = code.getOptional("code")
4242

4343
/**
4444
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type or is
@@ -50,7 +50,7 @@ private constructor(
5050
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
5151
* server responded with an unexpected value).
5252
*/
53-
fun param(): Optional<String> = Optional.ofNullable(param.getNullable("param"))
53+
fun param(): Optional<String> = param.getOptional("param")
5454

5555
/**
5656
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type or is

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ private constructor(
5454
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
5555
* server responded with an unexpected value).
5656
*/
57-
fun description(): Optional<String> =
58-
Optional.ofNullable(description.getNullable("description"))
57+
fun description(): Optional<String> = description.getOptional("description")
5958

6059
/**
6160
* The parameters the functions accepts, described as a JSON Schema object. See the
@@ -68,8 +67,7 @@ private constructor(
6867
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
6968
* server responded with an unexpected value).
7069
*/
71-
fun parameters(): Optional<FunctionParameters> =
72-
Optional.ofNullable(parameters.getNullable("parameters"))
70+
fun parameters(): Optional<FunctionParameters> = parameters.getOptional("parameters")
7371

7472
/**
7573
* Whether to enable strict schema adherence when generating the function call. If set to true,
@@ -80,7 +78,7 @@ private constructor(
8078
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
8179
* server responded with an unexpected value).
8280
*/
83-
fun strict(): Optional<Boolean> = Optional.ofNullable(strict.getNullable("strict"))
81+
fun strict(): Optional<Boolean> = strict.getOptional("strict")
8482

8583
/**
8684
* Returns the raw JSON value of [name].

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private constructor(
5050
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
5151
* server responded with an unexpected value).
5252
*/
53-
fun effort(): Optional<ReasoningEffort> = Optional.ofNullable(effort.getNullable("effort"))
53+
fun effort(): Optional<ReasoningEffort> = effort.getOptional("effort")
5454

5555
/**
5656
* **computer_use_preview only**
@@ -62,7 +62,7 @@ private constructor(
6262
* server responded with an unexpected value).
6363
*/
6464
fun generateSummary(): Optional<GenerateSummary> =
65-
Optional.ofNullable(generateSummary.getNullable("generate_summary"))
65+
generateSummary.getOptional("generate_summary")
6666

6767
/**
6868
* Returns the raw JSON value of [effort].

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,7 @@ private constructor(
241241
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
242242
* server responded with an unexpected value).
243243
*/
244-
fun description(): Optional<String> =
245-
Optional.ofNullable(description.getNullable("description"))
244+
fun description(): Optional<String> = description.getOptional("description")
246245

247246
/**
248247
* The schema for the response format, described as a JSON Schema object. Learn how to build
@@ -251,7 +250,7 @@ private constructor(
251250
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
252251
* server responded with an unexpected value).
253252
*/
254-
fun schema(): Optional<Schema> = Optional.ofNullable(schema.getNullable("schema"))
253+
fun schema(): Optional<Schema> = schema.getOptional("schema")
255254

256255
/**
257256
* Whether to enable strict schema adherence when generating the output. If set to true, the
@@ -262,7 +261,7 @@ private constructor(
262261
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
263262
* server responded with an unexpected value).
264263
*/
265-
fun strict(): Optional<Boolean> = Optional.ofNullable(strict.getNullable("strict"))
264+
fun strict(): Optional<Boolean> = strict.getOptional("strict")
266265

267266
/**
268267
* Returns the raw JSON value of [name].

openai-java-core/src/main/kotlin/com/openai/models/audio/speech/SpeechCreateParams.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,7 @@ private constructor(
488488
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
489489
* server responded with an unexpected value).
490490
*/
491-
fun instructions(): Optional<String> =
492-
Optional.ofNullable(instructions.getNullable("instructions"))
491+
fun instructions(): Optional<String> = instructions.getOptional("instructions")
493492

494493
/**
495494
* The format to audio in. Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and
@@ -499,7 +498,7 @@ private constructor(
499498
* server responded with an unexpected value).
500499
*/
501500
fun responseFormat(): Optional<ResponseFormat> =
502-
Optional.ofNullable(responseFormat.getNullable("response_format"))
501+
responseFormat.getOptional("response_format")
503502

504503
/**
505504
* The speed of the generated audio. Select a value from `0.25` to `4.0`. `1.0` is the
@@ -508,7 +507,7 @@ private constructor(
508507
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
509508
* server responded with an unexpected value).
510509
*/
511-
fun speed(): Optional<Double> = Optional.ofNullable(speed.getNullable("speed"))
510+
fun speed(): Optional<Double> = speed.getOptional("speed")
512511

513512
/**
514513
* Returns the raw JSON value of [input].

openai-java-core/src/main/kotlin/com/openai/models/audio/transcriptions/Transcription.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private constructor(
5151
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
5252
* server responded with an unexpected value).
5353
*/
54-
fun logprobs(): Optional<List<Logprob>> = Optional.ofNullable(logprobs.getNullable("logprobs"))
54+
fun logprobs(): Optional<List<Logprob>> = logprobs.getOptional("logprobs")
5555

5656
/**
5757
* Returns the raw JSON value of [text].
@@ -239,23 +239,23 @@ private constructor(
239239
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
240240
* server responded with an unexpected value).
241241
*/
242-
fun token(): Optional<String> = Optional.ofNullable(token.getNullable("token"))
242+
fun token(): Optional<String> = token.getOptional("token")
243243

244244
/**
245245
* The bytes of the token.
246246
*
247247
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
248248
* server responded with an unexpected value).
249249
*/
250-
fun bytes(): Optional<List<Double>> = Optional.ofNullable(bytes.getNullable("bytes"))
250+
fun bytes(): Optional<List<Double>> = bytes.getOptional("bytes")
251251

252252
/**
253253
* The log probability of the token.
254254
*
255255
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
256256
* server responded with an unexpected value).
257257
*/
258-
fun logprob(): Optional<Double> = Optional.ofNullable(logprob.getNullable("logprob"))
258+
fun logprob(): Optional<Double> = logprob.getOptional("logprob")
259259

260260
/**
261261
* Returns the raw JSON value of [token].

openai-java-core/src/main/kotlin/com/openai/models/audio/transcriptions/TranscriptionCreateParams.kt

+6-9
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,7 @@ private constructor(
573573
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
574574
* server responded with an unexpected value).
575575
*/
576-
fun include(): Optional<List<TranscriptionInclude>> =
577-
Optional.ofNullable(include.value.getNullable("include"))
576+
fun include(): Optional<List<TranscriptionInclude>> = include.value.getOptional("include")
578577

579578
/**
580579
* The language of the input audio. Supplying the input language in
@@ -584,8 +583,7 @@ private constructor(
584583
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
585584
* server responded with an unexpected value).
586585
*/
587-
fun language(): Optional<String> =
588-
Optional.ofNullable(language.value.getNullable("language"))
586+
fun language(): Optional<String> = language.value.getOptional("language")
589587

590588
/**
591589
* An optional text to guide the model's style or continue a previous audio segment. The
@@ -595,7 +593,7 @@ private constructor(
595593
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
596594
* server responded with an unexpected value).
597595
*/
598-
fun prompt(): Optional<String> = Optional.ofNullable(prompt.value.getNullable("prompt"))
596+
fun prompt(): Optional<String> = prompt.value.getOptional("prompt")
599597

600598
/**
601599
* The format of the output, in one of these options: `json`, `text`, `srt`, `verbose_json`,
@@ -606,7 +604,7 @@ private constructor(
606604
* server responded with an unexpected value).
607605
*/
608606
fun responseFormat(): Optional<AudioResponseFormat> =
609-
Optional.ofNullable(responseFormat.value.getNullable("response_format"))
607+
responseFormat.value.getOptional("response_format")
610608

611609
/**
612610
* The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output
@@ -618,8 +616,7 @@ private constructor(
618616
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
619617
* server responded with an unexpected value).
620618
*/
621-
fun temperature(): Optional<Double> =
622-
Optional.ofNullable(temperature.value.getNullable("temperature"))
619+
fun temperature(): Optional<Double> = temperature.value.getOptional("temperature")
623620

624621
/**
625622
* The timestamp granularities to populate for this transcription. `response_format` must be
@@ -631,7 +628,7 @@ private constructor(
631628
* server responded with an unexpected value).
632629
*/
633630
fun timestampGranularities(): Optional<List<TimestampGranularity>> =
634-
Optional.ofNullable(timestampGranularities.value.getNullable("timestamp_granularities"))
631+
timestampGranularities.value.getOptional("timestamp_granularities")
635632

636633
/**
637634
* Returns the raw multipart value of [file].

openai-java-core/src/main/kotlin/com/openai/models/audio/transcriptions/TranscriptionTextDeltaEvent.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private constructor(
7171
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
7272
* server responded with an unexpected value).
7373
*/
74-
fun logprobs(): Optional<List<Logprob>> = Optional.ofNullable(logprobs.getNullable("logprobs"))
74+
fun logprobs(): Optional<List<Logprob>> = logprobs.getOptional("logprobs")
7575

7676
/**
7777
* Returns the raw JSON value of [delta].
@@ -282,23 +282,23 @@ private constructor(
282282
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
283283
* server responded with an unexpected value).
284284
*/
285-
fun token(): Optional<String> = Optional.ofNullable(token.getNullable("token"))
285+
fun token(): Optional<String> = token.getOptional("token")
286286

287287
/**
288288
* The bytes that were used to generate the log probability.
289289
*
290290
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
291291
* server responded with an unexpected value).
292292
*/
293-
fun bytes(): Optional<List<JsonValue>> = Optional.ofNullable(bytes.getNullable("bytes"))
293+
fun bytes(): Optional<List<JsonValue>> = bytes.getOptional("bytes")
294294

295295
/**
296296
* The log probability of the token.
297297
*
298298
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
299299
* server responded with an unexpected value).
300300
*/
301-
fun logprob(): Optional<Double> = Optional.ofNullable(logprob.getNullable("logprob"))
301+
fun logprob(): Optional<Double> = logprob.getOptional("logprob")
302302

303303
/**
304304
* Returns the raw JSON value of [token].

openai-java-core/src/main/kotlin/com/openai/models/audio/transcriptions/TranscriptionTextDoneEvent.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private constructor(
7171
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
7272
* server responded with an unexpected value).
7373
*/
74-
fun logprobs(): Optional<List<Logprob>> = Optional.ofNullable(logprobs.getNullable("logprobs"))
74+
fun logprobs(): Optional<List<Logprob>> = logprobs.getOptional("logprobs")
7575

7676
/**
7777
* Returns the raw JSON value of [text].
@@ -282,23 +282,23 @@ private constructor(
282282
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
283283
* server responded with an unexpected value).
284284
*/
285-
fun token(): Optional<String> = Optional.ofNullable(token.getNullable("token"))
285+
fun token(): Optional<String> = token.getOptional("token")
286286

287287
/**
288288
* The bytes that were used to generate the log probability.
289289
*
290290
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
291291
* server responded with an unexpected value).
292292
*/
293-
fun bytes(): Optional<List<JsonValue>> = Optional.ofNullable(bytes.getNullable("bytes"))
293+
fun bytes(): Optional<List<JsonValue>> = bytes.getOptional("bytes")
294294

295295
/**
296296
* The log probability of the token.
297297
*
298298
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
299299
* server responded with an unexpected value).
300300
*/
301-
fun logprob(): Optional<Double> = Optional.ofNullable(logprob.getNullable("logprob"))
301+
fun logprob(): Optional<Double> = logprob.getOptional("logprob")
302302

303303
/**
304304
* Returns the raw JSON value of [token].

openai-java-core/src/main/kotlin/com/openai/models/audio/transcriptions/TranscriptionVerbose.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ private constructor(
7575
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
7676
* server responded with an unexpected value).
7777
*/
78-
fun segments(): Optional<List<TranscriptionSegment>> =
79-
Optional.ofNullable(segments.getNullable("segments"))
78+
fun segments(): Optional<List<TranscriptionSegment>> = segments.getOptional("segments")
8079

8180
/**
8281
* Extracted words and their corresponding timestamps.
8382
*
8483
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
8584
* server responded with an unexpected value).
8685
*/
87-
fun words(): Optional<List<TranscriptionWord>> = Optional.ofNullable(words.getNullable("words"))
86+
fun words(): Optional<List<TranscriptionWord>> = words.getOptional("words")
8887

8988
/**
9089
* Returns the raw JSON value of [duration].

openai-java-core/src/main/kotlin/com/openai/models/audio/translations/TranslationCreateParams.kt

+3-4
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ private constructor(
435435
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
436436
* server responded with an unexpected value).
437437
*/
438-
fun prompt(): Optional<String> = Optional.ofNullable(prompt.value.getNullable("prompt"))
438+
fun prompt(): Optional<String> = prompt.value.getOptional("prompt")
439439

440440
/**
441441
* The format of the output, in one of these options: `json`, `text`, `srt`, `verbose_json`,
@@ -445,7 +445,7 @@ private constructor(
445445
* server responded with an unexpected value).
446446
*/
447447
fun responseFormat(): Optional<ResponseFormat> =
448-
Optional.ofNullable(responseFormat.value.getNullable("response_format"))
448+
responseFormat.value.getOptional("response_format")
449449

450450
/**
451451
* The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output
@@ -457,8 +457,7 @@ private constructor(
457457
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
458458
* server responded with an unexpected value).
459459
*/
460-
fun temperature(): Optional<Double> =
461-
Optional.ofNullable(temperature.value.getNullable("temperature"))
460+
fun temperature(): Optional<Double> = temperature.value.getOptional("temperature")
462461

463462
/**
464463
* Returns the raw multipart value of [file].

openai-java-core/src/main/kotlin/com/openai/models/audio/translations/TranslationVerbose.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ private constructor(
6969
* @throws OpenAIInvalidDataException if the JSON field has an unexpected type (e.g. if the
7070
* server responded with an unexpected value).
7171
*/
72-
fun segments(): Optional<List<TranscriptionSegment>> =
73-
Optional.ofNullable(segments.getNullable("segments"))
72+
fun segments(): Optional<List<TranscriptionSegment>> = segments.getOptional("segments")
7473

7574
/**
7675
* Returns the raw JSON value of [duration].

0 commit comments

Comments
 (0)