utils.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package common
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "reflect"
  9. "regexp"
  10. "strings"
  11. "unicode"
  12. )
  13. var (
  14. environment = map[string]string{}
  15. wordRegexp = regexp.MustCompile(`\w+`)
  16. )
  17. // Setenv - cria ou atualiza uma variavel de ambiente dentro da aplicação
  18. func Setenv(id, value string) {
  19. environment[id] = value
  20. }
  21. // Getenv - recupera uma variavel de ambiente dentro da aplicação
  22. func Getenv(id string, fallback ...string) (value string) {
  23. var exist bool
  24. if value, exist = environment[id]; !exist {
  25. for _, value = range fallback {
  26. break
  27. }
  28. }
  29. return value
  30. }
  31. func LcFirst(str string) string {
  32. for i, v := range str {
  33. return string(unicode.ToLower(v)) + str[i+1:]
  34. }
  35. return ""
  36. }
  37. func UpFirst(str string) string {
  38. for i, v := range str {
  39. return string(unicode.ToUpper(v)) + str[i+1:]
  40. }
  41. return ""
  42. }
  43. func JSONStringfy(v interface{}) (string, error) {
  44. o, err := json.Marshal(v)
  45. if err != nil {
  46. return "", err
  47. }
  48. return string(o), nil
  49. }
  50. // FileExists reports whether the named file or directory exists.
  51. func FileExists(name string) bool {
  52. if _, err := os.Stat(name); err != nil {
  53. if os.IsNotExist(err) {
  54. return false
  55. }
  56. }
  57. return true
  58. }
  59. func ResolveParams(format string, data map[string]interface{}) string {
  60. for k, v := range data {
  61. format = strings.Replace(
  62. format,
  63. fmt.Sprintf("${%s}", k),
  64. fmt.Sprintf("%v", v),
  65. -1,
  66. )
  67. }
  68. return format
  69. }
  70. func UnderToCamel(input string) string {
  71. input = strings.ToLower(input)
  72. input = strings.ReplaceAll(input, "_", " ")
  73. input = strings.Title(input)
  74. return strings.ReplaceAll(input, " ", "")
  75. }
  76. func Mkdir(perm os.FileMode, paths ...string) (err error) {
  77. for _, path := range paths {
  78. if _, err := os.Stat(path); os.IsNotExist(err) {
  79. if err = os.MkdirAll(path, perm); err != nil {
  80. return err
  81. }
  82. }
  83. }
  84. return
  85. }
  86. func JsonParseMode(path, mode string, v interface{}) (err error) {
  87. if err = ParseJson(path+".json", v); err != nil {
  88. return
  89. }
  90. // fmt.Println(fmt.Sprintf("%s.%s.json", path, mode))
  91. err = ParseJson(fmt.Sprintf("%s.%s.json", path, mode), v, true)
  92. return
  93. }
  94. func ParseJson(filename string, v interface{}, ignoreFileExists ...bool) error {
  95. // fmt.Println("load file ", filename)
  96. data, err := FileGetContents(filename)
  97. if err != nil {
  98. if len(ignoreFileExists) > 0 && ignoreFileExists[0] {
  99. return nil
  100. }
  101. return err
  102. }
  103. if err = json.Unmarshal([]byte(data), v); err != nil {
  104. return err
  105. }
  106. return nil
  107. }
  108. func CamelToUnder(input string) string {
  109. out := camelToUnderRegex.ReplaceAllStringFunc(input, func(m string) string {
  110. return "_" + strings.ToLower(m)
  111. })
  112. return strings.TrimLeft(out, "_")
  113. }
  114. func FilePutContents(filename string, content string, perm os.FileMode) error {
  115. return FilePutContentsBytes(filename, []byte(content), perm)
  116. }
  117. func FilePutContentsBytes(filename string, content []byte, perm os.FileMode) error {
  118. var (
  119. dir = path.Dir(filename)
  120. )
  121. if _, err := os.Stat(dir); os.IsNotExist(err) {
  122. if err = os.MkdirAll(dir, perm); err != nil {
  123. return err
  124. }
  125. }
  126. return ioutil.WriteFile(filename, []byte(content), 0644)
  127. }
  128. func RemoveFile(file string) error {
  129. if err := os.Remove(file); err != nil {
  130. return err
  131. }
  132. return nil
  133. }
  134. func GetFiles(directory string) ([]os.FileInfo, error) {
  135. files, err := ioutil.ReadDir(directory)
  136. if err != nil {
  137. return []os.FileInfo{}, err
  138. }
  139. return files, nil
  140. }
  141. func FileGetContents(filename string) (string, error) {
  142. b, err := ioutil.ReadFile(filename)
  143. if err != nil {
  144. return "", err
  145. }
  146. return string(b), nil
  147. }
  148. func WriteToJson(path string, object interface{}) (err error) {
  149. var data []byte
  150. if data, err = json.MarshalIndent(object, "", " "); err == nil {
  151. err = FilePutContentsBytes(path, data, 0777)
  152. }
  153. return
  154. }
  155. func IsEmpty(reference interface{}) bool {
  156. return isEmptyReflection(reflect.ValueOf(reference))
  157. }
  158. func isEmptyReflection(v reflect.Value) bool {
  159. switch v.Kind() {
  160. case reflect.String, reflect.Array:
  161. return v.Len() == 0
  162. case reflect.Map, reflect.Slice:
  163. return v.Len() == 0 || v.IsNil()
  164. // case reflect.Bool:
  165. // return !v.Bool()
  166. // case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  167. // return v.Int() == 0
  168. // case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  169. // return v.Uint() == 0
  170. // case reflect.Float32, reflect.Float64:
  171. // return v.Float() == 0
  172. case reflect.Interface, reflect.Ptr:
  173. return v.IsNil()
  174. }
  175. return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
  176. }