Skip to content

Commit a29a010

Browse files
committed
Refactor: improve code
1 parent cc44c73 commit a29a010

1 file changed

Lines changed: 35 additions & 42 deletions

File tree

main.go

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ var (
3737
)
3838

3939
var (
40-
refMap = make(map[string]*List)
40+
refMap = make(map[string][]*Entry)
4141
plMap = make(map[string]*ParsedList)
42-
finalMap = make(map[string][]Entry)
42+
finalMap = make(map[string][]*Entry)
4343
cirIncMap = make(map[string]bool) // Used for circular inclusion detection
4444
)
4545

@@ -57,23 +57,18 @@ type Inclusion struct {
5757
BanAttrs []string
5858
}
5959

60-
type List struct {
61-
Name string
62-
Entry []Entry
63-
}
64-
6560
type ParsedList struct {
6661
Name string
67-
Inclusions []Inclusion
68-
Entry []Entry
62+
Inclusions []*Inclusion
63+
Entries []*Entry
6964
}
7065

71-
func makeProtoList(listName string, entries *[]Entry) (*router.GeoSite, error) {
66+
func makeProtoList(listName string, entries []*Entry) (*router.GeoSite, error) {
7267
site := &router.GeoSite{
7368
CountryCode: listName,
74-
Domain: make([]*router.Domain, 0, len(*entries)),
69+
Domain: make([]*router.Domain, 0, len(entries)),
7570
}
76-
for _, entry := range *entries {
71+
for _, entry := range entries {
7772
pdomain := &router.Domain{Value: entry.Value}
7873
for _, attr := range entry.Attrs {
7974
pdomain.Attribute = append(pdomain.Attribute, &router.Domain_Attribute{
@@ -175,21 +170,22 @@ func parseEntry(line string) (Entry, error) {
175170
return entry, nil
176171
}
177172

178-
func Load(path string) (*List, error) {
173+
func loadData(path string) error {
179174
file, err := os.Open(path)
180175
if err != nil {
181-
return nil, err
176+
return err
182177
}
183178
defer file.Close()
184179

185180
listName := strings.ToUpper(filepath.Base(path))
186181
if !SiteChecker.MatchString(listName) {
187-
return nil, fmt.Errorf("invalid list name: %s", listName)
182+
return fmt.Errorf("invalid list name: %s", listName)
188183
}
189-
list := &List{Name: listName}
190184
scanner := bufio.NewScanner(file)
185+
lineIdx := 0
191186
for scanner.Scan() {
192187
line := scanner.Text()
188+
lineIdx++
193189
// Remove comments
194190
if idx := strings.Index(line, "#"); idx != -1 {
195191
line = line[:idx]
@@ -200,26 +196,25 @@ func Load(path string) (*List, error) {
200196
}
201197
entry, err := parseEntry(line)
202198
if err != nil {
203-
return nil, err
199+
return fmt.Errorf("error in %s at line %d: %v", path, lineIdx, err)
204200
}
205-
list.Entry = append(list.Entry, entry)
201+
refMap[listName] = append(refMap[listName], &entry)
206202
}
207-
208-
return list, nil
203+
return nil
209204
}
210205

211-
func parseList(refList *List) error {
212-
pl, _ := plMap[refList.Name]
206+
func parseList(refName string, refList []*Entry) error {
207+
pl, _ := plMap[refName]
213208
if pl == nil {
214-
pl = &ParsedList{Name: refList.Name}
215-
plMap[refList.Name] = pl
209+
pl = &ParsedList{Name: refName}
210+
plMap[refName] = pl
216211
}
217-
for _, entry := range refList.Entry {
212+
for _, entry := range refList {
218213
if entry.Type == RuleTypeInclude {
219214
if len(entry.Affs) != 0 {
220215
return fmt.Errorf("affiliation is not allowed for include:%s", entry.Value)
221216
}
222-
inc := Inclusion{Source: strings.ToUpper(entry.Value)}
217+
inc := &Inclusion{Source: strings.ToUpper(entry.Value)}
223218
for _, attr := range entry.Attrs {
224219
if strings.HasPrefix(attr, "-") {
225220
inc.BanAttrs = append(inc.BanAttrs, attr[1:]) // Trim attribute prefix `-` character
@@ -235,17 +230,17 @@ func parseList(refList *List) error {
235230
apl = &ParsedList{Name: aff}
236231
plMap[aff] = apl
237232
}
238-
apl.Entry = append(apl.Entry, entry)
233+
apl.Entries = append(apl.Entries, entry)
239234
}
240-
pl.Entry = append(pl.Entry, entry)
235+
pl.Entries = append(pl.Entries, entry)
241236
}
242237
}
243238
return nil
244239
}
245240

246-
func polishList(roughMap *map[string]Entry) []Entry {
247-
finalList := make([]Entry, 0, len(*roughMap))
248-
queuingList := make([]Entry, 0, len(*roughMap)) // Domain/full entries without attr
241+
func polishList(roughMap *map[string]*Entry) []*Entry {
242+
finalList := make([]*Entry, 0, len(*roughMap))
243+
queuingList := make([]*Entry, 0, len(*roughMap)) // Domain/full entries without attr
249244
domainsMap := make(map[string]bool)
250245
for _, entry := range *roughMap {
251246
switch entry.Type { // Bypass regexp, keyword and "full/domain with attr"
@@ -287,7 +282,7 @@ func polishList(roughMap *map[string]Entry) []Entry {
287282
}
288283
}
289284
// Sort final entries
290-
slices.SortFunc(finalList, func(a, b Entry) int {
285+
slices.SortFunc(finalList, func(a, b *Entry) int {
291286
return strings.Compare(a.Plain, b.Plain)
292287
})
293288
return finalList
@@ -302,7 +297,7 @@ func resolveList(pl *ParsedList) error {
302297
cirIncMap[pl.Name] = true
303298
defer delete(cirIncMap, pl.Name)
304299

305-
isMatchAttrFilters := func(entry Entry, incFilter Inclusion) bool {
300+
isMatchAttrFilters := func(entry *Entry, incFilter *Inclusion) bool {
306301
if len(incFilter.MustAttrs) == 0 && len(incFilter.BanAttrs) == 0 { return true }
307302
if len(entry.Attrs) == 0 { return len(incFilter.MustAttrs) == 0 }
308303

@@ -315,8 +310,8 @@ func resolveList(pl *ParsedList) error {
315310
return true
316311
}
317312

318-
roughMap := make(map[string]Entry) // Avoid basic duplicates
319-
for _, dentry := range pl.Entry { // Add direct entries
313+
roughMap := make(map[string]*Entry) // Avoid basic duplicates
314+
for _, dentry := range pl.Entries { // Add direct entries
320315
roughMap[dentry.Plain] = dentry
321316
}
322317
for _, inc := range pl.Inclusions {
@@ -351,21 +346,19 @@ func main() {
351346
if info.IsDir() {
352347
return nil
353348
}
354-
list, err := Load(path)
355-
if err != nil {
349+
if err := loadData(path); err != nil {
356350
return err
357351
}
358-
refMap[list.Name] = list
359352
return nil
360353
})
361354
if err != nil {
362-
fmt.Println("Failed:", err)
355+
fmt.Println("Failed to loadData:", err)
363356
os.Exit(1)
364357
}
365358

366359
// Generate plMap
367-
for _, refList := range refMap {
368-
if err := parseList(refList); err != nil {
360+
for refName, refList := range refMap {
361+
if err := parseList(refName, refList); err != nil {
369362
fmt.Println("Failed to parseList:", err)
370363
os.Exit(1)
371364
}
@@ -402,7 +395,7 @@ func main() {
402395
// Generate dat file
403396
protoList := new(router.GeoSiteList)
404397
for siteName, siteEntries := range finalMap {
405-
site, err := makeProtoList(siteName, &siteEntries)
398+
site, err := makeProtoList(siteName, siteEntries)
406399
if err != nil {
407400
fmt.Println("Failed:", err)
408401
os.Exit(1)

0 commit comments

Comments
 (0)