mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-07-04 21:37:47 +02:00
feat: add output package with Formatter interface (#537)
* docs: add RFC-004 for CLI (cobra) and output design * feat: add output package with Formatter interface and BrowserData.Each * fix: golangci config array syntax + add output package tests * refactor: encapsulated Output as Writer, collect-then-write pattern * refactor: unified row type with reflection-based CSV/JSON output * fix: ProfileName empty guard, writeFile close error check, sync RFC-004
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package output
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// row wraps any entry with browser/profile context for output.
|
||||
type row struct {
|
||||
Browser string
|
||||
Profile string
|
||||
entry any
|
||||
}
|
||||
|
||||
func (r row) csvHeader() []string {
|
||||
return append([]string{"browser", "profile"}, structCSVHeader(r.entry)...)
|
||||
}
|
||||
|
||||
func (r row) csvRow() []string {
|
||||
return append([]string{r.Browser, r.Profile}, structCSVRow(r.entry)...)
|
||||
}
|
||||
|
||||
// MarshalJSON produces flat JSON with browser/profile followed by the entry's fields.
|
||||
// Uses reflect.StructOf to dynamically build a struct that json.Marshal handles natively,
|
||||
// avoiding manual JSON string concatenation.
|
||||
func (r row) MarshalJSON() ([]byte, error) {
|
||||
ev := reflect.ValueOf(r.entry)
|
||||
et := ev.Type()
|
||||
|
||||
fields := make([]reflect.StructField, 0, et.NumField()+2)
|
||||
fields = append(fields,
|
||||
reflect.StructField{Name: "Browser", Type: reflect.TypeOf(""), Tag: `json:"browser"`},
|
||||
reflect.StructField{Name: "Profile", Type: reflect.TypeOf(""), Tag: `json:"profile"`},
|
||||
)
|
||||
for i := 0; i < et.NumField(); i++ {
|
||||
fields = append(fields, et.Field(i))
|
||||
}
|
||||
|
||||
flat := reflect.New(reflect.StructOf(fields)).Elem()
|
||||
flat.Field(0).SetString(r.Browser)
|
||||
flat.Field(1).SetString(r.Profile)
|
||||
for i := 0; i < et.NumField(); i++ {
|
||||
flat.Field(i + 2).Set(ev.Field(i))
|
||||
}
|
||||
|
||||
return json.Marshal(flat.Interface())
|
||||
}
|
||||
Reference in New Issue
Block a user