blob: cebf10d6806aafdcd95da0c983025176534c976e [file] [log] [blame]
Akron57ee5582025-05-21 15:25:13 +02001package config
2
3import (
4 "fmt"
5 "os"
Akronf98ba282026-02-24 11:13:30 +01006 "strconv"
Akron57ee5582025-05-21 15:25:13 +02007
Akron2ef703c2025-07-03 15:57:42 +02008 "github.com/KorAP/Koral-Mapper/ast"
9 "github.com/KorAP/Koral-Mapper/parser"
Akron7e8da932025-07-01 11:56:46 +020010 "github.com/rs/zerolog/log"
Akron57ee5582025-05-21 15:25:13 +020011 "gopkg.in/yaml.v3"
12)
13
Akron06d21f02025-06-04 14:36:07 +020014const (
Akron2ac2ec02025-06-05 15:26:42 +020015 defaultServer = "https://korap.ids-mannheim.de/"
16 defaultSDK = "https://korap.ids-mannheim.de/js/korap-plugin-latest.js"
Akron43fb1022026-02-20 11:38:49 +010017 defaultStylesheet = "https://korap.ids-mannheim.de/css/kalamar-plugin-latest.css"
Akron2ef703c2025-07-03 15:57:42 +020018 defaultServiceURL = "https://korap.ids-mannheim.de/plugin/koralmapper"
Akron43fb1022026-02-20 11:38:49 +010019 defaultCookieName = "km-config"
Akron14c13a52025-06-06 15:36:23 +020020 defaultPort = 5725
Akron2ac2ec02025-06-05 15:26:42 +020021 defaultLogLevel = "warn"
Akron06d21f02025-06-04 14:36:07 +020022)
23
Akron57ee5582025-05-21 15:25:13 +020024// MappingRule represents a single mapping rule in the configuration
25type MappingRule string
26
27// MappingList represents a list of mapping rules with metadata
28type MappingList struct {
Akrondab27112025-06-05 13:52:43 +020029 ID string `yaml:"id"`
Akron2f93c582026-02-19 16:49:13 +010030 Type string `yaml:"type,omitempty"` // "annotation" (default) or "corpus"
Akrondab27112025-06-05 13:52:43 +020031 Description string `yaml:"desc,omitempty"`
32 FoundryA string `yaml:"foundryA,omitempty"`
33 LayerA string `yaml:"layerA,omitempty"`
34 FoundryB string `yaml:"foundryB,omitempty"`
35 LayerB string `yaml:"layerB,omitempty"`
Akrona67de8f2026-02-23 17:54:26 +010036 FieldA string `yaml:"fieldA,omitempty"`
37 FieldB string `yaml:"fieldB,omitempty"`
Akrondab27112025-06-05 13:52:43 +020038 Mappings []MappingRule `yaml:"mappings"`
Akron57ee5582025-05-21 15:25:13 +020039}
40
Akron2f93c582026-02-19 16:49:13 +010041// IsCorpus returns true if the mapping list type is "corpus".
42func (list *MappingList) IsCorpus() bool {
43 return list.Type == "corpus"
44}
45
46// ParseCorpusMappings parses all mapping rules as corpus rules.
Akrona67de8f2026-02-23 17:54:26 +010047// Bare values (without key=) are always allowed and receive the default
48// field name from the mapping list header (FieldA/FieldB) when set.
Akron2f93c582026-02-19 16:49:13 +010049func (list *MappingList) ParseCorpusMappings() ([]*parser.CorpusMappingResult, error) {
50 corpusParser := parser.NewCorpusParser()
Akrona67de8f2026-02-23 17:54:26 +010051 corpusParser.AllowBareValues = true
52
Akron2f93c582026-02-19 16:49:13 +010053 results := make([]*parser.CorpusMappingResult, len(list.Mappings))
54 for i, rule := range list.Mappings {
55 if rule == "" {
56 return nil, fmt.Errorf("empty corpus mapping rule at index %d in list '%s'", i, list.ID)
57 }
58 result, err := corpusParser.ParseMapping(string(rule))
59 if err != nil {
60 return nil, fmt.Errorf("failed to parse corpus mapping rule %d in list '%s': %w", i, list.ID, err)
61 }
Akrona67de8f2026-02-23 17:54:26 +010062
63 if list.FieldA != "" {
64 applyDefaultCorpusKey(result.Upper, list.FieldA)
65 }
66 if list.FieldB != "" {
67 applyDefaultCorpusKey(result.Lower, list.FieldB)
68 }
69
Akron2f93c582026-02-19 16:49:13 +010070 results[i] = result
71 }
72 return results, nil
73}
74
Akrona67de8f2026-02-23 17:54:26 +010075// applyDefaultCorpusKey recursively fills in empty keys on CorpusField nodes.
76func applyDefaultCorpusKey(node parser.CorpusNode, defaultKey string) {
77 switch n := node.(type) {
78 case *parser.CorpusField:
79 if n.Key == "" {
80 n.Key = defaultKey
81 }
82 case *parser.CorpusGroup:
83 for _, op := range n.Operands {
84 applyDefaultCorpusKey(op, defaultKey)
85 }
86 }
87}
88
Akron06d21f02025-06-04 14:36:07 +020089// MappingConfig represents the root configuration containing multiple mapping lists
90type MappingConfig struct {
Akron2ac2ec02025-06-05 15:26:42 +020091 SDK string `yaml:"sdk,omitempty"`
Akron43fb1022026-02-20 11:38:49 +010092 Stylesheet string `yaml:"stylesheet,omitempty"`
Akron2ac2ec02025-06-05 15:26:42 +020093 Server string `yaml:"server,omitempty"`
94 ServiceURL string `yaml:"serviceURL,omitempty"`
Akron43fb1022026-02-20 11:38:49 +010095 CookieName string `yaml:"cookieName,omitempty"`
Akron2ac2ec02025-06-05 15:26:42 +020096 Port int `yaml:"port,omitempty"`
97 LogLevel string `yaml:"loglevel,omitempty"`
98 Lists []MappingList `yaml:"lists,omitempty"`
Akron57ee5582025-05-21 15:25:13 +020099}
100
Akrone1cff7c2025-06-04 18:43:32 +0200101// LoadFromSources loads configuration from multiple sources and merges them:
102// - A main configuration file (optional) containing global settings and lists
103// - Individual mapping files (optional) containing single mapping lists each
104// At least one source must be provided
105func LoadFromSources(configFile string, mappingFiles []string) (*MappingConfig, error) {
106 var allLists []MappingList
107 var globalConfig MappingConfig
Akron57ee5582025-05-21 15:25:13 +0200108
Akrone1cff7c2025-06-04 18:43:32 +0200109 // Track seen IDs across all sources to detect duplicates
110 seenIDs := make(map[string]bool)
Akrona5d88142025-05-22 14:42:09 +0200111
Akrone1cff7c2025-06-04 18:43:32 +0200112 // Load main configuration file if provided
113 if configFile != "" {
114 data, err := os.ReadFile(configFile)
115 if err != nil {
116 return nil, fmt.Errorf("failed to read config file '%s': %w", configFile, err)
Akron06d21f02025-06-04 14:36:07 +0200117 }
Akrone1cff7c2025-06-04 18:43:32 +0200118
119 if len(data) == 0 {
120 return nil, fmt.Errorf("EOF: config file '%s' is empty", configFile)
121 }
122
123 // Try to unmarshal as new format first (object with optional sdk/server and lists)
Akron813780f2025-06-05 15:44:28 +0200124 if err := yaml.Unmarshal(data, &globalConfig); err == nil {
125 // Successfully parsed as new format - accept it regardless of whether it has lists
Akrone1cff7c2025-06-04 18:43:32 +0200126 for _, list := range globalConfig.Lists {
127 if seenIDs[list.ID] {
128 return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID)
129 }
130 seenIDs[list.ID] = true
131 }
132 allLists = append(allLists, globalConfig.Lists...)
133 } else {
134 // Fall back to old format (direct list)
135 var lists []MappingList
136 if err := yaml.Unmarshal(data, &lists); err != nil {
137 return nil, fmt.Errorf("failed to parse YAML config file '%s': %w", configFile, err)
138 }
139
140 for _, list := range lists {
141 if seenIDs[list.ID] {
142 return nil, fmt.Errorf("duplicate mapping list ID found: %s", list.ID)
143 }
144 seenIDs[list.ID] = true
145 }
146 allLists = append(allLists, lists...)
147 // Clear the lists from globalConfig since we got them from the old format
148 globalConfig.Lists = nil
149 }
Akron06d21f02025-06-04 14:36:07 +0200150 }
151
Akrone1cff7c2025-06-04 18:43:32 +0200152 // Load individual mapping files
153 for _, file := range mappingFiles {
154 data, err := os.ReadFile(file)
155 if err != nil {
Akron7e8da932025-07-01 11:56:46 +0200156 log.Error().Err(err).Str("file", file).Msg("Failed to read mapping file")
157 continue
Akrone1cff7c2025-06-04 18:43:32 +0200158 }
159
160 if len(data) == 0 {
Akron7e8da932025-07-01 11:56:46 +0200161 log.Error().Err(err).Str("file", file).Msg("EOF: mapping file is empty")
162 continue
Akrone1cff7c2025-06-04 18:43:32 +0200163 }
164
165 var list MappingList
166 if err := yaml.Unmarshal(data, &list); err != nil {
Akron7e8da932025-07-01 11:56:46 +0200167 log.Error().Err(err).Str("file", file).Msg("Failed to parse YAML mapping file")
168 continue
Akrone1cff7c2025-06-04 18:43:32 +0200169 }
170
171 if seenIDs[list.ID] {
Akron7e8da932025-07-01 11:56:46 +0200172 log.Error().Err(err).Str("file", file).Str("list-id", list.ID).Msg("Duplicate mapping list ID found")
173 continue
Akrone1cff7c2025-06-04 18:43:32 +0200174 }
175 seenIDs[list.ID] = true
176 allLists = append(allLists, list)
Akron57ee5582025-05-21 15:25:13 +0200177 }
178
Akrone1cff7c2025-06-04 18:43:32 +0200179 // Ensure we have at least some configuration
180 if len(allLists) == 0 {
181 return nil, fmt.Errorf("no mapping lists found: provide either a config file (-c) with lists or mapping files (-m)")
182 }
183
Akron585f50f2025-07-03 13:55:47 +0200184 // Validate all mapping lists (skip duplicate ID check since we already did it)
Akrone1cff7c2025-06-04 18:43:32 +0200185 if err := validateMappingLists(allLists); err != nil {
Akron06d21f02025-06-04 14:36:07 +0200186 return nil, err
187 }
188
Akrone1cff7c2025-06-04 18:43:32 +0200189 // Create final configuration
190 result := &MappingConfig{
Akron2ac2ec02025-06-05 15:26:42 +0200191 SDK: globalConfig.SDK,
Akron43fb1022026-02-20 11:38:49 +0100192 Stylesheet: globalConfig.Stylesheet,
Akron2ac2ec02025-06-05 15:26:42 +0200193 Server: globalConfig.Server,
194 ServiceURL: globalConfig.ServiceURL,
195 Port: globalConfig.Port,
196 LogLevel: globalConfig.LogLevel,
197 Lists: allLists,
Akrone1cff7c2025-06-04 18:43:32 +0200198 }
199
Akronf98ba282026-02-24 11:13:30 +0100200 // Apply environment variable overrides (ENV > config file)
201 ApplyEnvOverrides(result)
202
Akron06d21f02025-06-04 14:36:07 +0200203 // Apply defaults if not specified
Akron2ac2ec02025-06-05 15:26:42 +0200204 ApplyDefaults(result)
Akrone1cff7c2025-06-04 18:43:32 +0200205
206 return result, nil
207}
208
Akron585f50f2025-07-03 13:55:47 +0200209// ApplyDefaults sets default values for configuration fields if they are empty
Akron2ac2ec02025-06-05 15:26:42 +0200210func ApplyDefaults(config *MappingConfig) {
Akron585f50f2025-07-03 13:55:47 +0200211 defaults := map[*string]string{
212 &config.SDK: defaultSDK,
Akron43fb1022026-02-20 11:38:49 +0100213 &config.Stylesheet: defaultStylesheet,
Akron585f50f2025-07-03 13:55:47 +0200214 &config.Server: defaultServer,
215 &config.ServiceURL: defaultServiceURL,
Akron43fb1022026-02-20 11:38:49 +0100216 &config.CookieName: defaultCookieName,
Akron585f50f2025-07-03 13:55:47 +0200217 &config.LogLevel: defaultLogLevel,
Akron06d21f02025-06-04 14:36:07 +0200218 }
Akron585f50f2025-07-03 13:55:47 +0200219
220 for field, defaultValue := range defaults {
221 if *field == "" {
222 *field = defaultValue
223 }
Akron06d21f02025-06-04 14:36:07 +0200224 }
Akron585f50f2025-07-03 13:55:47 +0200225
Akrona8a66ce2025-06-05 10:50:17 +0200226 if config.Port == 0 {
227 config.Port = defaultPort
228 }
Akron06d21f02025-06-04 14:36:07 +0200229}
230
Akronf98ba282026-02-24 11:13:30 +0100231// ApplyEnvOverrides overrides configuration fields from environment variables.
232// All environment variables are uppercase and prefixed with KORAL_MAPPER_.
233// Non-empty environment values override any previously loaded config values.
234func ApplyEnvOverrides(config *MappingConfig) {
235 envMappings := map[string]*string{
236 "KORAL_MAPPER_SERVER": &config.Server,
237 "KORAL_MAPPER_SDK": &config.SDK,
238 "KORAL_MAPPER_STYLESHEET": &config.Stylesheet,
239 "KORAL_MAPPER_SERVICE_URL": &config.ServiceURL,
240 "KORAL_MAPPER_COOKIE_NAME": &config.CookieName,
241 "KORAL_MAPPER_LOG_LEVEL": &config.LogLevel,
242 }
243
244 for envKey, field := range envMappings {
245 if val := os.Getenv(envKey); val != "" {
246 *field = val
247 }
248 }
249
250 if val := os.Getenv("KORAL_MAPPER_PORT"); val != "" {
251 if port, err := strconv.Atoi(val); err == nil {
252 config.Port = port
253 }
254 }
255}
256
Akron585f50f2025-07-03 13:55:47 +0200257// validateMappingLists validates a slice of mapping lists (without duplicate ID checking)
Akron06d21f02025-06-04 14:36:07 +0200258func validateMappingLists(lists []MappingList) error {
Akron57ee5582025-05-21 15:25:13 +0200259 for i, list := range lists {
260 if list.ID == "" {
Akron06d21f02025-06-04 14:36:07 +0200261 return fmt.Errorf("mapping list at index %d is missing an ID", i)
Akron57ee5582025-05-21 15:25:13 +0200262 }
Akrona5d88142025-05-22 14:42:09 +0200263
Akron57ee5582025-05-21 15:25:13 +0200264 if len(list.Mappings) == 0 {
Akron06d21f02025-06-04 14:36:07 +0200265 return fmt.Errorf("mapping list '%s' has no mapping rules", list.ID)
Akron57ee5582025-05-21 15:25:13 +0200266 }
267
268 // Validate each mapping rule
269 for j, rule := range list.Mappings {
270 if rule == "" {
Akron06d21f02025-06-04 14:36:07 +0200271 return fmt.Errorf("mapping list '%s' rule at index %d is empty", list.ID, j)
Akron57ee5582025-05-21 15:25:13 +0200272 }
273 }
274 }
Akron06d21f02025-06-04 14:36:07 +0200275 return nil
Akron57ee5582025-05-21 15:25:13 +0200276}
277
278// ParseMappings parses all mapping rules in a list and returns a slice of parsed rules
279func (list *MappingList) ParseMappings() ([]*parser.MappingResult, error) {
280 // Create a grammar parser with the list's default foundries and layers
281 grammarParser, err := parser.NewGrammarParser("", "")
282 if err != nil {
283 return nil, fmt.Errorf("failed to create grammar parser: %w", err)
284 }
285
286 results := make([]*parser.MappingResult, len(list.Mappings))
287 for i, rule := range list.Mappings {
Akrona5d88142025-05-22 14:42:09 +0200288 // Check for empty rules first
289 if rule == "" {
290 return nil, fmt.Errorf("empty mapping rule at index %d in list '%s'", i, list.ID)
291 }
292
Akron57ee5582025-05-21 15:25:13 +0200293 // Parse the mapping rule
294 result, err := grammarParser.ParseMapping(string(rule))
295 if err != nil {
296 return nil, fmt.Errorf("failed to parse mapping rule %d in list '%s': %w", i, list.ID, err)
297 }
298
299 // Apply default foundries and layers if not specified in the rule
300 if list.FoundryA != "" {
301 applyDefaultFoundryAndLayer(result.Upper.Wrap, list.FoundryA, list.LayerA)
302 }
303 if list.FoundryB != "" {
304 applyDefaultFoundryAndLayer(result.Lower.Wrap, list.FoundryB, list.LayerB)
305 }
306
307 results[i] = result
308 }
309
310 return results, nil
311}
312
313// applyDefaultFoundryAndLayer recursively applies default foundry and layer to terms that don't have them specified
314func applyDefaultFoundryAndLayer(node ast.Node, defaultFoundry, defaultLayer string) {
315 switch n := node.(type) {
316 case *ast.Term:
Akron585f50f2025-07-03 13:55:47 +0200317 if n.Foundry == "" && defaultFoundry != "" {
Akron57ee5582025-05-21 15:25:13 +0200318 n.Foundry = defaultFoundry
319 }
Akron585f50f2025-07-03 13:55:47 +0200320 if n.Layer == "" && defaultLayer != "" {
Akron57ee5582025-05-21 15:25:13 +0200321 n.Layer = defaultLayer
322 }
323 case *ast.TermGroup:
324 for _, op := range n.Operands {
325 applyDefaultFoundryAndLayer(op, defaultFoundry, defaultLayer)
326 }
327 }
328}