Files
HackBrowserData/internal/export/reflect.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +08:00

83 lines
1.7 KiB
Go

package export
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
var timeType = reflect.TypeOf(time.Time{})
func structCSVHeader(value any) []string {
structType := reflect.Indirect(reflect.ValueOf(value)).Type()
headers := make([]string, 0, structType.NumField())
for i := 0; i < structType.NumField(); i++ {
name := tagName(structType.Field(i), "csv")
if name == "" {
continue
}
headers = append(headers, name)
}
return headers
}
func structCSVRow(value any) []string {
structValue := reflect.Indirect(reflect.ValueOf(value))
structType := structValue.Type()
row := make([]string, 0, structType.NumField())
for i := 0; i < structType.NumField(); i++ {
if tagName(structType.Field(i), "csv") == "" {
continue
}
row = append(row, fieldToString(structValue.Field(i)))
}
return row
}
func tagName(field reflect.StructField, key string) string {
tag, ok := field.Tag.Lookup(key)
if !ok || tag == "-" {
return ""
}
if index := strings.IndexByte(tag, ','); index != -1 {
tag = tag[:index]
}
if tag == "" {
return ""
}
return tag
}
func fieldToString(value reflect.Value) string {
if value.Type() == timeType {
timestamp, _ := value.Interface().(time.Time)
return formatTime(timestamp)
}
switch value.Kind() {
case reflect.String:
return value.String()
case reflect.Bool:
return formatBool(value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(value.Int(), 10)
default:
return fmt.Sprintf("%v", value.Interface())
}
}
func formatTime(timestamp time.Time) string {
if timestamp.IsZero() {
return ""
}
return timestamp.Format(time.RFC3339)
}
func formatBool(value bool) string {
if value {
return "true"
}
return "false"
}