package utils import ( "fmt" "log" "reflect" "strings" ) 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() } 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) }