refactor(gperr): simplify JSON marshaling in withSubject by using slices package for cloning and reversing subjects

This commit is contained in:
yusing 2025-04-05 14:05:32 +08:00
parent fb217cf80e
commit 4d030d2e16

View file

@ -2,6 +2,7 @@ package gperr
import ( import (
"encoding/json" "encoding/json"
"slices"
"strings" "strings"
"github.com/yusing/go-proxy/internal/utils/strutils/ansi" "github.com/yusing/go-proxy/internal/utils/strutils/ansi"
@ -89,10 +90,8 @@ func (err *withSubject) Error() string {
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (err *withSubject) MarshalJSON() ([]byte, error) { func (err *withSubject) MarshalJSON() ([]byte, error) {
subjects := make([]string, len(err.Subjects)) subjects := slices.Clone(err.Subjects)
for i, s := range err.Subjects { slices.Reverse(subjects)
subjects[len(err.Subjects)-i-1] = s
}
reversed := map[string]any{ reversed := map[string]any{
"subjects": subjects, "subjects": subjects,