Skip to content

Commit 35ea50b

Browse files
committed
fix: simplify marshal/unmarshal
Signed-off-by: Carlos Alexandro Becker <[email protected]>
1 parent dfa03c5 commit 35ea50b

File tree

2 files changed

+25
-49
lines changed

2 files changed

+25
-49
lines changed

content_json.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,8 @@ func (t *ToolResultContent) UnmarshalJSON(data []byte) error {
393393

394394
// MarshalJSON implements json.Marshaler for ToolResultOutputContentText.
395395
func (t ToolResultOutputContentText) MarshalJSON() ([]byte, error) {
396-
dataBytes, err := json.Marshal(struct {
397-
Text string `json:"text"`
398-
}{
399-
Text: t.Text,
400-
})
396+
type alias ToolResultOutputContentText
397+
dataBytes, err := json.Marshal(alias(t))
401398
if err != nil {
402399
return nil, err
403400
}
@@ -415,15 +412,14 @@ func (t *ToolResultOutputContentText) UnmarshalJSON(data []byte) error {
415412
return err
416413
}
417414

418-
var temp struct {
419-
Text string `json:"text"`
420-
}
415+
type alias ToolResultOutputContentText
416+
var temp alias
421417

422418
if err := json.Unmarshal(tr.Data, &temp); err != nil {
423419
return err
424420
}
425421

426-
t.Text = temp.Text
422+
*t = ToolResultOutputContentText(temp)
427423
return nil
428424
}
429425

@@ -470,13 +466,8 @@ func (t *ToolResultOutputContentError) UnmarshalJSON(data []byte) error {
470466

471467
// MarshalJSON implements json.Marshaler for ToolResultOutputContentMedia.
472468
func (t ToolResultOutputContentMedia) MarshalJSON() ([]byte, error) {
473-
dataBytes, err := json.Marshal(struct {
474-
Data string `json:"data"`
475-
MediaType string `json:"media_type"`
476-
}{
477-
Data: t.Data,
478-
MediaType: t.MediaType,
479-
})
469+
type alias ToolResultOutputContentMedia
470+
dataBytes, err := json.Marshal(alias(t))
480471
if err != nil {
481472
return nil, err
482473
}
@@ -494,17 +485,14 @@ func (t *ToolResultOutputContentMedia) UnmarshalJSON(data []byte) error {
494485
return err
495486
}
496487

497-
var temp struct {
498-
Data string `json:"data"`
499-
MediaType string `json:"media_type"`
500-
}
488+
type alias ToolResultOutputContentMedia
489+
var temp alias
501490

502491
if err := json.Unmarshal(tr.Data, &temp); err != nil {
503492
return err
504493
}
505494

506-
t.Data = temp.Data
507-
t.MediaType = temp.MediaType
495+
*t = ToolResultOutputContentMedia(temp)
508496
return nil
509497
}
510498

@@ -870,15 +858,8 @@ func (f *FunctionTool) UnmarshalJSON(data []byte) error {
870858

871859
// MarshalJSON implements json.Marshaler for ProviderDefinedTool.
872860
func (p ProviderDefinedTool) MarshalJSON() ([]byte, error) {
873-
dataBytes, err := json.Marshal(struct {
874-
ID string `json:"id"`
875-
Name string `json:"name"`
876-
Args map[string]any `json:"args"`
877-
}{
878-
ID: p.ID,
879-
Name: p.Name,
880-
Args: p.Args,
881-
})
861+
type alias ProviderDefinedTool
862+
dataBytes, err := json.Marshal(alias(p))
882863
if err != nil {
883864
return nil, err
884865
}
@@ -896,19 +877,14 @@ func (p *ProviderDefinedTool) UnmarshalJSON(data []byte) error {
896877
return err
897878
}
898879

899-
var aux struct {
900-
ID string `json:"id"`
901-
Name string `json:"name"`
902-
Args map[string]any `json:"args"`
903-
}
880+
type alias ProviderDefinedTool
881+
var aux alias
904882

905883
if err := json.Unmarshal(tj.Data, &aux); err != nil {
906884
return err
907885
}
908886

909-
p.ID = aux.ID
910-
p.Name = aux.Name
911-
p.Args = aux.Args
887+
*p = ProviderDefinedTool(aux)
912888

913889
return nil
914890
}

model_json.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,34 +104,34 @@ func (r *Response) UnmarshalJSON(data []byte) error {
104104

105105
// MarshalJSON implements json.Marshaler for StreamPart.
106106
func (s StreamPart) MarshalJSON() ([]byte, error) {
107-
type Alias StreamPart
107+
type alias StreamPart
108108
aux := struct {
109-
Alias
109+
alias
110110
Error string `json:"error,omitempty"`
111111
}{
112-
Alias: (Alias)(s),
112+
alias: (alias)(s),
113113
}
114-
114+
115115
// Marshal error to string
116116
if s.Error != nil {
117117
aux.Error = s.Error.Error()
118118
}
119-
119+
120120
// Clear the original Error field to avoid duplicate marshaling
121-
aux.Alias.Error = nil
122-
121+
aux.alias.Error = nil
122+
123123
return json.Marshal(aux)
124124
}
125125

126126
// UnmarshalJSON implements json.Unmarshaler for StreamPart.
127127
func (s *StreamPart) UnmarshalJSON(data []byte) error {
128-
type Alias StreamPart
128+
type alias StreamPart
129129
aux := struct {
130-
*Alias
130+
*alias
131131
Error string `json:"error"`
132132
ProviderMetadata map[string]json.RawMessage `json:"provider_metadata"`
133133
}{
134-
Alias: (*Alias)(s),
134+
alias: (*alias)(s),
135135
}
136136

137137
if err := json.Unmarshal(data, &aux); err != nil {

0 commit comments

Comments
 (0)