openai/openai-go

Public

mirrored from https://github.com/openai/openai-goAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jacob/file-param-example

Branches

Tags

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

Clone

HTTPS

Download ZIP

packages/param/option.go

114lines · modecode

1package param
2
3import (
4 "encoding/json"
5 "fmt"
6 "reflect"
7 "time"
8)
9
10func NewOpt[T comparable](v T) Opt[T] {
11 return Opt[T]{Value: v, Status: included}
12}
13
14// Sets an optional field to null, to set an object to null use [NullObj].
15func NullOpt[T comparable]() Opt[T] { return Opt[T]{Status: null} }
16
17type Opt[T comparable] struct {
18 Value T
19 // indicates whether the field should be omitted, null, or valid
20 Status Status
21}
22
23type Status int8
24
25const (
26 omitted Status = iota
27 null
28 included
29)
30
31type Optional interface {
32 // IsPresent returns true if the value is not "null" or omitted
33 IsPresent() bool
34
35 // IsOmitted returns true if the value is omitted, it returns false if the value is "null".
36 IsOmitted() bool
37
38 // IsNull returns true if the value is "null", it returns false if the value is omitted.
39 IsNull() bool
40}
41
42// IsPresent returns true if the value is not "null" and not omitted
43func (o Opt[T]) IsPresent() bool {
44 var empty Opt[T]
45 return o.Status == included || o != empty && o.Status != null
46}
47
48// IsNull returns true if the value is specifically the JSON value "null".
49// It returns false if the value is omitted.
50//
51// Prefer to use [IsPresent] to check the presence of a value.
52func (o Opt[T]) IsNull() bool { return o.Status == null }
53
54// IsOmitted returns true if the value is omitted.
55// It returns false if the value is the JSON value "null".
56//
57// Prefer to use [IsPresent] to check the presence of a value.
58func (o Opt[T]) IsOmitted() bool { return o == Opt[T]{} }
59
60func (o Opt[T]) MarshalJSON() ([]byte, error) {
61 if !o.IsPresent() {
62 return []byte("null"), nil
63 }
64 return json.Marshal(o.Value)
65}
66
67func (o *Opt[T]) UnmarshalJSON(data []byte) error {
68 if string(data) == "null" {
69 o.Status = null
70 return nil
71 }
72 return json.Unmarshal(data, &o.Value)
73}
74
75func (o Opt[T]) Or(v T) T {
76 if o.IsPresent() {
77 return o.Value
78 }
79 return v
80}
81
82func (o Opt[T]) String() string {
83 if o.IsNull() {
84 return "null"
85 }
86 if s, ok := any(o.Value).(fmt.Stringer); ok {
87 return s.String()
88 }
89 return fmt.Sprintf("%v", o.Value)
90}
91
92// This is a sketchy way to implement time Formatting
93var timeType = reflect.TypeOf(time.Time{})
94var timeTimeValueLoc, _ = reflect.TypeOf(Opt[time.Time]{}).FieldByName("Value")
95
96// Don't worry about this function, returns nil to fallback towards [MarshalJSON]
97func (o Opt[T]) MarshalJSONWithTimeLayout(format string) []byte {
98 t, ok := any(o.Value).(time.Time)
99 if !ok || o.IsNull() {
100 return nil
101 }
102
103 if format == "" {
104 format = time.RFC3339
105 } else if format == "date" {
106 format = "2006-01-02"
107 }
108
109 b, err := json.Marshal(t.Format(format))
110 if err != nil {
111 return nil
112 }
113 return b
114}
115