blob: 20850934d9ef528f8e86caf17f9f631db1a7c2ea [file] [log] [blame]
Akron32d53de2025-05-22 13:45:32 +02001package mapper
2
3import (
Akron32d53de2025-05-22 13:45:32 +02004 "fmt"
5
Akron2ef703c2025-07-03 15:57:42 +02006 "github.com/KorAP/Koral-Mapper/config"
7 "github.com/KorAP/Koral-Mapper/parser"
Akron32d53de2025-05-22 13:45:32 +02008)
9
10// Direction represents the mapping direction (A to B or B to A)
Akrona1a183f2025-05-26 17:47:33 +020011type Direction bool
Akron32d53de2025-05-22 13:45:32 +020012
13const (
Akrona1a183f2025-05-26 17:47:33 +020014 AtoB Direction = true
15 BtoA Direction = false
Akron2f93c582026-02-19 16:49:13 +010016
17 RewriteEditor = "Koral-Mapper"
Akron32d53de2025-05-22 13:45:32 +020018)
19
Akrona1a183f2025-05-26 17:47:33 +020020// String converts the Direction to its string representation
21func (d Direction) String() string {
22 if d {
23 return "atob"
24 }
25 return "btoa"
26}
27
28// ParseDirection converts a string direction to Direction type
29func ParseDirection(dir string) (Direction, error) {
30 switch dir {
31 case "atob":
32 return AtoB, nil
33 case "btoa":
34 return BtoA, nil
35 default:
36 return false, fmt.Errorf("invalid direction: %s", dir)
37 }
38}
39
Akron32d53de2025-05-22 13:45:32 +020040// Mapper handles the application of mapping rules to JSON objects
41type Mapper struct {
Akron2f93c582026-02-19 16:49:13 +010042 mappingLists map[string]*config.MappingList
43 parsedQueryRules map[string][]*parser.MappingResult
44 parsedCorpusRules map[string][]*parser.CorpusMappingResult
Akron32d53de2025-05-22 13:45:32 +020045}
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{
Akron2f93c582026-02-19 16:49:13 +010050 mappingLists: make(map[string]*config.MappingList),
51 parsedQueryRules: make(map[string][]*parser.MappingResult),
52 parsedCorpusRules: make(map[string][]*parser.CorpusMappingResult),
Akron32d53de2025-05-22 13:45:32 +020053 }
54
Akrona00d4752025-05-26 17:34:36 +020055 // Store mapping lists by ID
56 for _, list := range lists {
57 if _, exists := m.mappingLists[list.ID]; exists {
58 return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID)
59 }
60
Akrona00d4752025-05-26 17:34:36 +020061 listCopy := list
62 m.mappingLists[list.ID] = &listCopy
63
Akron2f93c582026-02-19 16:49:13 +010064 if list.IsCorpus() {
65 corpusRules, err := list.ParseCorpusMappings()
66 if err != nil {
67 return nil, fmt.Errorf("failed to parse corpus mappings for list %s: %w", list.ID, err)
68 }
69 m.parsedCorpusRules[list.ID] = corpusRules
70 } else {
71 queryRules, err := list.ParseMappings()
72 if err != nil {
73 return nil, fmt.Errorf("failed to parse mappings for list %s: %w", list.ID, err)
74 }
75 m.parsedQueryRules[list.ID] = queryRules
Akron32d53de2025-05-22 13:45:32 +020076 }
Akron32d53de2025-05-22 13:45:32 +020077 }
78
79 return m, nil
80}
81
82// MappingOptions contains the options for applying mappings
83type MappingOptions struct {
Akron0d9117c2025-05-27 15:20:21 +020084 FoundryA string
85 LayerA string
86 FoundryB string
87 LayerB string
Akron41310262026-02-23 18:58:53 +010088 FieldA string
89 FieldB string
Akron0d9117c2025-05-27 15:20:21 +020090 Direction Direction
91 AddRewrites bool
Akron32d53de2025-05-22 13:45:32 +020092}
Akrone4f570d2026-02-20 08:18:06 +010093
94// CascadeQueryMappings applies multiple mapping lists sequentially,
95// feeding the output of each into the next. orderedIDs and
96// perMappingOpts must have the same length. An empty list returns
97// jsonData unchanged.
98func (m *Mapper) CascadeQueryMappings(orderedIDs []string, perMappingOpts []MappingOptions, jsonData any) (any, error) {
99 if len(orderedIDs) != len(perMappingOpts) {
100 return nil, fmt.Errorf("orderedIDs length (%d) must match perMappingOpts length (%d)", len(orderedIDs), len(perMappingOpts))
101 }
102
103 result := jsonData
104 for i, id := range orderedIDs {
105 var err error
106 result, err = m.ApplyQueryMappings(id, perMappingOpts[i], result)
107 if err != nil {
108 return nil, fmt.Errorf("cascade step %d (mapping %q): %w", i, id, err)
109 }
110 }
111 return result, nil
112}
113
114// CascadeResponseMappings applies multiple mapping lists sequentially
115// to a response object, feeding the output of each into the next.
116// orderedIDs and perMappingOpts must have the same length. An empty
117// list returns jsonData unchanged.
118func (m *Mapper) CascadeResponseMappings(orderedIDs []string, perMappingOpts []MappingOptions, jsonData any) (any, error) {
119 if len(orderedIDs) != len(perMappingOpts) {
120 return nil, fmt.Errorf("orderedIDs length (%d) must match perMappingOpts length (%d)", len(orderedIDs), len(perMappingOpts))
121 }
122
123 result := jsonData
124 for i, id := range orderedIDs {
125 var err error
126 result, err = m.ApplyResponseMappings(id, perMappingOpts[i], result)
127 if err != nil {
128 return nil, fmt.Errorf("cascade step %d (mapping %q): %w", i, id, err)
129 }
130 }
131 return result, nil
132}