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 | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 16 | ) |
| 17 | |
Akron | a1a183f | 2025-05-26 17:47:33 +0200 | [diff] [blame] | 18 | // String converts the Direction to its string representation |
| 19 | func (d Direction) String() string { |
| 20 | if d { |
| 21 | return "atob" |
| 22 | } |
| 23 | return "btoa" |
| 24 | } |
| 25 | |
| 26 | // ParseDirection converts a string direction to Direction type |
| 27 | func ParseDirection(dir string) (Direction, error) { |
| 28 | switch dir { |
| 29 | case "atob": |
| 30 | return AtoB, nil |
| 31 | case "btoa": |
| 32 | return BtoA, nil |
| 33 | default: |
| 34 | return false, fmt.Errorf("invalid direction: %s", dir) |
| 35 | } |
| 36 | } |
| 37 | |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 38 | // Mapper handles the application of mapping rules to JSON objects |
| 39 | type Mapper struct { |
| 40 | mappingLists map[string]*config.MappingList |
| 41 | parsedRules map[string][]*parser.MappingResult |
| 42 | } |
| 43 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 44 | // NewMapper creates a new Mapper instance from a list of MappingLists |
| 45 | func NewMapper(lists []config.MappingList) (*Mapper, error) { |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 46 | m := &Mapper{ |
| 47 | mappingLists: make(map[string]*config.MappingList), |
| 48 | parsedRules: make(map[string][]*parser.MappingResult), |
| 49 | } |
| 50 | |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 51 | // Store mapping lists by ID |
| 52 | for _, list := range lists { |
| 53 | if _, exists := m.mappingLists[list.ID]; exists { |
| 54 | return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
| 55 | } |
| 56 | |
| 57 | // Create a copy of the list to store |
| 58 | listCopy := list |
| 59 | m.mappingLists[list.ID] = &listCopy |
| 60 | |
| 61 | // Parse the rules immediately |
| 62 | parsedRules, err := list.ParseMappings() |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 63 | if err != nil { |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 64 | return nil, fmt.Errorf("failed to parse mappings for list %s: %w", list.ID, err) |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 65 | } |
Akron | a00d475 | 2025-05-26 17:34:36 +0200 | [diff] [blame] | 66 | m.parsedRules[list.ID] = parsedRules |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return m, nil |
| 70 | } |
| 71 | |
| 72 | // MappingOptions contains the options for applying mappings |
| 73 | type MappingOptions struct { |
Akron | 0d9117c | 2025-05-27 15:20:21 +0200 | [diff] [blame] | 74 | FoundryA string |
| 75 | LayerA string |
| 76 | FoundryB string |
| 77 | LayerB string |
| 78 | Direction Direction |
| 79 | AddRewrites bool |
Akron | 32d53de | 2025-05-22 13:45:32 +0200 | [diff] [blame] | 80 | } |