Commit f74a6c3491763d749b2725a7bbba215af1b98511
1 parent
707782344f
Exists in
master
refactored
Showing
3 changed files
with
9 additions
and
98 deletions
Show diff stats
default_values.go
... | ... | @@ -1,58 +0,0 @@ |
1 | -package webutility | |
2 | - | |
3 | -// Int32ValueOrDefault ... | |
4 | -func Int32ValueOrDefault(val *interface{}) (def int32) { | |
5 | - if *val != nil { | |
6 | - def = (*val).(int32) | |
7 | - } | |
8 | - return def | |
9 | -} | |
10 | - | |
11 | -// Int64ValueOrDefault ... | |
12 | -func Int64ValueOrDefault(val *interface{}) (def int64) { | |
13 | - if *val != nil { | |
14 | - def = (*val).(int64) | |
15 | - } | |
16 | - return def | |
17 | -} | |
18 | - | |
19 | -// Uint32ValueOrDefault ... | |
20 | -func Uint32ValueOrDefault(val *interface{}) (def uint32) { | |
21 | - if *val != nil { | |
22 | - def = (*val).(uint32) | |
23 | - } | |
24 | - return def | |
25 | -} | |
26 | - | |
27 | -// Uint64ValueOrDefault ... | |
28 | -func Uint64ValueOrDefault(val *interface{}) (def uint64) { | |
29 | - if *val != nil { | |
30 | - def = (*val).(uint64) | |
31 | - } | |
32 | - return def | |
33 | -} | |
34 | - | |
35 | -// Float32ValueOrDefault ... | |
36 | -func Float32ValueOrDefault(val *interface{}) (def float32) { | |
37 | - if *val != nil { | |
38 | - def = (*val).(float32) | |
39 | - } | |
40 | - return def | |
41 | -} | |
42 | - | |
43 | -// Float64ValueOrDefault ... | |
44 | -func Float64ValueOrDefault(val *interface{}) (def float64) { | |
45 | - if *val != nil { | |
46 | - return (*val).(float64) | |
47 | - } | |
48 | - return def | |
49 | -} | |
50 | - | |
51 | -// StringValueOrDefault ... | |
52 | -func StringValueOrDefault(val *interface{}) (def string) { | |
53 | - if *val != nil { | |
54 | - def = (*val).(string) | |
55 | - } | |
56 | - return def | |
57 | -} | |
58 | - |
payload.go
... | ... | @@ -7,12 +7,12 @@ import ( |
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | 9 | "net/http" |
10 | - "os" | |
11 | 10 | "strings" |
12 | 11 | "sync" |
13 | 12 | "time" |
14 | 13 | |
15 | 14 | "git.to-net.rs/marko.tikvic/gologger" |
15 | + "git.to-net.rs/marko.tikvic/util" | |
16 | 16 | ) |
17 | 17 | |
18 | 18 | var ( |
... | ... | @@ -303,7 +303,7 @@ func initMetadata(project string) error { |
303 | 303 | // |
304 | 304 | // TODO(marko): Currently supports only one hardcoded language... |
305 | 305 | func LoadMetadataFromFile(path string) error { |
306 | - lines, err := ReadFileLines(path) | |
306 | + lines, err := util.ReadFileLines(path) | |
307 | 307 | if err != nil { |
308 | 308 | return err |
309 | 309 | } |
... | ... | @@ -313,11 +313,11 @@ func LoadMetadataFromFile(path string) error { |
313 | 313 | var name string |
314 | 314 | for i, l := range lines { |
315 | 315 | // skip empty lines |
316 | - if l = trimSpaces(l); len(l) == 0 { | |
316 | + if l = strings.TrimSpace(l); len(l) == 0 { | |
317 | 317 | continue |
318 | 318 | } |
319 | 319 | |
320 | - if isWrappedWith(l, "[", "]") { | |
320 | + if util.IsWrappedWith(l, "[", "]") { | |
321 | 321 | name = strings.Trim(l, "[]") |
322 | 322 | p := Payload{} |
323 | 323 | p.addLang("sr", make(map[string]string)) |
... | ... | @@ -334,8 +334,8 @@ func LoadMetadataFromFile(path string) error { |
334 | 334 | return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [invalid format] [%s]", i+1, l) |
335 | 335 | } |
336 | 336 | |
337 | - k := trimSpaces(parts[0]) | |
338 | - v := trimSpaces(parts[1]) | |
337 | + k := strings.TrimSpace(parts[0]) | |
338 | + v := strings.TrimSpace(parts[1]) | |
339 | 339 | if v != "-" { |
340 | 340 | metadata[name].Lang[0].FieldsLabels[k] = v |
341 | 341 | } |
... | ... | @@ -344,34 +344,6 @@ func LoadMetadataFromFile(path string) error { |
344 | 344 | return nil |
345 | 345 | } |
346 | 346 | |
347 | -func isWrappedWith(src, begin, end string) bool { | |
348 | - return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) | |
349 | -} | |
350 | - | |
351 | -func trimSpaces(s string) string { | |
352 | - return strings.TrimSpace(s) | |
353 | -} | |
354 | - | |
355 | -// ReadFileLines ... | |
356 | -// TODO(marko): Move to separate package | |
357 | -func ReadFileLines(path string) ([]string, error) { | |
358 | - f, err := os.Open(path) | |
359 | - if err != nil { | |
360 | - return nil, err | |
361 | - } | |
362 | - defer f.Close() | |
363 | - | |
364 | - var s strings.Builder | |
365 | - | |
366 | - if _, err = io.Copy(&s, f); err != nil { | |
367 | - return nil, err | |
368 | - } | |
369 | - | |
370 | - lines := strings.Split(s.String(), "\n") | |
371 | - | |
372 | - return lines, nil | |
373 | -} | |
374 | - | |
375 | 347 | func hotload(n int) { |
376 | 348 | entityScan := make(map[string]int64) |
377 | 349 | firstCheck := true | ... | ... |
string_sanitisation.go
1 | 1 | package webutility |
2 | 2 | |
3 | -import "strings" | |
3 | +import "git.to-net.rs/marko.tikvic/util" | |
4 | 4 | |
5 | 5 | const patern = "\"';&*<>=\\`:" |
6 | 6 | |
7 | 7 | // SanitiseString removes characters from s found in patern and returns new modified string. |
8 | -func SanitiseString(s string) (safe string) { | |
9 | - for _, c := range patern { | |
10 | - safe = strings.Replace(s, string(c), "", -1) | |
11 | - } | |
12 | - return safe | |
8 | +func SanitiseString(s string) string { | |
9 | + return util.ReplaceAny(s, patern, "") | |
13 | 10 | } | ... | ... |