mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-20 11:11:26 +00:00
🔨 Add 'PrettyJSONMarshal' function
This commit is contained in:
parent
b9dd14a588
commit
0fe782c768
@ -6,49 +6,77 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MarshalJSON(v any, maxSliceSize uint) string {
|
type TruncatedMarshaller struct {
|
||||||
rv := reflect.ValueOf(v)
|
Value interface{}
|
||||||
if rv.Kind() == reflect.Slice && rv.Len() == 0 {
|
MaxItems int
|
||||||
return "[]"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data, err := json.MarshalIndent(truncateLists(v, int(maxSliceSize)), "", " ")
|
func (t TruncatedMarshaller) MarshalJSON() ([]byte, error) {
|
||||||
if err != nil {
|
val := reflect.ValueOf(t.Value)
|
||||||
return "{}"
|
|
||||||
|
if val.Kind() != reflect.Slice && val.Kind() != reflect.Array {
|
||||||
|
return json.Marshal(t.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(data)
|
length := val.Len()
|
||||||
|
if length <= t.MaxItems {
|
||||||
|
return json.Marshal(t.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncateLists(v interface{}, maxItems int) interface{} {
|
truncated := make([]interface{}, t.MaxItems+1)
|
||||||
rv := reflect.ValueOf(v)
|
|
||||||
|
|
||||||
switch rv.Kind() {
|
for i := 0; i < t.MaxItems; i++ {
|
||||||
case reflect.Slice, reflect.Array:
|
truncated[i] = val.Index(i).Interface()
|
||||||
if rv.Len() > maxItems {
|
|
||||||
newSlice := reflect.MakeSlice(rv.Type(), maxItems, maxItems)
|
|
||||||
reflect.Copy(newSlice, rv.Slice(0, maxItems))
|
|
||||||
newSlice = reflect.Append(newSlice, reflect.ValueOf(fmt.Sprintf("...(%d more)", rv.Len()-maxItems)))
|
|
||||||
return newSlice.Interface()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remaining := length - t.MaxItems
|
||||||
|
truncated[t.MaxItems] = fmt.Sprintf("+%d", remaining)
|
||||||
|
|
||||||
|
return json.Marshal(truncated)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrettyJSONMarshal(v interface{}, maxItems int, prefix, indent string) []byte {
|
||||||
|
truncated := processValue(v, maxItems)
|
||||||
|
d, _ := json.MarshalIndent(truncated, prefix, indent)
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
func processValue(v interface{}, maxItems int) interface{} {
|
||||||
|
val := reflect.ValueOf(v)
|
||||||
|
|
||||||
|
switch val.Kind() {
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
newMap := reflect.MakeMap(rv.Type())
|
newMap := make(map[string]interface{})
|
||||||
for _, key := range rv.MapKeys() {
|
iter := val.MapRange()
|
||||||
newMap.SetMapIndex(key, reflect.ValueOf(truncateLists(rv.MapIndex(key).Interface(), maxItems)))
|
for iter.Next() {
|
||||||
}
|
k := iter.Key().String()
|
||||||
return newMap.Interface()
|
newMap[k] = processValue(iter.Value().Interface(), maxItems)
|
||||||
case reflect.Struct:
|
|
||||||
newStruct := reflect.New(rv.Type()).Elem()
|
|
||||||
for i := 0; i < rv.NumField(); i++ {
|
|
||||||
newStruct.Field(i).Set(reflect.ValueOf(truncateLists(rv.Field(i).Interface(), maxItems)))
|
|
||||||
}
|
|
||||||
return newStruct.Interface()
|
|
||||||
case reflect.Ptr:
|
|
||||||
if rv.IsNil() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return truncateLists(rv.Elem().Interface(), maxItems)
|
|
||||||
}
|
}
|
||||||
|
return newMap
|
||||||
|
|
||||||
|
case reflect.Slice, reflect.Array:
|
||||||
|
return TruncatedMarshaller{Value: v, MaxItems: maxItems}
|
||||||
|
|
||||||
|
case reflect.Struct:
|
||||||
|
newMap := make(map[string]interface{})
|
||||||
|
t := val.Type()
|
||||||
|
for i := 0; i < t.NumField(); i++ {
|
||||||
|
field := t.Field(i)
|
||||||
|
if field.IsExported() {
|
||||||
|
jsonTag := field.Tag.Get("json")
|
||||||
|
if jsonTag == "-" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fieldName := field.Name
|
||||||
|
if jsonTag != "" {
|
||||||
|
fieldName = jsonTag
|
||||||
|
}
|
||||||
|
newMap[fieldName] = processValue(val.Field(i).Interface(), maxItems)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newMap
|
||||||
|
|
||||||
|
default:
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user