当前位置:网站首页>On the escape of inequality value

On the escape of inequality value

2022-06-26 18:45:00 51CTO

Recently I met a need , Front end filling inequality , The back end needs to be stored , It is necessary to translate the inequality into the form of interval , Let's share some general solutions , For reference only .

One 、 Agree with the front end on the data format to be transmitted

The front page display is like this

 On the idea of escaping the value of inequality _ Version number

The parameter transfer format agreed with the front end is

      
      
gt @1 .0 .0 // Greater than 1.0.0
ge @1 .0 .0 // Greater than or equal to 1.0.0
lt @1 .0 .0 // Less than 1.0.0
le @1 .0 .0 // Less than or equal to 1.0.0
eq @1 .0 .0 // be equal to 1.0.0
ne @1 .0 .0 // It's not equal to 1.0.0
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

utilize ​​@​​  Symbols separate symbols from version numbers , This symbol can also be used for subsequent stock in , There are multiple rules that can be passed to a string array , You can also pass comma separated strings

Two 、 Verify before warehousing

      
      
// Check the maximum and minimum version numbers of multiple rules
versionRangeList : = strings . Split( body . VersionRange, ",")
switch len( versionRangeList) {
case 1:
version : = strings . Split( versionRangeList[ 0], "@")
switch version[ 0] {
case GT, GE, LT, LE, EQ, NE:
// Normal rule
default:
return tracerr . NewError( " The rules are illegal , Parameter transfer exception :" + versionRangeList[ 0])
}
case 2:
version1 : = strings . Split( versionRangeList[ 0], "@")
version2 : = strings . Split( versionRangeList[ 1], "@")
switch version1[ 0] {
case GT, GE:
if version1[ 1] >= version2[ 1] || ( version2[ 0] != LT && version2[ 0] != LE) {
return tracerr . NewError( " The rules are illegal , The minimum version number is not less than the maximum version number ")
}
case LT, LE:
if version1[ 1] <= version2[ 1] || ( version2[ 0] != GT && version2[ 0] != GE) {
return tracerr . NewError( " The rules are illegal , The minimum version number is not less than the maximum version number ")
}
default:
return tracerr . NewError( " The rules are illegal , Multiple rules can only be selected :gt,ge,lt,le")
}
default:
return tracerr . NewError( " The rules are illegal , The number of rules is abnormal , The number of rules is :" + strconv . Itoa( len( versionRangeList)))
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

3、 ... and 、 Escape to interval

Because it is necessary to convert inequality into interval form and transmit it to other modules , So I chose to create one map, Establish the corresponding relationship between inequality and interval scheme

      
      
// Inequality constant
const (
GT = "gt" // Greater than
GE = "ge" // Greater than or equal to
LT = "lt" // Less than
LE = "le" // Less than or equal to
EQ = "eq" // be equal to
NE = "ne" // It's not equal to
)

func RuleReversal( versionRange [] string) ( reversalResult string) {
// Reverse the rules Map
ruleReversalMap : = map[ string] string{
GT: "(,Ver]",
GE: "(,Ver)",
LT: "[Ver,)",
LE: "(Ver,)",
EQ: "(,Ver),(Ver,)",
NE: "[Ver]",
}

switch len( versionRange) {
case 1:
version : = strings . Split( versionRange[ 0], "@")
reversalResult = strings . ReplaceAll( ruleReversalMap[ version[ 0]], "Ver", version[ 1])
case 2:
version1 : = strings . Split( versionRange[ 0], "@")
version2 : = strings . Split( versionRange[ 1], "@")
switch version1[ 0] {
case GT, GE:
reversalResult = strings . ReplaceAll( ruleReversalMap[ version1[ 0]], "Ver", version1[ 1]) + "," + strings . ReplaceAll( ruleReversalMap[ version2[ 0]], "Ver", version2[ 1])
case LT, LE:
reversalResult = strings . ReplaceAll( ruleReversalMap[ version1[ 0]], "Ver", version1[ 1]) + "," + strings . ReplaceAll( ruleReversalMap[ version2[ 0]], "Ver", version2[ 1])
default:
tracerr . Print( errors . New( " The rules are illegal , Version inequality exception :" + version1[ 0]))
return ""
}
default:
tracerr . Print( errors . New( " The rules are illegal , The number of rules is abnormal , The number of rules is :" + strconv . Itoa( len( versionRange))))
return ""
}

return reversalResult

}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

utilize ​@​​  Symbols separate symbols from version numbers , Remove the separated inequality symbols map Get the corresponding interval format in , Then, the separated version number is matched to the... In the obtained interval format Ver Replace it .

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261832376030.html