@@ -314,16 +314,16 @@ send non-conforming fields in the request body. Extra fields overwrite any struc
314
314
key, so only use with trusted data.
315
315
316
316
``` go
317
- params := FooParams {
318
- ID : " id_xxx " , // required property
319
- Name : openai.String (" hello " ), // optional property
320
- Description : param.NullOpt [string ](), // explicit null property
317
+ params := openai. ExampleParams {
318
+ ID : " id_xxx " , // required property
319
+ Name : openai.String (" ... " ), // optional property
320
+ Description : param.NullOpt [string ](), // explicit null property
321
321
322
322
Point : openai.Point {
323
- X: 0 , // required field will serialize as 0
323
+ X: 0 , // required field will serialize as 0
324
324
Y: openai.Int (1 ), // optional field will serialize as 1
325
- // ... omitted non-required fields will not be serialized
326
- }) ,
325
+ // ... omitted non-required fields will not be serialized
326
+ },
327
327
328
328
Origin : openai.Origin {}, // the zero value of [Origin] is considered omitted
329
329
}
@@ -352,8 +352,8 @@ These methods return a mutable pointer to the underlying data, if present.
352
352
``` go
353
353
// Only one field can be non-zero, use param.IsOmitted() to check if a field is set
354
354
type AnimalUnionParam struct {
355
- OfCat *Cat ` json:",omitzero,inline`
356
- OfDog *Dog ` json:",omitzero,inline`
355
+ OfCat *Cat ` json:",omitzero,inline`
356
+ OfDog *Dog ` json:",omitzero,inline`
357
357
}
358
358
359
359
animal := AnimalUnionParam {
@@ -376,31 +376,41 @@ if address := animal.GetOwner().GetAddress(); address != nil {
376
376
All fields in response structs are value types (not pointers or wrappers).
377
377
378
378
If a given field is ` null ` , not present, or invalid, the corresponding field
379
- will simply be its zero value.
379
+ will simply be its zero value. To handle optional fields, see the ` IsPresent() ` method
380
+ below.
380
381
381
382
All response structs also include a special ` JSON ` field, containing more detailed
382
383
information about each property, which you can use like so:
383
384
384
385
``` go
385
- if res.Name == " " {
386
- // true if `"name"` was unmarshalled successfully
387
- res.JSON .Name .IsPresent ()
388
-
389
- res.JSON .Name .IsExplicitNull () // true if `"name"` is explicitly null
390
- res.JSON .Name .Raw () == " " // true if `"name"` field does not exist
391
-
392
- // When the API returns data that cannot be coerced to the expected type:
393
- if !res.JSON .Name .IsPresent () && res.JSON .Name .Raw () != " " {
394
- raw := res.JSON .Name .Raw ()
395
-
396
- legacyName := struct {
397
- First string ` json:"first"`
398
- Last string ` json:"last"`
399
- }{}
400
- json.Unmarshal ([]byte (raw), &legacyName)
401
- name = legacyName.First + " " + legacyName.Last
402
- }
403
- }
386
+ type Animal struct {
387
+ Name string ` json:"name,nullable"`
388
+ Owners int ` json:"owners"`
389
+ Age int ` json:"age"`
390
+ JSON struct {
391
+ Name resp.Field
392
+ Owner resp.Field
393
+ Age resp.Field
394
+ } ` json:"-"`
395
+ }
396
+
397
+ var res Animal
398
+ json.Unmarshal ([]byte (` {"name": null, "owners": 0}` ), &res)
399
+
400
+ // Use the IsPresent() method to handle optional fields
401
+ res.Owners // 0
402
+ res.JSON .Owners .IsPresent () // true
403
+ res.JSON .Owners .Raw () // "0"
404
+
405
+ res.Age // 0
406
+ res.JSON .Age .IsPresent () // false
407
+ res.JSON .Age .Raw () // ""
408
+
409
+ // Use the IsExplicitNull() method to differentiate null and omitted
410
+ res.Name // ""
411
+ res.JSON .Name .IsPresent () // false
412
+ res.JSON .Name .Raw () // "null"
413
+ res.JSON .Name .IsExplicitNull () // true
404
414
```
405
415
406
416
These ` .JSON ` structs also include an ` ExtraFields ` map containing
@@ -423,31 +433,26 @@ the properties but prefixed with `Of` and feature the tag `json:"...,inline"`.
423
433
424
434
``` go
425
435
type AnimalUnion struct {
426
- OfString string ` json:",inline"`
427
- Name string ` json:"name"`
428
- Owner Person ` json:"owner"`
436
+ // From variants [Dog], [Cat]
437
+ Owner Person ` json:"owner"`
438
+ // From variant [Dog]
439
+ DogBreed string ` json:"dog_breed"`
440
+ // From variant [Cat]
441
+ CatBreed string ` json:"cat_breed"`
429
442
// ...
430
443
JSON struct {
431
- OfString resp.Field
432
- Name resp.Field
433
- Owner resp.Field
444
+ Owner resp.Field
434
445
// ...
435
- }
446
+ } ` json:"-" `
436
447
}
437
448
438
449
// If animal variant
439
- if animal.Owner .Address .JSON . ZipCode == " " {
450
+ if animal.Owner .Address .ZipCode == " " {
440
451
panic (" missing zip code" )
441
452
}
442
453
443
- // If string variant
444
- if !animal.OfString == " " {
445
- panic (" expected a name" )
446
- }
447
-
448
454
// Switch on the variant
449
- switch variant := animalOrName.AsAny ().(type ) {
450
- case string :
455
+ switch variant := animal.AsAny ().(type ) {
451
456
case Dog :
452
457
case Cat :
453
458
default :
0 commit comments