Skip to content

Commit 1ad38b2

Browse files
authored
refactor: remove unncessary unmarshaling (envoyproxy#1178)
**Description** Do not need to unmarshaling twice. --------- Signed-off-by: yxia216 <[email protected]>
1 parent f863e99 commit 1ad38b2

File tree

1 file changed

+19
-30
lines changed

1 file changed

+19
-30
lines changed

internal/apischema/openai/openai.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/anthropics/anthropic-sdk-go"
2121
"github.com/openai/openai-go/v2"
22+
"github.com/tidwall/gjson"
2223
"google.golang.org/genai"
2324

2425
"github.com/envoyproxy/ai-gateway/internal/apischema/awsbedrock"
@@ -142,15 +143,14 @@ type ChatCompletionContentPartUserUnionParam struct {
142143
}
143144

144145
func (c *ChatCompletionContentPartUserUnionParam) UnmarshalJSON(data []byte) error {
145-
var chatContentPart map[string]any
146-
if err := json.Unmarshal(data, &chatContentPart); err != nil {
147-
return err
148-
}
149-
var contentType string
150-
var ok bool
151-
if contentType, ok = chatContentPart["type"].(string); !ok {
152-
return fmt.Errorf("chat content does not have type")
146+
typeResult := gjson.GetBytes(data, "type")
147+
if !typeResult.Exists() {
148+
return errors.New("chat content does not have type")
153149
}
150+
151+
// Based on the 'type' field, unmarshal into the correct struct.
152+
contentType := typeResult.String()
153+
154154
switch contentType {
155155
case string(ChatCompletionContentPartTextTypeText):
156156
var textContent ChatCompletionContentPartTextParam
@@ -290,18 +290,14 @@ type ChatCompletionMessageParamUnion struct {
290290
}
291291

292292
func (c *ChatCompletionMessageParamUnion) UnmarshalJSON(data []byte) error {
293-
var chatMessage map[string]any
294-
if err := json.Unmarshal(data, &chatMessage); err != nil {
295-
return err
296-
}
297-
if _, ok := chatMessage["role"]; !ok {
298-
return fmt.Errorf("chat message does not have role")
299-
}
300-
var role string
301-
var ok bool
302-
if role, ok = chatMessage["role"].(string); !ok {
303-
return fmt.Errorf("chat message role is not string: %s", role)
293+
roleResult := gjson.GetBytes(data, "role")
294+
if !roleResult.Exists() {
295+
return errors.New("chat message does not have role")
304296
}
297+
298+
// Based on the 'role' field, unmarshal into the correct struct.
299+
role := roleResult.String()
300+
305301
switch role {
306302
case ChatMessageRoleUser:
307303
var userMessage ChatCompletionUserMessageParam
@@ -545,20 +541,13 @@ func (c ChatCompletionResponseFormatUnion) MarshalJSON() ([]byte, error) {
545541

546542
// UnmarshalJSON implements the json.Unmarshaler interface for ChatCompletionResponseFormatUnion.
547543
func (c *ChatCompletionResponseFormatUnion) UnmarshalJSON(data []byte) error {
548-
// First, peek at the "type" field to determine the concrete type.
549-
var responseFormatPart map[string]any
550-
if err := json.Unmarshal(data, &responseFormatPart); err != nil {
551-
return err
552-
}
553-
554-
var typeValue string
555-
var ok bool
556-
if typeValue, ok = responseFormatPart["type"].(string); !ok {
557-
return fmt.Errorf("response format does not have type")
544+
typeResult := gjson.GetBytes(data, "type")
545+
if !typeResult.Exists() {
546+
return errors.New("response format does not have type")
558547
}
559548

560549
// Based on the 'type' field, unmarshal into the correct struct.
561-
responseFormatType := ChatCompletionResponseFormatType(typeValue)
550+
responseFormatType := ChatCompletionResponseFormatType(typeResult.String())
562551

563552
switch responseFormatType {
564553
case ChatCompletionResponseFormatTypeText:

0 commit comments

Comments
 (0)