openai/openai-go

Public

mirrored fromhttps://github.com/openai/openai-goAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
9314b6c3bd913e265f75d0a49600b60d6d25d8ae

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/param/null_test.go

49lines · modecode

1package param_test
2
3import (
4 "encoding/json"
5 "github.com/openai/openai-go/v3/packages/param"
6 "testing"
7)
8
9type Nullables struct {
10 Slice []int `json:"slice,omitzero"`
11 Map map[string]int `json:"map,omitzero"`
12 param.APIObject
13}
14
15func (n Nullables) MarshalJSON() ([]byte, error) {
16 type shadow Nullables
17 return param.MarshalObject(n, (*shadow)(&n))
18}
19
20func TestNullMarshal(t *testing.T) {
21 bytes, err := json.Marshal(Nullables{})
22 if err != nil {
23 t.Fatalf("json error %v", err.Error())
24 }
25 if string(bytes) != `{}` {
26 t.Fatalf("expected empty object, got %s", string(bytes))
27 }
28
29 obj := Nullables{
30 Slice: param.NullSlice[[]int](),
31 Map: param.NullMap[map[string]int](),
32 }
33 bytes, err = json.Marshal(obj)
34
35 if !param.IsNull(obj.Slice) {
36 t.Fatal("failed null check")
37 }
38 if !param.IsNull(obj.Map) {
39 t.Fatal("failed null check")
40 }
41
42 if err != nil {
43 t.Fatalf("json error %v", err.Error())
44 }
45 exp := `{"slice":null,"map":null}`
46 if string(bytes) != exp {
47 t.Fatalf("expected %s, got %s", exp, string(bytes))
48 }
49}
50