| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 1 | package mapper |
| 2 | |
| 3 | import ( |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 4 | "fmt" |
| 5 | |
| Akron | 2ef703c | 2025-07-03 15:57:42 +0200 | [diff] [blame] | 6 | "github.com/KorAP/Koral-Mapper/config" |
| 7 | "github.com/KorAP/Koral-Mapper/parser" |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Direction represents the mapping direction (A to B or B to A) |
| Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 11 | type Direction bool |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 12 | |
| 13 | const ( |
| Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 14 | AtoB Direction = true |
| 15 | BtoA Direction = false |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 16 | |
| 17 | RewriteEditor = "Koral-Mapper" |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 18 | ) |
| 19 | |
| Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 20 | // String converts the Direction to its string representation |
| 21 | func (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 |
| 29 | func 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 | |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 40 | // Mapper handles the application of mapping rules to JSON objects |
| 41 | type Mapper struct { |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 42 | mappingLists map[string]*config.MappingList |
| 43 | parsedQueryRules map[string][]*parser.MappingResult |
| 44 | parsedCorpusRules map[string][]*parser.CorpusMappingResult |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 47 | // NewMapper creates a new Mapper instance from a list of MappingLists |
| 48 | func NewMapper(lists []config.MappingList) (*Mapper, error) { |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 49 | m := &Mapper{ |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 50 | mappingLists: make(map[string]*config.MappingList), |
| 51 | parsedQueryRules: make(map[string][]*parser.MappingResult), |
| 52 | parsedCorpusRules: make(map[string][]*parser.CorpusMappingResult), |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 55 | // 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 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 61 | listCopy := list |
| 62 | m.mappingLists[list.ID] = &listCopy |
| 63 | |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 64 | 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 |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 76 | } |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return m, nil |
| 80 | } |
| 81 | |
| 82 | // MappingOptions contains the options for applying mappings |
| 83 | type MappingOptions struct { |
| Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 84 | FoundryA string |
| 85 | LayerA string |
| 86 | FoundryB string |
| 87 | LayerB string |
| Akron | 4131026 | 2026-02-23 18:58:53 +0100 | [diff] [blame] | 88 | FieldA string |
| 89 | FieldB string |
| Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 90 | Direction Direction |
| 91 | AddRewrites bool |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 92 | } |
| Akron | e4f570d | 2026-02-20 08:18:06 +0100 | [diff] [blame] | 93 | |
| 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. |
| 98 | func (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. |
| 118 | func (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 | } |