blob: 7d7b3db83044b64fc3a253aa91978c87eccc569b [file] [log] [blame]
Akron32d53de2025-05-22 13:45:32 +02001package mapper
2
3import (
4 "encoding/json"
5 "fmt"
6
Akronfa55bb22025-05-26 15:10:42 +02007 "github.com/KorAP/KoralPipe-TermMapper/ast"
8 "github.com/KorAP/KoralPipe-TermMapper/config"
9 "github.com/KorAP/KoralPipe-TermMapper/matcher"
10 "github.com/KorAP/KoralPipe-TermMapper/parser"
Akron32d53de2025-05-22 13:45:32 +020011)
12
13// Direction represents the mapping direction (A to B or B to A)
Akrona1a183f2025-05-26 17:47:33 +020014type Direction bool
Akron32d53de2025-05-22 13:45:32 +020015
16const (
Akrona1a183f2025-05-26 17:47:33 +020017 AtoB Direction = true
18 BtoA Direction = false
Akron32d53de2025-05-22 13:45:32 +020019)
20
Akrona1a183f2025-05-26 17:47:33 +020021// String converts the Direction to its string representation
22func (d Direction) String() string {
23 if d {
24 return "atob"
25 }
26 return "btoa"
27}
28
29// ParseDirection converts a string direction to Direction type
30func ParseDirection(dir string) (Direction, error) {
31 switch dir {
32 case "atob":
33 return AtoB, nil
34 case "btoa":
35 return BtoA, nil
36 default:
37 return false, fmt.Errorf("invalid direction: %s", dir)
38 }
39}
40
Akron32d53de2025-05-22 13:45:32 +020041// Mapper handles the application of mapping rules to JSON objects
42type Mapper struct {
43 mappingLists map[string]*config.MappingList
44 parsedRules map[string][]*parser.MappingResult
45}
46
Akrona00d4752025-05-26 17:34:36 +020047// NewMapper creates a new Mapper instance from a list of MappingLists
48func NewMapper(lists []config.MappingList) (*Mapper, error) {
Akron32d53de2025-05-22 13:45:32 +020049 m := &Mapper{
50 mappingLists: make(map[string]*config.MappingList),
51 parsedRules: make(map[string][]*parser.MappingResult),
52 }
53
Akrona00d4752025-05-26 17:34:36 +020054 // Store mapping lists by ID
55 for _, list := range lists {
56 if _, exists := m.mappingLists[list.ID]; exists {
57 return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID)
58 }
59
60 // Create a copy of the list to store
61 listCopy := list
62 m.mappingLists[list.ID] = &listCopy
63
64 // Parse the rules immediately
65 parsedRules, err := list.ParseMappings()
Akron32d53de2025-05-22 13:45:32 +020066 if err != nil {
Akrona00d4752025-05-26 17:34:36 +020067 return nil, fmt.Errorf("failed to parse mappings for list %s: %w", list.ID, err)
Akron32d53de2025-05-22 13:45:32 +020068 }
Akrona00d4752025-05-26 17:34:36 +020069 m.parsedRules[list.ID] = parsedRules
Akron32d53de2025-05-22 13:45:32 +020070 }
71
72 return m, nil
73}
74
75// MappingOptions contains the options for applying mappings
76type MappingOptions struct {
Akron0d9117c2025-05-27 15:20:21 +020077 FoundryA string
78 LayerA string
79 FoundryB string
80 LayerB string
81 Direction Direction
82 AddRewrites bool
Akron32d53de2025-05-22 13:45:32 +020083}
84
Akron7b4984e2025-05-26 19:12:20 +020085// ApplyQueryMappings applies the specified mapping rules to a JSON object
86func (m *Mapper) ApplyQueryMappings(mappingID string, opts MappingOptions, jsonData any) (any, error) {
Akron32d53de2025-05-22 13:45:32 +020087 // Validate mapping ID
88 if _, exists := m.mappingLists[mappingID]; !exists {
89 return nil, fmt.Errorf("mapping list with ID %s not found", mappingID)
90 }
91
Akron32d53de2025-05-22 13:45:32 +020092 // Get the parsed rules
93 rules := m.parsedRules[mappingID]
94
Akron7b4984e2025-05-26 19:12:20 +020095 // Check if we have a wrapper object with a "query" field
96 var queryData any
97 var hasQueryWrapper bool
98
99 if jsonMap, ok := jsonData.(map[string]any); ok {
100 if query, exists := jsonMap["query"]; exists {
101 queryData = query
102 hasQueryWrapper = true
103 }
104 }
105
106 // If no query wrapper was found, use the entire input
107 if !hasQueryWrapper {
108 // If the input itself is not a valid query object, return it as is
109 if !isValidQueryObject(jsonData) {
110 return jsonData, nil
111 }
112 queryData = jsonData
113 } else if queryData == nil || !isValidQueryObject(queryData) {
114 // If we have a query wrapper but the query is nil or not a valid object,
115 // return the original data
116 return jsonData, nil
117 }
118
Akroncc83eb52025-05-27 14:39:12 +0200119 // Store rewrites if they exist
120 var oldRewrites any
121 if queryMap, ok := queryData.(map[string]any); ok {
122 if rewrites, exists := queryMap["rewrites"]; exists {
123 oldRewrites = rewrites
124 delete(queryMap, "rewrites")
125 }
126 }
127
Akron32d53de2025-05-22 13:45:32 +0200128 // Convert input JSON to AST
Akron7b4984e2025-05-26 19:12:20 +0200129 jsonBytes, err := json.Marshal(queryData)
Akron32d53de2025-05-22 13:45:32 +0200130 if err != nil {
131 return nil, fmt.Errorf("failed to marshal input JSON: %w", err)
132 }
133
134 node, err := parser.ParseJSON(jsonBytes)
135 if err != nil {
136 return nil, fmt.Errorf("failed to parse JSON into AST: %w", err)
137 }
138
Akrond5850f82025-05-23 16:44:44 +0200139 // Store whether the input was a Token
140 isToken := false
141 var tokenWrap ast.Node
Akron32d53de2025-05-22 13:45:32 +0200142 if token, ok := node.(*ast.Token); ok {
Akrond5850f82025-05-23 16:44:44 +0200143 isToken = true
144 tokenWrap = token.Wrap
145 node = tokenWrap
Akron32d53de2025-05-22 13:45:32 +0200146 }
147
Akron0d9117c2025-05-27 15:20:21 +0200148 // Store original node for rewrite if needed
149 var originalNode ast.Node
150 if opts.AddRewrites {
151 originalBytes, err := parser.SerializeToJSON(node)
152 if err != nil {
153 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
154 }
155 originalNode, err = parser.ParseJSON(originalBytes)
156 if err != nil {
157 return nil, fmt.Errorf("failed to parse original node for rewrite: %w", err)
158 }
159 }
160
Akron32d53de2025-05-22 13:45:32 +0200161 // Apply each rule to the AST
162 for _, rule := range rules {
163 // Create pattern and replacement based on direction
164 var pattern, replacement ast.Node
Akrona1a183f2025-05-26 17:47:33 +0200165 if opts.Direction { // true means AtoB
Akron32d53de2025-05-22 13:45:32 +0200166 pattern = rule.Upper
167 replacement = rule.Lower
168 } else {
169 pattern = rule.Lower
170 replacement = rule.Upper
171 }
172
173 // Extract the inner nodes from the pattern and replacement tokens
174 if token, ok := pattern.(*ast.Token); ok {
175 pattern = token.Wrap
176 }
177 if token, ok := replacement.(*ast.Token); ok {
178 replacement = token.Wrap
179 }
180
181 // Apply foundry and layer overrides
Akrona1a183f2025-05-26 17:47:33 +0200182 if opts.Direction { // true means AtoB
Akron32d53de2025-05-22 13:45:32 +0200183 applyFoundryAndLayerOverrides(pattern, opts.FoundryA, opts.LayerA)
184 applyFoundryAndLayerOverrides(replacement, opts.FoundryB, opts.LayerB)
185 } else {
186 applyFoundryAndLayerOverrides(pattern, opts.FoundryB, opts.LayerB)
187 applyFoundryAndLayerOverrides(replacement, opts.FoundryA, opts.LayerA)
188 }
189
190 // Create matcher and apply replacement
Akrond5850f82025-05-23 16:44:44 +0200191 m, err := matcher.NewMatcher(ast.Pattern{Root: pattern}, ast.Replacement{Root: replacement})
192 if err != nil {
193 return nil, fmt.Errorf("failed to create matcher: %w", err)
194 }
Akron32d53de2025-05-22 13:45:32 +0200195 node = m.Replace(node)
196 }
197
Akrond5850f82025-05-23 16:44:44 +0200198 // Wrap the result in a token if the input was a token
199 var result ast.Node
200 if isToken {
201 result = &ast.Token{Wrap: node}
202 } else {
203 result = node
204 }
Akron32d53de2025-05-22 13:45:32 +0200205
206 // Convert AST back to JSON
207 resultBytes, err := parser.SerializeToJSON(result)
208 if err != nil {
209 return nil, fmt.Errorf("failed to serialize AST to JSON: %w", err)
210 }
211
Akron6f455152025-05-27 09:03:00 +0200212 // Parse the JSON string back into
213 var resultData any
Akron32d53de2025-05-22 13:45:32 +0200214 if err := json.Unmarshal(resultBytes, &resultData); err != nil {
215 return nil, fmt.Errorf("failed to parse result JSON: %w", err)
216 }
217
Akron0d9117c2025-05-27 15:20:21 +0200218 // Add rewrites if enabled and node was changed
219 if opts.AddRewrites && !ast.NodesEqual(node, originalNode) {
220 // Create rewrite object
221 rewrite := map[string]any{
222 "@type": "koral:rewrite",
223 "editor": "termMapper",
224 }
225
226 // Check if all terms in a group have their foundry changed
227 if term, ok := originalNode.(*ast.Term); ok {
228 if termGroup, ok := node.(*ast.TermGroup); ok {
229 // Check if all terms in the group have a different foundry
230 allFoundryChanged := true
231 for _, op := range termGroup.Operands {
232 if t, ok := op.(*ast.Term); ok {
233 if t.Foundry == term.Foundry {
234 allFoundryChanged = false
235 break
236 }
237 }
238 }
239 if allFoundryChanged {
240 rewrite["scope"] = "foundry"
241 rewrite["src"] = term.Foundry
242 } else {
243 // Full node replacement
244 originalBytes, err := parser.SerializeToJSON(originalNode)
245 if err != nil {
246 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
247 }
248 var originalJSON any
249 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
250 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
251 }
252 rewrite["src"] = originalJSON
253 }
254 } else if newTerm, ok := node.(*ast.Term); ok {
255 // Single term changes
256 if term.Foundry != newTerm.Foundry {
257 rewrite["scope"] = "foundry"
258 rewrite["src"] = term.Foundry
259 } else if term.Layer != newTerm.Layer {
260 rewrite["scope"] = "layer"
261 rewrite["src"] = term.Layer
262 } else if term.Key != newTerm.Key {
263 rewrite["scope"] = "key"
264 rewrite["src"] = term.Key
265 } else if term.Value != newTerm.Value {
266 rewrite["scope"] = "value"
267 rewrite["src"] = term.Value
268 } else {
269 // No specific attribute changed, use full node replacement
270 originalBytes, err := parser.SerializeToJSON(originalNode)
271 if err != nil {
272 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
273 }
274 var originalJSON any
275 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
276 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
277 }
278 rewrite["src"] = originalJSON
279 }
280 }
281 } else {
282 // Full node replacement
283 originalBytes, err := parser.SerializeToJSON(originalNode)
284 if err != nil {
285 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
286 }
287 var originalJSON any
288 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
289 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
290 }
291 rewrite["src"] = originalJSON
292 }
293
294 // Add rewrite to the node
295 if resultMap, ok := resultData.(map[string]any); ok {
296 if wrapMap, ok := resultMap["wrap"].(map[string]any); ok {
297 rewrites, exists := wrapMap["rewrites"]
298 if !exists {
299 rewrites = []any{}
300 }
301 if rewritesList, ok := rewrites.([]any); ok {
302 wrapMap["rewrites"] = append(rewritesList, rewrite)
303 } else {
304 wrapMap["rewrites"] = []any{rewrite}
305 }
306 }
307 }
308 }
309
Akroncc83eb52025-05-27 14:39:12 +0200310 // Restore rewrites if they existed
311 if oldRewrites != nil {
312 if resultMap, ok := resultData.(map[string]any); ok {
313 resultMap["rewrites"] = oldRewrites
314 }
315 }
316
Akron7b4984e2025-05-26 19:12:20 +0200317 // If we had a query wrapper, put the transformed data back in it
318 if hasQueryWrapper {
319 if wrapper, ok := jsonData.(map[string]any); ok {
320 wrapper["query"] = resultData
321 return wrapper, nil
322 }
323 }
324
Akron32d53de2025-05-22 13:45:32 +0200325 return resultData, nil
326}
327
Akron7b4984e2025-05-26 19:12:20 +0200328// isValidQueryObject checks if the query data is a valid object that can be processed
329func isValidQueryObject(data any) bool {
330 // Check if it's a map
331 queryMap, ok := data.(map[string]any)
332 if !ok {
333 return false
334 }
335
336 // Check if it has the required @type field
337 if _, ok := queryMap["@type"]; !ok {
338 return false
339 }
340
341 return true
342}
343
Akron32d53de2025-05-22 13:45:32 +0200344// applyFoundryAndLayerOverrides recursively applies foundry and layer overrides to terms
345func applyFoundryAndLayerOverrides(node ast.Node, foundry, layer string) {
346 if node == nil {
347 return
348 }
349
350 switch n := node.(type) {
351 case *ast.Term:
352 if foundry != "" {
353 n.Foundry = foundry
354 }
355 if layer != "" {
356 n.Layer = layer
357 }
358 case *ast.TermGroup:
359 for _, op := range n.Operands {
360 applyFoundryAndLayerOverrides(op, foundry, layer)
361 }
362 case *ast.Token:
363 if n.Wrap != nil {
364 applyFoundryAndLayerOverrides(n.Wrap, foundry, layer)
365 }
366 case *ast.CatchallNode:
367 if n.Wrap != nil {
368 applyFoundryAndLayerOverrides(n.Wrap, foundry, layer)
369 }
370 for _, op := range n.Operands {
371 applyFoundryAndLayerOverrides(op, foundry, layer)
372 }
373 }
374}