github.com/hdt3213/godis@v1.2.9/datastruct/sortedset/border.go (about) 1 package sortedset 2 3 import ( 4 "errors" 5 "strconv" 6 ) 7 8 /* 9 * ScoreBorder is a struct represents `min` `max` parameter of redis command `ZRANGEBYSCORE` 10 * can accept: 11 * int or float value, such as 2.718, 2, -2.718, -2 ... 12 * exclusive int or float value, such as (2.718, (2, (-2.718, (-2 ... 13 * infinity: +inf, -inf, inf(same as +inf) 14 */ 15 16 const ( 17 negativeInf int8 = -1 18 positiveInf int8 = 1 19 ) 20 21 // ScoreBorder represents range of a float value, including: <, <=, >, >=, +inf, -inf 22 type ScoreBorder struct { 23 Inf int8 24 Value float64 25 Exclude bool 26 } 27 28 // if max.greater(score) then the score is within the upper border 29 // do not use min.greater() 30 func (border *ScoreBorder) greater(value float64) bool { 31 if border.Inf == negativeInf { 32 return false 33 } else if border.Inf == positiveInf { 34 return true 35 } 36 if border.Exclude { 37 return border.Value > value 38 } 39 return border.Value >= value 40 } 41 42 func (border *ScoreBorder) less(value float64) bool { 43 if border.Inf == negativeInf { 44 return true 45 } else if border.Inf == positiveInf { 46 return false 47 } 48 if border.Exclude { 49 return border.Value < value 50 } 51 return border.Value <= value 52 } 53 54 var positiveInfBorder = &ScoreBorder{ 55 Inf: positiveInf, 56 } 57 58 var negativeInfBorder = &ScoreBorder{ 59 Inf: negativeInf, 60 } 61 62 // ParseScoreBorder creates ScoreBorder from redis arguments 63 func ParseScoreBorder(s string) (*ScoreBorder, error) { 64 if s == "inf" || s == "+inf" { 65 return positiveInfBorder, nil 66 } 67 if s == "-inf" { 68 return negativeInfBorder, nil 69 } 70 if s[0] == '(' { 71 value, err := strconv.ParseFloat(s[1:], 64) 72 if err != nil { 73 return nil, errors.New("ERR min or max is not a float") 74 } 75 return &ScoreBorder{ 76 Inf: 0, 77 Value: value, 78 Exclude: true, 79 }, nil 80 } 81 value, err := strconv.ParseFloat(s, 64) 82 if err != nil { 83 return nil, errors.New("ERR min or max is not a float") 84 } 85 return &ScoreBorder{ 86 Inf: 0, 87 Value: value, 88 Exclude: false, 89 }, nil 90 }