-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmappings.go
More file actions
275 lines (241 loc) · 7.91 KB
/
mappings.go
File metadata and controls
275 lines (241 loc) · 7.91 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package main
import (
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
yaml "gopkg.in/yaml.v3"
)
// ManualMapping represents a user-defined mapping between AniList and MAL IDs.
type ManualMapping struct {
AniListID int `yaml:"anilist_id"`
MALID int `yaml:"mal_id"`
Comment string `yaml:"comment,omitempty"`
}
// IgnoreEntry stores metadata for ignored entries (for comments).
type IgnoreEntry struct {
Title string
Reason string
}
// IgnoreConfig holds lists of entries to ignore during sync.
type IgnoreConfig struct {
AniListIDs []int `yaml:"anilist_ids,omitempty"`
MALIDs []int `yaml:"mal_ids,omitempty"`
Titles []string `yaml:"titles,omitempty"`
metadata map[int]IgnoreEntry // unexported, not in YAML (AniList IDs)
malMeta map[int]IgnoreEntry // unexported, not in YAML (MAL IDs)
}
// MappingsConfig holds user-editable mappings and ignore rules.
type MappingsConfig struct {
ManualMappings []ManualMapping `yaml:"manual_mappings,omitempty"`
Ignore IgnoreConfig `yaml:"ignore,omitempty"`
}
// LoadMappings loads mappings from YAML file. Creates empty file if it doesn't exist.
func LoadMappings(path string) (*MappingsConfig, error) {
if path == "" {
path = getDefaultMappingsPath()
}
data, err := os.ReadFile(path) // #nosec G304 - Path from user config
if err != nil {
if os.IsNotExist(err) {
return &MappingsConfig{}, nil
}
return nil, fmt.Errorf("read mappings file: %w", err)
}
var cfg MappingsConfig
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse mappings file: %w", err)
}
return &cfg, nil
}
// Save writes the mappings config to a YAML file.
func (m *MappingsConfig) Save(path string) error {
if path == "" {
path = getDefaultMappingsPath()
}
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0o750); err != nil { // #nosec G301 - Config directory
return fmt.Errorf("create mappings directory: %w", err)
}
file, err := os.Create(path) // #nosec G304 - Path from config
if err != nil {
return fmt.Errorf("create mappings file: %w", err)
}
defer func() { _ = file.Close() }() // #nosec ErrCheck - Error handling not needed for read-only file
encoder := yaml.NewEncoder(file)
encoder.SetIndent(2) // Match default yaml.v2 indent
if err := encoder.Encode(m); err != nil {
return fmt.Errorf("marshal mappings: %w", err)
}
return nil
}
// GetManualMALID returns the MAL ID for a given AniList ID from manual mappings.
func (m *MappingsConfig) GetManualMALID(anilistID int) (int, bool) {
for _, mapping := range m.ManualMappings {
if mapping.AniListID == anilistID {
return mapping.MALID, true
}
}
return 0, false
}
// GetManualAniListID returns the AniList ID for a given MAL ID from manual mappings.
func (m *MappingsConfig) GetManualAniListID(malID int) (int, bool) {
for _, mapping := range m.ManualMappings {
if mapping.MALID == malID {
return mapping.AniListID, true
}
}
return 0, false
}
// IsIgnored checks if an entry should be ignored by AniList ID or title.
func (m *MappingsConfig) IsIgnored(anilistID int, title string) bool {
if slices.Contains(m.Ignore.AniListIDs, anilistID) {
return true
}
lowerTitle := strings.ToLower(title)
for _, t := range m.Ignore.Titles {
if strings.ToLower(t) == lowerTitle {
return true
}
}
return false
}
// AddIgnoreByID adds an AniList ID to the ignore list with optional metadata.
func (m *MappingsConfig) AddIgnoreByID(anilistID int, title string, reason string) {
if slices.Contains(m.Ignore.AniListIDs, anilistID) {
return
}
m.Ignore.AniListIDs = append(m.Ignore.AniListIDs, anilistID)
if m.Ignore.metadata == nil {
m.Ignore.metadata = make(map[int]IgnoreEntry)
}
m.Ignore.metadata[anilistID] = IgnoreEntry{Title: title, Reason: reason}
}
// IsIgnoredByMALID checks if an entry should be ignored by MAL ID.
func (m *MappingsConfig) IsIgnoredByMALID(malID int) bool {
return slices.Contains(m.Ignore.MALIDs, malID)
}
// AddIgnoreByMALID adds a MAL ID to the ignore list with optional metadata.
func (m *MappingsConfig) AddIgnoreByMALID(malID int, title string, reason string) {
if slices.Contains(m.Ignore.MALIDs, malID) {
return
}
m.Ignore.MALIDs = append(m.Ignore.MALIDs, malID)
if m.Ignore.malMeta == nil {
m.Ignore.malMeta = make(map[int]IgnoreEntry)
}
m.Ignore.malMeta[malID] = IgnoreEntry{Title: title, Reason: reason}
}
// AddManualMapping adds a manual mapping if not already present.
func (m *MappingsConfig) AddManualMapping(anilistID, malID int, comment string) {
for i, mapping := range m.ManualMappings {
if mapping.AniListID == anilistID {
m.ManualMappings[i].MALID = malID
m.ManualMappings[i].Comment = comment
return
}
}
m.ManualMappings = append(m.ManualMappings, ManualMapping{
AniListID: anilistID,
MALID: malID,
Comment: comment,
})
}
func getDefaultMappingsPath() string {
configDir, err := os.UserConfigDir()
if err != nil {
return ""
}
return filepath.Join(configDir, "anilist-mal-sync", "mappings.yaml")
}
// buildIgnoreIDsNode creates a YAML sequence node for ignore IDs with inline comments.
func buildIgnoreIDsNode(ids []int, meta map[int]IgnoreEntry) *yaml.Node {
seqNode := &yaml.Node{Kind: yaml.SequenceNode}
for _, id := range ids {
idNode := &yaml.Node{Kind: yaml.ScalarNode, Value: strconv.Itoa(id)}
if meta, ok := meta[id]; ok {
var parts []string
if meta.Title != "" {
parts = append(parts, meta.Title)
}
if meta.Reason != "" {
parts = append(parts, meta.Reason)
}
if len(parts) > 0 {
idNode.LineComment = "# " + strings.Join(parts, " : ")
}
}
seqNode.Content = append(seqNode.Content, idNode)
}
return seqNode
}
// MarshalYAML implements custom YAML marshaling with inline comments.
func (m *MappingsConfig) MarshalYAML() (any, error) { //nolint:unparam // Error return required by yaml.Marshaler interface
node := &yaml.Node{
Kind: yaml.MappingNode,
}
// Handle manual_mappings (standard serialization)
if len(m.ManualMappings) > 0 {
manualMappingsNode := &yaml.Node{Kind: yaml.SequenceNode}
for _, mapping := range m.ManualMappings {
mappingNode := &yaml.Node{
Kind: yaml.MappingNode,
Content: []*yaml.Node{
{Kind: yaml.ScalarNode, Value: "anilist_id"},
{Kind: yaml.ScalarNode, Value: strconv.Itoa(mapping.AniListID)},
{Kind: yaml.ScalarNode, Value: "mal_id"},
{Kind: yaml.ScalarNode, Value: strconv.Itoa(mapping.MALID)},
},
}
if mapping.Comment != "" {
mappingNode.Content = append(mappingNode.Content,
[]*yaml.Node{
{Kind: yaml.ScalarNode, Value: "comment"},
{Kind: yaml.ScalarNode, Value: mapping.Comment},
}...,
)
}
manualMappingsNode.Content = append(manualMappingsNode.Content, mappingNode)
}
node.Content = append(node.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "manual_mappings"},
manualMappingsNode,
)
}
// Handle ignore section
hasIgnore := len(m.Ignore.AniListIDs) > 0 || len(m.Ignore.MALIDs) > 0 || len(m.Ignore.Titles) > 0
if hasIgnore {
ignoreNode := &yaml.Node{Kind: yaml.MappingNode}
if len(m.Ignore.AniListIDs) > 0 {
ignoreNode.Content = append(ignoreNode.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "anilist_ids"},
buildIgnoreIDsNode(m.Ignore.AniListIDs, m.Ignore.metadata),
)
}
if len(m.Ignore.MALIDs) > 0 {
ignoreNode.Content = append(ignoreNode.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "mal_ids"},
buildIgnoreIDsNode(m.Ignore.MALIDs, m.Ignore.malMeta),
)
}
if len(m.Ignore.Titles) > 0 {
titlesNode := &yaml.Node{Kind: yaml.SequenceNode}
for _, title := range m.Ignore.Titles {
titlesNode.Content = append(titlesNode.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: title},
)
}
ignoreNode.Content = append(ignoreNode.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "titles"},
titlesNode,
)
}
node.Content = append(node.Content,
&yaml.Node{Kind: yaml.ScalarNode, Value: "ignore"},
ignoreNode,
)
}
return node, nil
}