mqls_match.go 3.4 KB

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