From c4e256e69bdd78be45e015313f3919d40656c70c Mon Sep 17 00:00:00 2001 From: CalciumIon <1808837298@qq.com> Date: Sun, 22 Dec 2024 16:30:18 +0800 Subject: [PATCH] refactor: Update Message methods to use pointer receivers - Refactored ParseToolCalls, SetToolCalls, IsStringContent, and ParseContent methods in the Message struct to use pointer receivers, improving efficiency and consistency in handling mutable state. - Enhanced code readability and maintainability by ensuring all relevant methods operate on the pointer receiver, aligning with Go best practices. --- dto/openai_request.go | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/dto/openai_request.go b/dto/openai_request.go index 95f88812..4590e7b2 100644 --- a/dto/openai_request.go +++ b/dto/openai_request.go @@ -93,22 +93,6 @@ type Message struct { ToolCallId string `json:"tool_call_id,omitempty"` } -func (m Message) ParseToolCalls() []ToolCall { - if m.ToolCalls == nil { - return nil - } - var toolCalls []ToolCall - if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil { - return toolCalls - } - return toolCalls -} - -func (m *Message) SetToolCalls(toolCalls any) { - toolCallsJson, _ := json.Marshal(toolCalls) - m.ToolCalls = toolCallsJson -} - type MediaContent struct { Type string `json:"type"` Text string `json:"text"` @@ -132,7 +116,23 @@ const ( ContentTypeInputAudio = "input_audio" ) -func (m Message) StringContent() string { +func (m *Message) ParseToolCalls() []ToolCall { + if m.ToolCalls == nil { + return nil + } + var toolCalls []ToolCall + if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil { + return toolCalls + } + return toolCalls +} + +func (m *Message) SetToolCalls(toolCalls any) { + toolCallsJson, _ := json.Marshal(toolCalls) + m.ToolCalls = toolCallsJson +} + +func (m *Message) StringContent() string { var stringContent string if err := json.Unmarshal(m.Content, &stringContent); err == nil { return stringContent @@ -145,7 +145,7 @@ func (m *Message) SetStringContent(content string) { m.Content = jsonContent } -func (m Message) IsStringContent() bool { +func (m *Message) IsStringContent() bool { var stringContent string if err := json.Unmarshal(m.Content, &stringContent); err == nil { return true @@ -153,7 +153,7 @@ func (m Message) IsStringContent() bool { return false } -func (m Message) ParseContent() []MediaContent { +func (m *Message) ParseContent() []MediaContent { var contentList []MediaContent var stringContent string if err := json.Unmarshal(m.Content, &stringContent); err == nil {