-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
148 lines (116 loc) · 3.8 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package addrFmt
import (
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"strings"
)
const componentFileDelimiter = "---"
type template interface{}
type abbreviation map[string]map[string]string
type componentAlias struct {
componentName string
aliasOrderRank int
}
type ConfigFiles struct {
CountriesPath string
ComponentsPath string
StateCodesPath string
CountryToLangPath string
CountyCodesPath string
CountryCodesPath string
AbbreviationFiles string
}
type OutputFormat int
const (
Array OutputFormat = iota
OneLine OutputFormat = iota
PostalFormat OutputFormat = iota
)
type Config struct {
ComponentAliases map[string]componentAlias
Templates map[string]template
StateCodes map[string]map[string]interface{}
CountryToLang map[string]interface{}
CountyCodes map[string]map[string]interface{}
CountryCodes map[string]string
Abbreviations map[string]abbreviation
Abbreviate bool
UnknownAsAttention bool
OutputFormat OutputFormat
}
// LoadConfig parses the configuration files into a Config structure
func LoadConfig(configFiles ConfigFiles) *Config {
var config Config
config.ComponentAliases = getComponentsAliasesConfig(configFiles.ComponentsPath)
config.Abbreviations = loadAbbreviationConfig(configFiles.AbbreviationFiles)
config.CountryCodes = loadCountryCodesConfig(configFiles.CountryCodesPath)
loadConfig(configFiles.CountriesPath, &config.Templates)
loadConfig(configFiles.StateCodesPath, &config.StateCodes)
loadConfig(configFiles.CountryToLangPath, &config.CountryToLang)
loadConfig(configFiles.CountyCodesPath, &config.CountyCodes)
return &config
}
func getFileContent(path string) string {
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("Could not read %s: %v", path, err)
}
return string(content)
}
var fileContentRegExp = regexp.MustCompile(` #`)
func loadCountryCodesConfig(countryCodesPath string) map[string]string {
fileContent := getFileContent(countryCodesPath)
fileContent = fileContentRegExp.ReplaceAllString(fileContent, "")
var countryCodes map[string]string
err := yaml.Unmarshal([]byte(fileContent), &countryCodes)
if err != nil {
log.Fatalf("Could not load countries config file: %v", err)
}
return countryCodes
}
func getComponentsAliasesConfig(componentsPath string) map[string]componentAlias {
componentFileContent := getFileContent(componentsPath)
componentParts := strings.Split(componentFileContent, componentFileDelimiter)
componentAliases := make(map[string]componentAlias)
for _, componentPart := range componentParts {
var component struct {
Name string `yaml:"name"`
Aliases []string `yaml:"aliases"`
}
err := yaml.Unmarshal([]byte(componentPart), &component)
if err != nil {
log.Fatalf("Could not load components config file: %v", err)
}
if len(component.Aliases) > 0 {
for i, alias := range component.Aliases {
componentAliases[alias] = componentAlias{component.Name, i}
}
}
}
return componentAliases
}
func loadAbbreviationConfig(abbreviationPath string) map[string]abbreviation {
abbreviationFiles, err := filepath.Glob(abbreviationPath)
abbreviations := make(map[string]abbreviation, len(abbreviationFiles))
if err != nil {
log.Fatalf("Could not load abbreviation config file: %v", err)
}
for _, filePath := range abbreviationFiles {
var abbreviation abbreviation
loadConfig(filePath, &abbreviation)
fileBase := filepath.Base(filePath)
language := fileBase[0 : len(fileBase)-len(filepath.Ext(fileBase))]
abbreviations[language] = abbreviation
}
return abbreviations
}
func loadConfig(path string, config interface{}) {
fileContent := getFileContent(path)
err := yaml.Unmarshal([]byte(fileContent), config)
if err != nil {
log.Fatalf("Could not load %s: %v", path, err)
}
}