imap/export/json_exporter.go
H0llyW00dzZ ab122177c2 Improve [Client] Support Exporting Messages with various formats (#4)
- [+] feat(export): add message exporting functionality with JSON support
- [+] feat(readme): update documentation to include message export feature
- [+] refactor(.gitignore): change ignored files from emails.csv to test.csv and add test.json
- [+] refactor(client): update message handling to use map structure instead of MessageDetails struct

Reviewed-on: #4
Co-authored-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
Co-committed-by: H0llyW00dzZ <h0llyw00dzz@pm.me>
2025-01-24 07:08:59 +00:00

37 lines
1.0 KiB
Go

// Copyright (c) 2025 H0llyW00dzZ All rights reserved.
//
// By accessing or using this software, you agree to be bound by the terms
// of the License Agreement, which you can find at LICENSE files.
package export
import (
"encoding/json"
"fmt"
"io"
)
// JSONEncoderFunc defines a function type for custom JSON encoding
type JSONEncoderFunc func(v any, writer io.Writer) error
// JSONExporter implements the Exporter interface for JSON with a custom encoder
type JSONExporter struct {
Encoder JSONEncoderFunc
}
// Export writes messages to the writer using the custom JSON encoder
func (e *JSONExporter) Export(messages []map[string]any, writer io.Writer) error {
for _, msg := range messages {
if err := e.Encoder(msg, writer); err != nil {
return fmt.Errorf("failed to encode message to JSON: %v", err)
}
}
return nil
}
// DefaultJSONEncoder is the default JSON encoding function using the standard library
func DefaultJSONEncoder(v any, writer io.Writer) error {
encoder := json.NewEncoder(writer)
return encoder.Encode(v)
}