test new PrintStructContents
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,11 +2,12 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
/*
|
||||||
func PrintStructContents(s interface{}, indentLevel int) string {
|
func PrintStructContents(s interface{}, indentLevel int) string {
|
||||||
var result strings.Builder
|
var result strings.Builder
|
||||||
|
|
||||||
@@ -47,6 +48,42 @@ func PrintStructContents(s interface{}, indentLevel int) string {
|
|||||||
|
|
||||||
return result.String()
|
return result.String()
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
func PrintStructContents(s interface{}, indentLevel int) string {
|
||||||
|
var result strings.Builder
|
||||||
|
|
||||||
|
val := reflect.ValueOf(s)
|
||||||
|
|
||||||
|
if val.Kind() == reflect.Ptr {
|
||||||
|
val = val.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
|
typ := val.Type()
|
||||||
|
|
||||||
|
for i := 0; i < val.NumField(); i++ {
|
||||||
|
field := val.Field(i)
|
||||||
|
fieldType := typ.Field(i)
|
||||||
|
|
||||||
|
indent := strings.Repeat("\t", indentLevel)
|
||||||
|
result.WriteString(fmt.Sprintf("%s%s: ", indent, fieldType.Name))
|
||||||
|
|
||||||
|
switch field.Kind() {
|
||||||
|
case reflect.Struct:
|
||||||
|
if fieldType.Type == reflect.TypeOf(time.Time{}) {
|
||||||
|
// Handle time.Time field
|
||||||
|
result.WriteString(fmt.Sprintf("%v\n", field.Interface().(time.Time).Format("2006-01-02 15:04:05")))
|
||||||
|
} else {
|
||||||
|
result.WriteString("\n")
|
||||||
|
result.WriteString(PrintStructContents(field.Interface(), indentLevel+1))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
result.WriteString(fmt.Sprintf("%v\n", field.Interface()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.String()
|
||||||
|
}
|
||||||
|
|
||||||
type Identifiable interface {
|
type Identifiable interface {
|
||||||
GetId() int
|
GetId() int
|
||||||
|
Reference in New Issue
Block a user