-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.go
136 lines (126 loc) · 3.66 KB
/
helper.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
// Copyright 2015 by Leipzig University Library, http://ub.uni-leipzig.de
// The Finc Authors, http://finc.info
// Martin Czygan, <martin.czygan@uni-leipzig.de>
//
// This file is part of some open source application.
//
// Some open source application is free software: you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// Some open source application is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Foobar. If not, see <http://www.gnu.org/licenses/>.
//
// @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
//
package assetutil
import (
"bufio"
"bytes"
"io"
"regexp"
"strings"
"github.com/segmentio/encoding/json"
"github.com/miku/span"
"github.com/miku/span/container"
)
// RegexpMapEntry maps a regex pattern to a string value.
type RegexpMapEntry struct {
Pattern *regexp.Regexp
Value string
}
// RegexpMap holds a list of entries, which contain pattern string pairs.
type RegexpMap struct {
Entries []RegexpMapEntry
}
// LookupDefault tries to match a given string against patterns. If none of the
// matterns match, return a given default.
func (r RegexpMap) LookupDefault(s, def string) string {
for _, entry := range r.Entries {
if entry.Pattern.MatchString(s) {
return entry.Value
}
}
return def
}
// MustLoadRegexpMap loads the content of a given asset path into a RegexpMap. It
// will panic, if the asset path is not found and if the patterns found in the
// file cannot be compiled.
func MustLoadRegexpMap(path string) RegexpMap {
b, err := span.Static.ReadFile(path)
if err != nil {
panic(err)
}
d := make(map[string]string)
err = json.Unmarshal(b, &d)
if err != nil {
panic(err)
}
remap := RegexpMap{}
for k, v := range d {
remap.Entries = append(remap.Entries, RegexpMapEntry{Pattern: regexp.MustCompile(k), Value: v})
}
return remap
}
// MustLoadStringSet load one or more paths containing lines into a string set.
func MustLoadStringSet(paths ...string) *container.StringSet {
s := container.NewStringSet()
for _, path := range paths {
b, err := span.Static.ReadFile(path)
if err != nil {
panic(err)
}
rdr := bufio.NewReader(bytes.NewReader(b))
for {
line, err := rdr.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
s.Add(line)
}
}
return s
}
// MustLoadStringMap loads a JSON file from an asset path and parses it into a
// container.StringMap. This function will panic, if the asset cannot be found
// or the JSON is erroneous.
func MustLoadStringMap(path string) container.MapDefault {
b, err := span.Static.ReadFile(path)
if err != nil {
panic(err)
}
d := make(map[string]string)
err = json.Unmarshal(b, &d)
if err != nil {
panic(err)
}
return container.MapDefault(d)
}
// MustLoadStringSliceMap loads a JSON file from an asset path and parses it into
// a container.StringSliceMap. This function will halt the world, if it is
// called with an invalid argument.
func MustLoadStringSliceMap(path string) container.MapSliceDefault {
b, err := span.Static.ReadFile(path)
if err != nil {
panic(err)
}
d := make(map[string][]string)
err = json.Unmarshal(b, &d)
if err != nil {
panic(err)
}
return container.MapSliceDefault(d)
}