Skip to content

Commit 54ad943

Browse files
chore(docs): readme improvements (#356)
1 parent 1f11530 commit 54ad943

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

README.md

+49-44
Original file line numberDiff line numberDiff line change
@@ -314,16 +314,16 @@ send non-conforming fields in the request body. Extra fields overwrite any struc
314314
key, so only use with trusted data.
315315

316316
```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
321321

322322
Point: openai.Point{
323-
X: 0, // required field will serialize as 0
323+
X: 0, // required field will serialize as 0
324324
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+
},
327327

328328
Origin: openai.Origin{}, // the zero value of [Origin] is considered omitted
329329
}
@@ -352,8 +352,8 @@ These methods return a mutable pointer to the underlying data, if present.
352352
```go
353353
// Only one field can be non-zero, use param.IsOmitted() to check if a field is set
354354
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`
357357
}
358358

359359
animal := AnimalUnionParam{
@@ -376,31 +376,41 @@ if address := animal.GetOwner().GetAddress(); address != nil {
376376
All fields in response structs are value types (not pointers or wrappers).
377377

378378
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.
380381

381382
All response structs also include a special `JSON` field, containing more detailed
382383
information about each property, which you can use like so:
383384

384385
```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
404414
```
405415

406416
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"`.
423433

424434
```go
425435
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"`
429442
// ...
430443
JSON struct {
431-
OfString resp.Field
432-
Name resp.Field
433-
Owner resp.Field
444+
Owner resp.Field
434445
// ...
435-
}
446+
} `json:"-"`
436447
}
437448

438449
// If animal variant
439-
if animal.Owner.Address.JSON.ZipCode == "" {
450+
if animal.Owner.Address.ZipCode == "" {
440451
panic("missing zip code")
441452
}
442453

443-
// If string variant
444-
if !animal.OfString == "" {
445-
panic("expected a name")
446-
}
447-
448454
// Switch on the variant
449-
switch variant := animalOrName.AsAny().(type) {
450-
case string:
455+
switch variant := animal.AsAny().(type) {
451456
case Dog:
452457
case Cat:
453458
default:

0 commit comments

Comments
 (0)