| 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 | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 20 | // newRewriteEntry creates a koral:rewrite annotation entry. |
| 21 | func newRewriteEntry(scope string, original any) map[string]any { |
| 22 | r := map[string]any{ |
| 23 | "@type": "koral:rewrite", |
| 24 | "editor": RewriteEditor, |
| 25 | } |
| 26 | if scope != "" { |
| 27 | r["scope"] = scope |
| 28 | } |
| 29 | if original != nil { |
| 30 | r["original"] = original |
| 31 | } |
| 32 | return r |
| 33 | } |
| 34 | |
| Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 35 | // String converts the Direction to its string representation |
| 36 | func (d Direction) String() string { |
| 37 | if d { |
| 38 | return "atob" |
| 39 | } |
| 40 | return "btoa" |
| 41 | } |
| 42 | |
| 43 | // ParseDirection converts a string direction to Direction type |
| 44 | func ParseDirection(dir string) (Direction, error) { |
| 45 | switch dir { |
| 46 | case "atob": |
| 47 | return AtoB, nil |
| 48 | case "btoa": |
| 49 | return BtoA, nil |
| 50 | default: |
| 51 | return false, fmt.Errorf("invalid direction: %s", dir) |
| 52 | } |
| 53 | } |
| 54 | |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 55 | // Mapper handles the application of mapping rules to JSON objects |
| 56 | type Mapper struct { |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 57 | mappingLists map[string]*config.MappingList |
| 58 | parsedQueryRules map[string][]*parser.MappingResult |
| 59 | parsedCorpusRules map[string][]*parser.CorpusMappingResult |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 62 | // NewMapper creates a new Mapper instance from a list of MappingLists |
| 63 | func NewMapper(lists []config.MappingList) (*Mapper, error) { |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 64 | m := &Mapper{ |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 65 | mappingLists: make(map[string]*config.MappingList), |
| 66 | parsedQueryRules: make(map[string][]*parser.MappingResult), |
| 67 | parsedCorpusRules: make(map[string][]*parser.CorpusMappingResult), |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 70 | // Store mapping lists by ID |
| 71 | for _, list := range lists { |
| 72 | if _, exists := m.mappingLists[list.ID]; exists { |
| 73 | return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
| 74 | } |
| 75 | |
| Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 76 | listCopy := list |
| 77 | m.mappingLists[list.ID] = &listCopy |
| 78 | |
| Akron | 2f93c58 | 2026-02-19 16:49:13 +0100 | [diff] [blame] | 79 | if list.IsCorpus() { |
| 80 | corpusRules, err := list.ParseCorpusMappings() |
| 81 | if err != nil { |
| 82 | return nil, fmt.Errorf("failed to parse corpus mappings for list %s: %w", list.ID, err) |
| 83 | } |
| 84 | m.parsedCorpusRules[list.ID] = corpusRules |
| 85 | } else { |
| 86 | queryRules, err := list.ParseMappings() |
| 87 | if err != nil { |
| 88 | return nil, fmt.Errorf("failed to parse mappings for list %s: %w", list.ID, err) |
| 89 | } |
| 90 | m.parsedQueryRules[list.ID] = queryRules |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 91 | } |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return m, nil |
| 95 | } |
| 96 | |
| 97 | // MappingOptions contains the options for applying mappings |
| 98 | type MappingOptions struct { |
| Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 99 | FoundryA string |
| 100 | LayerA string |
| 101 | FoundryB string |
| 102 | LayerB string |
| 103 | Direction Direction |
| 104 | AddRewrites bool |
| Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 105 | } |
| Akron | e4f570d | 2026-02-20 08:18:06 +0100 | [diff] [blame] | 106 | |
| 107 | // CascadeQueryMappings applies multiple mapping lists sequentially, |
| 108 | // feeding the output of each into the next. orderedIDs and |
| 109 | // perMappingOpts must have the same length. An empty list returns |
| 110 | // jsonData unchanged. |
| 111 | func (m *Mapper) CascadeQueryMappings(orderedIDs []string, perMappingOpts []MappingOptions, jsonData any) (any, error) { |
| 112 | if len(orderedIDs) != len(perMappingOpts) { |
| 113 | return nil, fmt.Errorf("orderedIDs length (%d) must match perMappingOpts length (%d)", len(orderedIDs), len(perMappingOpts)) |
| 114 | } |
| 115 | |
| 116 | result := jsonData |
| 117 | for i, id := range orderedIDs { |
| 118 | var err error |
| 119 | result, err = m.ApplyQueryMappings(id, perMappingOpts[i], result) |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("cascade step %d (mapping %q): %w", i, id, err) |
| 122 | } |
| 123 | } |
| 124 | return result, nil |
| 125 | } |
| 126 | |
| 127 | // CascadeResponseMappings applies multiple mapping lists sequentially |
| 128 | // to a response object, feeding the output of each into the next. |
| 129 | // orderedIDs and perMappingOpts must have the same length. An empty |
| 130 | // list returns jsonData unchanged. |
| 131 | func (m *Mapper) CascadeResponseMappings(orderedIDs []string, perMappingOpts []MappingOptions, jsonData any) (any, error) { |
| 132 | if len(orderedIDs) != len(perMappingOpts) { |
| 133 | return nil, fmt.Errorf("orderedIDs length (%d) must match perMappingOpts length (%d)", len(orderedIDs), len(perMappingOpts)) |
| 134 | } |
| 135 | |
| 136 | result := jsonData |
| 137 | for i, id := range orderedIDs { |
| 138 | var err error |
| 139 | result, err = m.ApplyResponseMappings(id, perMappingOpts[i], result) |
| 140 | if err != nil { |
| 141 | return nil, fmt.Errorf("cascade step %d (mapping %q): %w", i, id, err) |
| 142 | } |
| 143 | } |
| 144 | return result, nil |
| 145 | } |