blob: 0377c881095150bf5e9dda7b34fb15f90de1c338 [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
Akron8a87d9a2025-05-27 15:30:48 +0200226 // Check if the node types are different (structural change)
227 if originalNode.Type() != node.Type() {
228 // Full node replacement
229 originalBytes, err := parser.SerializeToJSON(originalNode)
230 if err != nil {
231 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
232 }
233 var originalJSON any
234 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
235 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
236 }
237 rewrite["original"] = originalJSON
238 } else if term, ok := originalNode.(*ast.Term); ok && ast.IsTermNode(node) {
239 // Check which attributes changed
240 newTerm := node.(*ast.Term)
241 if term.Foundry != newTerm.Foundry {
242 rewrite["scope"] = "foundry"
243 rewrite["original"] = term.Foundry
244 } else if term.Layer != newTerm.Layer {
245 rewrite["scope"] = "layer"
246 rewrite["original"] = term.Layer
247 } else if term.Key != newTerm.Key {
248 rewrite["scope"] = "key"
249 rewrite["original"] = term.Key
250 } else if term.Value != newTerm.Value {
251 rewrite["scope"] = "value"
252 rewrite["original"] = term.Value
253 } else {
254 // No specific attribute changed, use full node replacement
255 originalBytes, err := parser.SerializeToJSON(originalNode)
256 if err != nil {
257 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
Akron0d9117c2025-05-27 15:20:21 +0200258 }
Akron8a87d9a2025-05-27 15:30:48 +0200259 var originalJSON any
260 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
261 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
Akron0d9117c2025-05-27 15:20:21 +0200262 }
Akron8a87d9a2025-05-27 15:30:48 +0200263 rewrite["original"] = originalJSON
Akron0d9117c2025-05-27 15:20:21 +0200264 }
265 } else {
266 // Full node replacement
267 originalBytes, err := parser.SerializeToJSON(originalNode)
268 if err != nil {
269 return nil, fmt.Errorf("failed to serialize original node for rewrite: %w", err)
270 }
271 var originalJSON any
272 if err := json.Unmarshal(originalBytes, &originalJSON); err != nil {
273 return nil, fmt.Errorf("failed to parse original node JSON for rewrite: %w", err)
274 }
Akron8a87d9a2025-05-27 15:30:48 +0200275 rewrite["original"] = originalJSON
Akron0d9117c2025-05-27 15:20:21 +0200276 }
277
278 // Add rewrite to the node
279 if resultMap, ok := resultData.(map[string]any); ok {
280 if wrapMap, ok := resultMap["wrap"].(map[string]any); ok {
281 rewrites, exists := wrapMap["rewrites"]
282 if !exists {
283 rewrites = []any{}
284 }
285 if rewritesList, ok := rewrites.([]any); ok {
286 wrapMap["rewrites"] = append(rewritesList, rewrite)
287 } else {
288 wrapMap["rewrites"] = []any{rewrite}
289 }
290 }
291 }
292 }
293
Akroncc83eb52025-05-27 14:39:12 +0200294 // Restore rewrites if they existed
295 if oldRewrites != nil {
296 if resultMap, ok := resultData.(map[string]any); ok {
297 resultMap["rewrites"] = oldRewrites
298 }
299 }
300
Akron7b4984e2025-05-26 19:12:20 +0200301 // If we had a query wrapper, put the transformed data back in it
302 if hasQueryWrapper {
303 if wrapper, ok := jsonData.(map[string]any); ok {
304 wrapper["query"] = resultData
305 return wrapper, nil
306 }
307 }
308
Akron32d53de2025-05-22 13:45:32 +0200309 return resultData, nil
310}
311
Akron7b4984e2025-05-26 19:12:20 +0200312// isValidQueryObject checks if the query data is a valid object that can be processed
313func isValidQueryObject(data any) bool {
314 // Check if it's a map
315 queryMap, ok := data.(map[string]any)
316 if !ok {
317 return false
318 }
319
320 // Check if it has the required @type field
321 if _, ok := queryMap["@type"]; !ok {
322 return false
323 }
324
325 return true
326}
327
Akron32d53de2025-05-22 13:45:32 +0200328// applyFoundryAndLayerOverrides recursively applies foundry and layer overrides to terms
329func applyFoundryAndLayerOverrides(node ast.Node, foundry, layer string) {
330 if node == nil {
331 return
332 }
333
334 switch n := node.(type) {
335 case *ast.Term:
336 if foundry != "" {
337 n.Foundry = foundry
338 }
339 if layer != "" {
340 n.Layer = layer
341 }
342 case *ast.TermGroup:
343 for _, op := range n.Operands {
344 applyFoundryAndLayerOverrides(op, foundry, layer)
345 }
346 case *ast.Token:
347 if n.Wrap != nil {
348 applyFoundryAndLayerOverrides(n.Wrap, foundry, layer)
349 }
350 case *ast.CatchallNode:
351 if n.Wrap != nil {
352 applyFoundryAndLayerOverrides(n.Wrap, foundry, layer)
353 }
354 for _, op := range n.Operands {
355 applyFoundryAndLayerOverrides(op, foundry, layer)
356 }
357 }
358}