-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
114 lines (96 loc) · 2.74 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package osrscache
import (
"encoding/json"
"fmt"
"image"
"image/png"
"os"
"path/filepath"
)
type JSONExportMode string
const (
JsonExportModeSingle JSONExportMode = "single"
JsonExportModeIndividual JSONExportMode = "individual"
)
type JSONExporter[K comparable, V any] struct {
definitions map[K]V
outputDir string
}
func NewJSONExporter[K comparable, V any](definitions map[K]V, outputDir string) *JSONExporter[K, V] {
return &JSONExporter[K, V]{
definitions: definitions,
outputDir: outputDir,
}
}
func (e *JSONExporter[K, V]) ExportAll(filename string) error {
data, err := json.MarshalIndent(e.definitions, "", " ")
if err != nil {
return fmt.Errorf("marshaling definitions: %w", err)
}
fullPath := filepath.Join(e.outputDir, filename+".json")
err = os.WriteFile(fullPath, data, 0644)
if err != nil {
return fmt.Errorf("writing file: %w", err)
}
return nil
}
func (e *JSONExporter[K, V]) ExportIndividual(prefix string) error {
for id, def := range e.definitions {
data, err := json.MarshalIndent(def, "", " ")
if err != nil {
return fmt.Errorf("marshaling definition %v: %w", id, err)
}
filename := fmt.Sprintf("%s_%v.json", prefix, id)
fullPath := filepath.Join(e.outputDir, filename)
err = os.WriteFile(fullPath, data, 0644)
if err != nil {
return fmt.Errorf("writing file %s: %w", filename, err)
}
}
return nil
}
func (e *JSONExporter[K, V]) ExportToJSON(mode JSONExportMode, filename string) error {
if err := os.MkdirAll(e.outputDir, os.ModePerm); err != nil {
return fmt.Errorf("creating output directory: %w", err)
}
switch mode {
case JsonExportModeSingle:
return e.ExportAll(filename)
case JsonExportModeIndividual:
return e.ExportIndividual(filename)
default:
return fmt.Errorf("invalid export mode: %s", mode)
}
}
type ImageExportable interface {
Image() *image.RGBA
}
type ImageExporter[K comparable, V ImageExportable] struct {
definitions map[K]V
outputDir string
}
func NewImageExporter[K comparable, V ImageExportable](definitions map[K]V, outputDir string) *ImageExporter[K, V] {
return &ImageExporter[K, V]{
definitions: definitions,
outputDir: outputDir,
}
}
func (e *ImageExporter[K, V]) ExportToImage(prefix string) error {
if err := os.MkdirAll(e.outputDir, os.ModePerm); err != nil {
return fmt.Errorf("creating output directory: %w", err)
}
for id, def := range e.definitions {
img := def.Image()
filename := fmt.Sprintf("%s_%v.png", prefix, id)
fullPath := filepath.Join(e.outputDir, filename)
f, err := os.Create(fullPath)
if err != nil {
return fmt.Errorf("creating file %s: %w", filename, err)
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
return fmt.Errorf("encoding image %s: %w", filename, err)
}
}
return nil
}