string.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Author : Eric Kim
  2. // Build Date : 23 Jul 2018 Last Update 02 Aug 2018
  3. // End-Agent for Passcon Multi OS go binding with Windows, MacOS, iOS, and Android
  4. // All rights are reserved.
  5. package etc
  6. import (
  7. "crypto/rand"
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "os"
  12. "reflect"
  13. "strings"
  14. )
  15. func RandBytes(i int) []byte {
  16. return []byte(RandString(i))
  17. }
  18. func RandString(i int) string {
  19. b := make([]byte, i)
  20. rand.Read(b)
  21. return (base64.URLEncoding.EncodeToString(b))[0:i]
  22. }
  23. func GetAskName() string {
  24. i := len(os.Args)
  25. if i < 2 {
  26. MyErr("ZMXCDKDALKSJD", errors.New("command arguments are less then 2"), true)
  27. } else {
  28. return os.Args[i-1] // 맨뒤의 Arg 를 json 화일명으로 간주
  29. }
  30. return ""
  31. }
  32. // func (t *EnvConf) StrToStruct(str string) {
  33. // if err := json.Unmarshal([]byte(str), &t); err != nil {
  34. // MyErr("sjdfljsf", err, true)
  35. // }
  36. // return
  37. // }
  38. // func structToMap(in interface{}, tag string) (map[string]interface{}, string) {
  39. // out := make(map[string]interface{})
  40. // v := reflect.ValueOf(in)
  41. // if v.Kind() == reflect.Ptr {
  42. // v = v.Elem()
  43. // }
  44. // // we only accept structs
  45. // if v.Kind() != reflect.Struct {
  46. // fmt.Errorf("ToMap only accepts structs; got %T", v)
  47. // return nil, MyErr("only accepts structs", nil, false)
  48. // }
  49. // typ := v.Type()
  50. // for i := 0; i < v.NumField(); i++ {
  51. // // gets us a StructField
  52. // fi := typ.Field(i)
  53. // if tagv := fi.Tag.Get(tag); tagv != "" {
  54. // out[tagv] = v.Field(i).Interface()
  55. // }
  56. // }
  57. // return out, ""
  58. // }
  59. func ParentDir(params ...string) string {
  60. var workdir string
  61. if len(params) == 0 {
  62. workdir, _ = os.Getwd()
  63. } else {
  64. workdir = params[0]
  65. }
  66. sp := strings.Split(workdir, "/")
  67. parentdir := ""
  68. for i := 1; i < len(sp)-1; i++ {
  69. parentdir += "/" + sp[i]
  70. }
  71. return parentdir
  72. }
  73. func TableName(m interface{}) string {
  74. s := reflect.TypeOf(m).Name()
  75. return SnakeString(s)
  76. }
  77. func PageName(m interface{}) string {
  78. s := reflect.TypeOf(m).Elem().Name()
  79. return SnakeString(s)
  80. }
  81. // snake string, XxYy to xx_yy , XxYY to xx_yy
  82. func SnakeString(s string) string {
  83. data := make([]byte, 0, len(s)*2)
  84. j := false
  85. num := len(s)
  86. for i := 0; i < num; i++ {
  87. d := s[i]
  88. if i > 0 && d >= 'A' && d <= 'Z' && j {
  89. data = append(data, '_')
  90. }
  91. if d != '_' {
  92. j = true
  93. }
  94. data = append(data, d)
  95. }
  96. return strings.ToLower(string(data[:]))
  97. }
  98. // camel string, xx_yy to XxYy
  99. func CamelString(s string) string {
  100. data := make([]byte, 0, len(s))
  101. flag, num := true, len(s)-1
  102. for i := 0; i <= num; i++ {
  103. d := s[i]
  104. if d == '_' {
  105. flag = true
  106. continue
  107. } else if flag {
  108. if d >= 'a' && d <= 'z' {
  109. d = d - 32
  110. }
  111. flag = false
  112. }
  113. data = append(data, d)
  114. }
  115. return string(data[:])
  116. }
  117. func StructToMap(in interface{}, tag string) (map[string]interface{}, error) {
  118. out := make(map[string]interface{})
  119. v := reflect.ValueOf(in)
  120. if v.Kind() == reflect.Ptr {
  121. v = v.Elem()
  122. }
  123. // we only accept structs
  124. if v.Kind() != reflect.Struct {
  125. fmt.Errorf("ToMap only accepts structs; got %T", v)
  126. return nil, MyErr("HJKMNHGYUH-only accepts structs:", nil, false)
  127. }
  128. typ := v.Type()
  129. for i := 0; i < v.NumField(); i++ {
  130. // gets us a StructField
  131. fi := typ.Field(i)
  132. if tagv := fi.Tag.Get(tag); tagv != "" {
  133. out[tagv] = v.Field(i).Interface()
  134. }
  135. }
  136. return out, nil
  137. }
  138. func FileNameFromPath(input string) string {
  139. result := strings.ReplaceAll(input, "\\", "/")
  140. parts := strings.Split(result, "/")
  141. return parts[len(parts)-1]
  142. }
  143. // return the source filename after the last slash
  144. func ChopPath(original string) string {
  145. i := strings.LastIndex(original, "/")
  146. if i == -1 {
  147. return original
  148. } else {
  149. return original[i+1:]
  150. }
  151. }