Skip to content

Commit 3d70a7e

Browse files
committed
Merge function fix
1 parent ba5b0ef commit 3d70a7e

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

claims.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,31 @@ func MaxAgeMap(maxAge time.Duration, claims Map) {
296296
func Merge(values ...any) ([]byte, error) {
297297
parts := make([][]byte, 0, len(values))
298298

299-
for _, value := range values {
299+
for i, value := range values {
300300
if value == nil {
301301
continue
302302
}
303-
jsonBytes, err := json.Marshal(value)
303+
304+
var (
305+
jsonBytes []byte
306+
err error
307+
)
308+
309+
switch v := value.(type) {
310+
case string:
311+
// If the value is a string, treat it as a JSON object.
312+
jsonBytes = []byte(v)
313+
case []byte:
314+
// If the value is a byte slice, treat it as a JSON object.
315+
jsonBytes = v
316+
default:
317+
jsonBytes, err = json.Marshal(value)
318+
}
319+
304320
if err != nil {
305-
return nil, err
321+
return nil, fmt.Errorf("part: %d: %w", i+1, err)
306322
}
323+
307324
// Check that the marshaled JSON is an object.
308325
if len(jsonBytes) < 2 || jsonBytes[0] != '{' || jsonBytes[len(jsonBytes)-1] != '}' {
309326
return nil, fmt.Errorf("value does not marshal to a JSON object: %v", value)

0 commit comments

Comments
 (0)