github.com/TIBCOSoftware/flogo-lib@v0.5.9/util/utils.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "runtime" 7 "runtime/debug" 8 "strings" 9 10 "github.com/TIBCOSoftware/flogo-lib/logger" 11 ) 12 13 // HandlePanic helper method to handle panics 14 func HandlePanic(name string, err *error) { 15 if r := recover(); r != nil { 16 17 logger.Warnf("%s: PANIC Occurred : %v\n", name, r) 18 19 // todo: useful for debugging 20 logger.Debugf("StackTrace: %s", debug.Stack()) 21 22 if err != nil { 23 *err = fmt.Errorf("%v", r) 24 } 25 } 26 } 27 28 // URLStringToFilePath convert fileURL to file path 29 func URLStringToFilePath(fileURL string) (string, bool) { 30 31 if strings.HasPrefix(fileURL, "file://") { 32 33 filePath := fileURL[7:] 34 35 if runtime.GOOS == "windows" { 36 if strings.HasPrefix(filePath, "/") { 37 filePath = filePath[1:] 38 } 39 filePath = filepath.FromSlash(filePath) 40 } 41 42 filePath = strings.Replace(filePath, "%20", " ", -1) 43 44 return filePath, true 45 } 46 47 return "", false 48 } 49 50 //ParseKeyValuePairs get key-value map from "key=value,key1=value1" str, if value have , please use quotes 51 func ParseKeyValuePairs(keyvalueStr string) map[string]string { 52 m := make(map[string]string) 53 parseKeyValue(removeQuote(keyvalueStr), m) 54 return m 55 } 56 57 func parseKeyValue(keyvalueStr string, m map[string]string) { 58 var key, value, rest string 59 eidx := strings.Index(keyvalueStr, "=") 60 if eidx >= 1 { 61 //Remove space in case it has space between = 62 key = strings.TrimSpace(keyvalueStr[:eidx]) 63 } 64 65 afterKeyStr := strings.TrimSpace(keyvalueStr[eidx+1:]) 66 67 if len(afterKeyStr) > 0 { 68 nextChar := afterKeyStr[0:1] 69 if nextChar == "\"" || nextChar == "'" { 70 //String value 71 reststring := afterKeyStr[1:] 72 endStrIdx := strings.Index(reststring, nextChar) 73 value = reststring[:endStrIdx] 74 rest = reststring[endStrIdx+1:] 75 if strings.Index(rest, ",") == 0 { 76 rest = rest[1:] 77 } 78 } else { 79 cIdx := strings.Index(afterKeyStr, ",") 80 //No value provide 81 if cIdx == 0 { 82 value = "" 83 rest = afterKeyStr[1:] 84 } else if cIdx < 0 { 85 //no more properties 86 value = afterKeyStr 87 rest = "" 88 } else { 89 //more properties 90 value = afterKeyStr[:cIdx] 91 if cIdx < len(afterKeyStr) { 92 rest = afterKeyStr[cIdx+1:] 93 } 94 } 95 96 } 97 m[key] = value 98 if rest != "" { 99 parseKeyValue(rest, m) 100 } 101 } 102 } 103 104 func removeQuote(quoteStr string) string { 105 if (strings.HasPrefix(quoteStr, `"`) && strings.HasSuffix(quoteStr, `"`)) || (strings.HasPrefix(quoteStr, `'`) && strings.HasSuffix(quoteStr, `'`)) { 106 quoteStr = quoteStr[1 : len(quoteStr)-1] 107 } 108 return quoteStr 109 }