openai/openai-go
Publicmirrored from https://github.com/openai/openai-goAvailable
packages/param/option.go
114lines · modecode
| 1 | package param |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | func 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]. |
| 15 | func NullOpt[T comparable]() Opt[T] { return Opt[T]{Status: null} } |
| 16 | |
| 17 | type Opt[T comparable] struct { |
| 18 | Value T |
| 19 | // indicates whether the field should be omitted, null, or valid |
| 20 | Status Status |
| 21 | } |
| 22 | |
| 23 | type Status int8 |
| 24 | |
| 25 | const ( |
| 26 | omitted Status = iota |
| 27 | null |
| 28 | included |
| 29 | ) |
| 30 | |
| 31 | type 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 |
| 43 | func (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. |
| 52 | func (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. |
| 58 | func (o Opt[T]) IsOmitted() bool { return o == Opt[T]{} } |
| 59 | |
| 60 | func (o Opt[T]) MarshalJSON() ([]byte, error) { |
| 61 | if !o.IsPresent() { |
| 62 | return []byte("null"), nil |
| 63 | } |
| 64 | return json.Marshal(o.Value) |
| 65 | } |
| 66 | |
| 67 | func (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 | |
| 75 | func (o Opt[T]) Or(v T) T { |
| 76 | if o.IsPresent() { |
| 77 | return o.Value |
| 78 | } |
| 79 | return v |
| 80 | } |
| 81 | |
| 82 | func (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 |
| 93 | var timeType = reflect.TypeOf(time.Time{}) |
| 94 | var timeTimeValueLoc, _ = reflect.TypeOf(Opt[time.Time]{}).FieldByName("Value") |
| 95 | |
| 96 | // Don't worry about this function, returns nil to fallback towards [MarshalJSON] |
| 97 | func (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 | |