blob: d11ae01096f2b41383868a71ccd5e879cfd61102 [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
Akron32d53de2025-05-22 13:45:32 +020016)
17
Akrona1a183f2025-05-26 17:47:33 +020018// String converts the Direction to its string representation
19func (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
27func 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
Akron32d53de2025-05-22 13:45:32 +020038// Mapper handles the application of mapping rules to JSON objects
39type Mapper struct {
40 mappingLists map[string]*config.MappingList
41 parsedRules map[string][]*parser.MappingResult
42}
43
Akrona00d4752025-05-26 17:34:36 +020044// NewMapper creates a new Mapper instance from a list of MappingLists
45func NewMapper(lists []config.MappingList) (*Mapper, error) {
Akron32d53de2025-05-22 13:45:32 +020046 m := &Mapper{
47 mappingLists: make(map[string]*config.MappingList),
48 parsedRules: make(map[string][]*parser.MappingResult),
49 }
50
Akrona00d4752025-05-26 17:34:36 +020051 // 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()
Akron32d53de2025-05-22 13:45:32 +020063 if err != nil {
Akrona00d4752025-05-26 17:34:36 +020064 return nil, fmt.Errorf("failed to parse mappings for list %s: %w", list.ID, err)
Akron32d53de2025-05-22 13:45:32 +020065 }
Akrona00d4752025-05-26 17:34:36 +020066 m.parsedRules[list.ID] = parsedRules
Akron32d53de2025-05-22 13:45:32 +020067 }
68
69 return m, nil
70}
71
72// MappingOptions contains the options for applying mappings
73type MappingOptions struct {
Akron0d9117c2025-05-27 15:20:21 +020074 FoundryA string
75 LayerA string
76 FoundryB string
77 LayerB string
78 Direction Direction
79 AddRewrites bool
Akron32d53de2025-05-22 13:45:32 +020080}