mqls_match.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package odbcmql
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strings"
  6. "time"
  7. odb "git.wecise.com/wecise/odb-go/odb"
  8. "gitee.com/wecisecode/util/cast"
  9. )
  10. func MatchValues(rtn *odb.Result, ks []string, ms []any) (x int, matchingvalues [][]any) {
  11. var matchingvalue any
  12. for _, dat := range rtn.Data {
  13. matchingvalues = append(matchingvalues, []any{})
  14. m := false
  15. sv := ""
  16. match := true
  17. for i, k := range ks {
  18. matchingvalue = dat[k]
  19. m, sv = matchvalue(ms[i], matchingvalue)
  20. if !m {
  21. ks := strings.Split(k, ".")
  22. if len(ks) <= 1 {
  23. match = false
  24. break
  25. } else {
  26. k := ""
  27. for i := 0; match && i < len(ks); i++ {
  28. if matchingvalue == nil {
  29. if k != "" {
  30. k += "."
  31. }
  32. k += ks[i]
  33. matchingvalue = dat[k]
  34. if matchingvalue == nil && ks[i] == "len" {
  35. matchingvalue = 0
  36. }
  37. } else {
  38. switch mv := matchingvalue.(type) {
  39. case map[string]any:
  40. matchingvalue = mv[ks[i]]
  41. if matchingvalue == nil && ks[i] == "len" {
  42. matchingvalue = len(mv)
  43. }
  44. case []any:
  45. if ks[i] == "len" {
  46. matchingvalue = len(mv)
  47. } else {
  48. n, e := cast.ToIntE(ks[i])
  49. if e != nil {
  50. match = false
  51. break
  52. }
  53. if n >= len(mv) {
  54. match = false
  55. break
  56. }
  57. matchingvalue = mv[n]
  58. }
  59. case string:
  60. if ks[i] == "len" {
  61. matchingvalue = len(mv)
  62. } else {
  63. n, e := cast.ToIntE(ks[i])
  64. if e != nil {
  65. match = false
  66. break
  67. }
  68. if n >= len(mv) {
  69. match = false
  70. break
  71. }
  72. matchingvalue = mv[n]
  73. }
  74. default:
  75. if ks[i] == "len" {
  76. matchingvalue = 0
  77. } else {
  78. matchingvalue = nil
  79. }
  80. }
  81. }
  82. }
  83. m, sv = matchvalue(ms[i], matchingvalue)
  84. if !m {
  85. match = false
  86. break
  87. }
  88. }
  89. }
  90. matchingvalues[len(matchingvalues)-1] = append(matchingvalues[len(matchingvalues)-1], sv)
  91. }
  92. if match {
  93. x++
  94. matchingvalues = matchingvalues[:len(matchingvalues)-1]
  95. } else {
  96. matchingvalues[len(matchingvalues)-1] = append(matchingvalues[len(matchingvalues)-1], sv)
  97. }
  98. }
  99. return
  100. }
  101. func matchvalue(matcher any, matchingvalue any) (bool, string) {
  102. switch m := matcher.(type) {
  103. case *regexp.Regexp:
  104. switch v := matchingvalue.(type) {
  105. case string:
  106. return m.MatchString(v), v
  107. default:
  108. sv := fmt.Sprintf("%#v", matchingvalue)
  109. return m.MatchString(sv), sv
  110. }
  111. case string:
  112. sv := ""
  113. switch v := matchingvalue.(type) {
  114. case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, bool:
  115. sv = fmt.Sprintf("%v", matchingvalue)
  116. case float32, float64:
  117. sv = fmt.Sprintf("%v", matchingvalue)
  118. fvrange := strings.Split(m, "~")
  119. if len(fvrange) > 1 {
  120. return sv >= fvrange[0] && sv <= fvrange[len(fvrange)-1], sv
  121. }
  122. case time.Time:
  123. sv = fmt.Sprintf("%#v", v.Format("2006-01-02T15:04:05.000-07:00"))
  124. default:
  125. sv = fmt.Sprintf("%#v", matchingvalue)
  126. }
  127. return sv == m, sv
  128. case *Action:
  129. switch m.Name {
  130. case "<FUZZY>":
  131. return matchFuzzy(matchingvalue, m.Args)
  132. }
  133. }
  134. panic("未定义的匹配类型")
  135. }
  136. func matchFuzzy(matchingvalue any, mvrange []any) (bool, string) {
  137. sv := ""
  138. switch matchingvalue.(type) {
  139. case int, uint, int8, uint8, int16, uint16, int32, uint32, int64, uint64, bool, float32, float64, string:
  140. sv = fmt.Sprintf("%v", matchingvalue)
  141. default:
  142. sv = fmt.Sprintf("%#v", matchingvalue)
  143. }
  144. if len(mvrange) == 0 {
  145. return false, sv
  146. }
  147. m1 := fmt.Sprintf("%v", mvrange[0])
  148. m2 := fmt.Sprintf("%v", mvrange[len(mvrange)-1])
  149. return sv >= m1 && sv <= m2, sv
  150. }