Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | |
Akron | fa55bb2 | 2025-05-26 15:10:42 +0200 | [diff] [blame] | 7 | "github.com/KorAP/KoralPipe-TermMapper/ast" |
| 8 | "github.com/KorAP/KoralPipe-TermMapper/parser" |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 9 | "gopkg.in/yaml.v3" |
| 10 | ) |
| 11 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 12 | const ( |
| 13 | defaultServer = "https://korap.ids-mannheim.de/" |
| 14 | defaultSDK = "https://korap.ids-mannheim.de/js/korap-plugin-latest.js" |
| 15 | ) |
| 16 | |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 17 | // MappingRule represents a single mapping rule in the configuration |
| 18 | type MappingRule string |
| 19 | |
| 20 | // MappingList represents a list of mapping rules with metadata |
| 21 | type MappingList struct { |
| 22 | ID string `yaml:"id"` |
| 23 | FoundryA string `yaml:"foundryA,omitempty"` |
| 24 | LayerA string `yaml:"layerA,omitempty"` |
| 25 | FoundryB string `yaml:"foundryB,omitempty"` |
| 26 | LayerB string `yaml:"layerB,omitempty"` |
| 27 | Mappings []MappingRule `yaml:"mappings"` |
| 28 | } |
| 29 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 30 | // MappingConfig represents the root configuration containing multiple mapping lists |
| 31 | type MappingConfig struct { |
| 32 | SDK string `yaml:"sdk,omitempty"` |
| 33 | Server string `yaml:"server,omitempty"` |
| 34 | Lists []MappingList `yaml:"lists,omitempty"` |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 35 | } |
| 36 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 37 | // LoadFromSources loads configuration from multiple sources and merges them: |
| 38 | // - A main configuration file (optional) containing global settings and lists |
| 39 | // - Individual mapping files (optional) containing single mapping lists each |
| 40 | // At least one source must be provided |
| 41 | func LoadFromSources(configFile string, mappingFiles []string) (*MappingConfig, error) { |
| 42 | var allLists []MappingList |
| 43 | var globalConfig MappingConfig |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 44 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 45 | // Track seen IDs across all sources to detect duplicates |
| 46 | seenIDs := make(map[string]bool) |
Akron | a5d8814 | 2025-05-22 14:42:09 +0200 | [diff] [blame] | 47 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 48 | // Load main configuration file if provided |
| 49 | if configFile != "" { |
| 50 | data, err := os.ReadFile(configFile) |
| 51 | if err != nil { |
| 52 | return nil, fmt.Errorf("failed to read config file '%s': %w", configFile, err) |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 53 | } |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 54 | |
| 55 | if len(data) == 0 { |
| 56 | return nil, fmt.Errorf("EOF: config file '%s' is empty", configFile) |
| 57 | } |
| 58 | |
| 59 | // Try to unmarshal as new format first (object with optional sdk/server and lists) |
| 60 | if err := yaml.Unmarshal(data, &globalConfig); err == nil && len(globalConfig.Lists) > 0 { |
| 61 | // Successfully parsed as new format with lists field |
| 62 | for _, list := range globalConfig.Lists { |
| 63 | if seenIDs[list.ID] { |
| 64 | return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
| 65 | } |
| 66 | seenIDs[list.ID] = true |
| 67 | } |
| 68 | allLists = append(allLists, globalConfig.Lists...) |
| 69 | } else { |
| 70 | // Fall back to old format (direct list) |
| 71 | var lists []MappingList |
| 72 | if err := yaml.Unmarshal(data, &lists); err != nil { |
| 73 | return nil, fmt.Errorf("failed to parse YAML config file '%s': %w", configFile, err) |
| 74 | } |
| 75 | |
| 76 | for _, list := range lists { |
| 77 | if seenIDs[list.ID] { |
| 78 | return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
| 79 | } |
| 80 | seenIDs[list.ID] = true |
| 81 | } |
| 82 | allLists = append(allLists, lists...) |
| 83 | // Clear the lists from globalConfig since we got them from the old format |
| 84 | globalConfig.Lists = nil |
| 85 | } |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 88 | // Load individual mapping files |
| 89 | for _, file := range mappingFiles { |
| 90 | data, err := os.ReadFile(file) |
| 91 | if err != nil { |
| 92 | return nil, fmt.Errorf("failed to read mapping file '%s': %w", file, err) |
| 93 | } |
| 94 | |
| 95 | if len(data) == 0 { |
| 96 | return nil, fmt.Errorf("EOF: mapping file '%s' is empty", file) |
| 97 | } |
| 98 | |
| 99 | var list MappingList |
| 100 | if err := yaml.Unmarshal(data, &list); err != nil { |
| 101 | return nil, fmt.Errorf("failed to parse YAML mapping file '%s': %w", file, err) |
| 102 | } |
| 103 | |
| 104 | if seenIDs[list.ID] { |
| 105 | return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
| 106 | } |
| 107 | seenIDs[list.ID] = true |
| 108 | allLists = append(allLists, list) |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 111 | // Ensure we have at least some configuration |
| 112 | if len(allLists) == 0 { |
| 113 | return nil, fmt.Errorf("no mapping lists found: provide either a config file (-c) with lists or mapping files (-m)") |
| 114 | } |
| 115 | |
| 116 | // Validate all mapping lists |
| 117 | if err := validateMappingLists(allLists); err != nil { |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 118 | return nil, err |
| 119 | } |
| 120 | |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 121 | // Create final configuration |
| 122 | result := &MappingConfig{ |
| 123 | SDK: globalConfig.SDK, |
| 124 | Server: globalConfig.Server, |
| 125 | Lists: allLists, |
| 126 | } |
| 127 | |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 128 | // Apply defaults if not specified |
Akron | e1cff7c | 2025-06-04 18:43:32 +0200 | [diff] [blame^] | 129 | applyDefaults(result) |
| 130 | |
| 131 | return result, nil |
| 132 | } |
| 133 | |
| 134 | // LoadConfig loads a YAML configuration file and returns a Config object |
| 135 | // Deprecated: Use LoadFromSources for new code |
| 136 | func LoadConfig(filename string) (*MappingConfig, error) { |
| 137 | return LoadFromSources(filename, nil) |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // applyDefaults sets default values for SDK and Server if they are empty |
| 141 | func applyDefaults(config *MappingConfig) { |
| 142 | if config.SDK == "" { |
| 143 | config.SDK = defaultSDK |
| 144 | } |
| 145 | if config.Server == "" { |
| 146 | config.Server = defaultServer |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // validateMappingLists validates a slice of mapping lists |
| 151 | func validateMappingLists(lists []MappingList) error { |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 152 | // Validate the configuration |
Akron | a5d8814 | 2025-05-22 14:42:09 +0200 | [diff] [blame] | 153 | seenIDs := make(map[string]bool) |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 154 | for i, list := range lists { |
| 155 | if list.ID == "" { |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 156 | return fmt.Errorf("mapping list at index %d is missing an ID", i) |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 157 | } |
Akron | a5d8814 | 2025-05-22 14:42:09 +0200 | [diff] [blame] | 158 | |
| 159 | // Check for duplicate IDs |
| 160 | if seenIDs[list.ID] { |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 161 | return fmt.Errorf("duplicate mapping list ID found: %s", list.ID) |
Akron | a5d8814 | 2025-05-22 14:42:09 +0200 | [diff] [blame] | 162 | } |
| 163 | seenIDs[list.ID] = true |
| 164 | |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 165 | if len(list.Mappings) == 0 { |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 166 | return fmt.Errorf("mapping list '%s' has no mapping rules", list.ID) |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Validate each mapping rule |
| 170 | for j, rule := range list.Mappings { |
| 171 | if rule == "" { |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 172 | return fmt.Errorf("mapping list '%s' rule at index %d is empty", list.ID, j) |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | } |
Akron | 06d21f0 | 2025-06-04 14:36:07 +0200 | [diff] [blame] | 176 | return nil |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // ParseMappings parses all mapping rules in a list and returns a slice of parsed rules |
| 180 | func (list *MappingList) ParseMappings() ([]*parser.MappingResult, error) { |
| 181 | // Create a grammar parser with the list's default foundries and layers |
| 182 | grammarParser, err := parser.NewGrammarParser("", "") |
| 183 | if err != nil { |
| 184 | return nil, fmt.Errorf("failed to create grammar parser: %w", err) |
| 185 | } |
| 186 | |
| 187 | results := make([]*parser.MappingResult, len(list.Mappings)) |
| 188 | for i, rule := range list.Mappings { |
Akron | a5d8814 | 2025-05-22 14:42:09 +0200 | [diff] [blame] | 189 | // Check for empty rules first |
| 190 | if rule == "" { |
| 191 | return nil, fmt.Errorf("empty mapping rule at index %d in list '%s'", i, list.ID) |
| 192 | } |
| 193 | |
Akron | 57ee558 | 2025-05-21 15:25:13 +0200 | [diff] [blame] | 194 | // Parse the mapping rule |
| 195 | result, err := grammarParser.ParseMapping(string(rule)) |
| 196 | if err != nil { |
| 197 | return nil, fmt.Errorf("failed to parse mapping rule %d in list '%s': %w", i, list.ID, err) |
| 198 | } |
| 199 | |
| 200 | // Apply default foundries and layers if not specified in the rule |
| 201 | if list.FoundryA != "" { |
| 202 | applyDefaultFoundryAndLayer(result.Upper.Wrap, list.FoundryA, list.LayerA) |
| 203 | } |
| 204 | if list.FoundryB != "" { |
| 205 | applyDefaultFoundryAndLayer(result.Lower.Wrap, list.FoundryB, list.LayerB) |
| 206 | } |
| 207 | |
| 208 | results[i] = result |
| 209 | } |
| 210 | |
| 211 | return results, nil |
| 212 | } |
| 213 | |
| 214 | // applyDefaultFoundryAndLayer recursively applies default foundry and layer to terms that don't have them specified |
| 215 | func applyDefaultFoundryAndLayer(node ast.Node, defaultFoundry, defaultLayer string) { |
| 216 | switch n := node.(type) { |
| 217 | case *ast.Term: |
| 218 | if n.Foundry == "" { |
| 219 | n.Foundry = defaultFoundry |
| 220 | } |
| 221 | if n.Layer == "" { |
| 222 | n.Layer = defaultLayer |
| 223 | } |
| 224 | case *ast.TermGroup: |
| 225 | for _, op := range n.Operands { |
| 226 | applyDefaultFoundryAndLayer(op, defaultFoundry, defaultLayer) |
| 227 | } |
| 228 | } |
| 229 | } |