diff --git a/default_values.go b/default_values.go deleted file mode 100644 index f74c6cc..0000000 --- a/default_values.go +++ /dev/null @@ -1,58 +0,0 @@ -package webutility - -// Int32ValueOrDefault ... -func Int32ValueOrDefault(val *interface{}) (def int32) { - if *val != nil { - def = (*val).(int32) - } - return def -} - -// Int64ValueOrDefault ... -func Int64ValueOrDefault(val *interface{}) (def int64) { - if *val != nil { - def = (*val).(int64) - } - return def -} - -// Uint32ValueOrDefault ... -func Uint32ValueOrDefault(val *interface{}) (def uint32) { - if *val != nil { - def = (*val).(uint32) - } - return def -} - -// Uint64ValueOrDefault ... -func Uint64ValueOrDefault(val *interface{}) (def uint64) { - if *val != nil { - def = (*val).(uint64) - } - return def -} - -// Float32ValueOrDefault ... -func Float32ValueOrDefault(val *interface{}) (def float32) { - if *val != nil { - def = (*val).(float32) - } - return def -} - -// Float64ValueOrDefault ... -func Float64ValueOrDefault(val *interface{}) (def float64) { - if *val != nil { - return (*val).(float64) - } - return def -} - -// StringValueOrDefault ... -func StringValueOrDefault(val *interface{}) (def string) { - if *val != nil { - def = (*val).(string) - } - return def -} - diff --git a/payload.go b/payload.go index 9978586..e7cf773 100644 --- a/payload.go +++ b/payload.go @@ -7,12 +7,12 @@ import ( "fmt" "io" "net/http" - "os" "strings" "sync" "time" "git.to-net.rs/marko.tikvic/gologger" + "git.to-net.rs/marko.tikvic/util" ) var ( @@ -303,7 +303,7 @@ func initMetadata(project string) error { // // TODO(marko): Currently supports only one hardcoded language... func LoadMetadataFromFile(path string) error { - lines, err := ReadFileLines(path) + lines, err := util.ReadFileLines(path) if err != nil { return err } @@ -313,11 +313,11 @@ func LoadMetadataFromFile(path string) error { var name string for i, l := range lines { // skip empty lines - if l = trimSpaces(l); len(l) == 0 { + if l = strings.TrimSpace(l); len(l) == 0 { continue } - if isWrappedWith(l, "[", "]") { + if util.IsWrappedWith(l, "[", "]") { name = strings.Trim(l, "[]") p := Payload{} p.addLang("sr", make(map[string]string)) @@ -334,8 +334,8 @@ func LoadMetadataFromFile(path string) error { return fmt.Errorf("webutility: LoadMetadataFromFile: error on line %d: [invalid format] [%s]", i+1, l) } - k := trimSpaces(parts[0]) - v := trimSpaces(parts[1]) + k := strings.TrimSpace(parts[0]) + v := strings.TrimSpace(parts[1]) if v != "-" { metadata[name].Lang[0].FieldsLabels[k] = v } @@ -344,34 +344,6 @@ func LoadMetadataFromFile(path string) error { return nil } -func isWrappedWith(src, begin, end string) bool { - return strings.HasPrefix(src, begin) && strings.HasSuffix(src, end) -} - -func trimSpaces(s string) string { - return strings.TrimSpace(s) -} - -// ReadFileLines ... -// TODO(marko): Move to separate package -func ReadFileLines(path string) ([]string, error) { - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - - var s strings.Builder - - if _, err = io.Copy(&s, f); err != nil { - return nil, err - } - - lines := strings.Split(s.String(), "\n") - - return lines, nil -} - func hotload(n int) { entityScan := make(map[string]int64) firstCheck := true diff --git a/string_sanitisation.go b/string_sanitisation.go index 54c9bef..c6cdee4 100644 --- a/string_sanitisation.go +++ b/string_sanitisation.go @@ -1,13 +1,10 @@ package webutility -import "strings" +import "git.to-net.rs/marko.tikvic/util" const patern = "\"';&*<>=\\`:" // SanitiseString removes characters from s found in patern and returns new modified string. -func SanitiseString(s string) (safe string) { - for _, c := range patern { - safe = strings.Replace(s, string(c), "", -1) - } - return safe +func SanitiseString(s string) string { + return util.ReplaceAny(s, patern, "") }