All checks were successful
continuous-integration/drone/push Build is passing
122 lines
3.0 KiB
Go
122 lines
3.0 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
/*
|
|
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)
|
|
|
|
log.Printf("PrintStructContents [%d] field '%s' (%T)\n", i, field, fieldType)
|
|
|
|
indent := strings.Repeat("\t", indentLevel)
|
|
result.WriteString(fmt.Sprintf("%s%s: ", indent, fieldType.Name))
|
|
log.Printf("%s%s: \n", indent, fieldType.Name)
|
|
|
|
switch field.Kind() {
|
|
case reflect.Struct:
|
|
result.WriteString("\n")
|
|
foo := PrintStructContents(field.Interface(), indentLevel+1)
|
|
log.Printf("foo: %s\n", foo)
|
|
result.WriteString(foo)
|
|
case reflect.Uint64:
|
|
log.Printf("uint64 %v\n", field.Interface())
|
|
result.WriteString(fmt.Sprintf("%v\n", field.Interface()))
|
|
default:
|
|
log.Printf("default %v\n", field.Interface())
|
|
result.WriteString(fmt.Sprintf("%v\n", field.Interface()))
|
|
}
|
|
}
|
|
|
|
log.Printf("PrintStructContents completed\n")
|
|
|
|
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-12-24 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 {
|
|
GetId() int
|
|
}
|
|
|
|
// AppendIfNotExists requires a struct to implement the GetId() method
|
|
// Then we can use this function to avoid creating duplicate entries in the slice
|
|
func AppendIfNotExists[T Identifiable](slice []T, element T) []T {
|
|
for _, existingElement := range slice {
|
|
if existingElement.GetId() == element.GetId() {
|
|
// Element with the same Id already exists, don't append
|
|
return slice
|
|
}
|
|
}
|
|
|
|
// Element with the same Id does not exist, append the new element
|
|
return append(slice, element)
|
|
}
|
|
|
|
// UpdateStruct updates the values in the destination struct with values from the source struct
|
|
func UpdateStruct(dest interface{}, src interface{}) {
|
|
destValue := reflect.ValueOf(dest).Elem()
|
|
srcValue := reflect.ValueOf(src).Elem()
|
|
|
|
destType := destValue.Type()
|
|
|
|
for i := 0; i < srcValue.NumField(); i++ {
|
|
srcField := srcValue.Field(i)
|
|
|
|
destField := destValue.FieldByName(destType.Field(i).Name)
|
|
if destField.IsValid() && destField.Type() == srcField.Type() && destField.CanSet() {
|
|
destField.Set(srcField)
|
|
}
|
|
}
|
|
}
|