This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion.go
120 lines (101 loc) · 2.69 KB
/
region.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
package regions
import (
"fmt"
"sort"
"strings"
yaml "gopkg.in/yaml.v3"
)
// Region hold the information about a region
type Region struct {
Codes map[string]string `yaml:",omitempty"` // ISO 3166-2 region code, EU identifier, etc
Names []*RegionName `yaml:",omitempty"`
}
// RemoveSource removes regions attributed to a source
//
// Matching is done based on a prefix, this allows to remove all sources from a
// given website.
//
// Language attributions are not removed, when the region exists in other sources
//
// When a region is attributed to multiple sources the region is retained and only
// the attribution to the removed source is removed.
func (r *Region) RemoveSource(source string) error {
delete(r.Codes, source)
for nid := 0; nid < len(r.Names); nid++ {
err := r.Names[nid].removeSource(source)
if err != nil {
return err
}
// Remove region name if no sources left
if r.Names[nid].Sources == nil || len(r.Names[nid].Sources) == 0 {
r.Names = append(r.Names[:nid], r.Names[nid+1:]...)
nid--
}
}
return nil
}
// Add a new region
func (r *Region) Add(regionName, language, source, nameType string) error {
normalizedName := removeMetaData(regionName)
if normalizedName == "" {
normalizedName = regionName
}
for _, n := range r.Names {
// If exists, check if source is listed, else add for reference
if strings.EqualFold(n.Name, normalizedName) {
return n.addSource(normalizedName, regionName, language, source, nameType)
}
}
// Add name if not present
rn := &RegionName{Name: normalizedName}
err := rn.addSource(normalizedName, regionName, language, source, nameType)
if err != nil {
return err
}
r.Names = append(r.Names, rn)
return nil
}
func (r *Region) sort() {
// Sort by name to preserver order in updates
sort.Slice(r.Names, func(i, j int) bool {
switch strings.Compare(r.Names[i].Name, r.Names[j].Name) {
case -1:
return true
case 1:
return false
}
return r.Names[i].Name > r.Names[j].Name
})
}
// String returns the region name in English or the first name when no English
// name is present.
func (r *Region) String() string {
for _, n := range r.Names {
for _, s := range n.Sources {
if s.Type != "" && s.Type != "primary" {
continue
}
for _, l := range s.Languages {
if l == "en" {
return n.Name
}
}
}
}
if len(r.Names) > 0 {
return r.Names[0].Name
}
return ""
}
// MarshalYAML is a custom mashaller to sort and restoring comments automatically
func (r *Region) MarshalYAML() (interface{}, error) {
r.sort()
type alias *Region
node := yaml.Node{}
err := node.Encode(alias(r))
if err != nil {
return nil, err
}
node.HeadComment = fmt.Sprintf("\n\n%s", r.String())
return node, nil
}