Skip to content

Commit

Permalink
[AUTOPR] Automatic updates (#263)
Browse files Browse the repository at this point in the history
* Return last non-nil value

* Update dependencies

---------

Co-authored-by: nicolaasuni-vonage <[email protected]>
  • Loading branch information
github-actions[bot] and nicolaasuni-vonage committed Jul 25, 2024
1 parent abf5b7c commit 712437f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 16 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.98.0
1.98.1
2 changes: 1 addition & 1 deletion examples/service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
replace github.com/Vonage/gosrvlib => ../..

require (
github.com/Vonage/gosrvlib v1.98.0
github.com/Vonage/gosrvlib v1.98.1
github.com/golang/mock v1.6.0
github.com/jstemmer/go-junit-report/v2 v2.1.0
github.com/prometheus/client_golang v1.19.1
Expand Down
4 changes: 2 additions & 2 deletions examples/service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rakyll/gotest v0.0.6 h1:hBTqkO3jiuwYW/M9gL4bu0oTYcm8J6knQAAPUsJsz1I=
github.com/rakyll/gotest v0.0.6/go.mod h1:SkoesdNCWmiD4R2dljIUcfSnNdVZ12y8qK4ojDkc2Sc=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/julienschmidt/httprouter v1.3.0
github.com/prometheus/client_golang v1.19.1
github.com/rakyll/gotest v0.0.6
github.com/redis/go-redis/v9 v9.6.0
github.com/redis/go-redis/v9 v9.6.1
github.com/segmentio/kafka-go v0.4.47
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.19.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/rakyll/gotest v0.0.6 h1:hBTqkO3jiuwYW/M9gL4bu0oTYcm8J6knQAAPUsJsz1I=
github.com/rakyll/gotest v0.0.6/go.mod h1:SkoesdNCWmiD4R2dljIUcfSnNdVZ12y8qK4ojDkc2Sc=
github.com/redis/go-redis/v9 v9.6.0 h1:NLck+Rab3AOTHw21CGRpvQpgTrAU4sgdCswqGtlhGRA=
github.com/redis/go-redis/v9 v9.6.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a h1:3QH7VyOaaiUHNrA9Se4YQIRkDTCw1EJls9xTUCaCeRM=
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
Expand Down
23 changes: 18 additions & 5 deletions pkg/numtrie/example_numtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,46 @@ func ExampleNode_Get() {
fmt.Println(*got, status)
}

// StatusMatchPartial (1 = 0b00000001) indicates that the full number matches
// a trie node that is not a leaf.
// StatusMatchPartial (1 = 0b00000001) indicates that the full number
// matches a trie node that is not a leaf.
got, status = node.Get("702")
if got != nil {
fmt.Println(*got, status)
}

// StatusMatchPrefix (2 = 0b00000010) indicates that only a prefix of the
// number matches a trie leaf. The remaining digits are not present in the trie.
// number matches a trie leaf. The remaining digits are not present in the
// trie.
got, status = node.Get("702153-99")
if got != nil {
fmt.Println(*got, status)
}

// StatusMatchPartialPrefix (3 = 0b00000011) indicates that only a prefix of
// the number matches a trie node that is not a leaf. The remaining digits are
// not present in the trie.
// the number matches a trie node that is not a leaf. The remaining digits
// are not present in the trie.
got, status = node.Get("702-99")
if got != nil {
fmt.Println(*got, status)
}

// StatusMatchPartialPrefix (3 = 0b00000011) indicates that only a prefix of
// the number matches a trie node that is not a leaf. The remaining digits
// are not present in the trie. The last non-nil value on the trie path is
// returned.
// The match is with 7021 but the node at 1 is nil, so the last non-nil
// value at node 702 is returned.
got, status = node.Get("702166")
if got != nil {
fmt.Println(*got, status)
}

// Output:
// <nil> -127
// <nil> -125
// foxtrot 0
// gamma 1
// foxtrot 2
// gamma 3
// gamma 3
}
20 changes: 17 additions & 3 deletions pkg/numtrie/numtrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ func (t *Node[T]) Add(num string, val *T) bool {
}

// Get retrieves a value from the trie with the given numerical key. It supports
// partial matches. The return value should always be checked for the nil value.
// The second return value provides information about the match status:
// partial matches. It returns the last non-nil value found in the trie path for
// the specified number. The return value should always be checked for the nil
// value. The second return value provides information about the match status:
// - StatusMatchEmpty (-127 = 0b10000001) indicates that the input string is
// empty and no match was found.
// - StatusMatchNo (-125 = 0b10000011) indicates that no match was found. The
Expand All @@ -98,27 +99,40 @@ func (t *Node[T]) Get(num string) (*T, int8) {
var match, digit int

node := t
val := node.value // the root node value is also the default value

for _, v := range num {
i, ok := phonekeypad.KeypadDigit(v)
if !ok {
// ingnore non-digit characters
continue
}

digit++

if node.children[i] == nil {
// there are no more children to match
if node.value != nil {
val = node.value
}

break
}

// move to the next child node
node = node.children[i]

if node.value != nil {
// remember the last non-nil value found
val = node.value
}

match++
}

status := (int8(typeutil.BoolToInt(match == 0)<<7) |
int8(typeutil.BoolToInt(digit > match)<<1) |
int8(typeutil.BoolToInt(node.numChildren > 0)))

return node.value, status
return val, status
}
50 changes: 49 additions & 1 deletion pkg/numtrie/numtrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ func TestNode_Get(t *testing.T) {
valC := 53
node.Add("4567", &valC)

valD := 57
valD := 59
node.Add("456", &valD)

valE := 61
node.Add("07", &valE)

valF := 67
node.Add("0732", &valF)

tests := []struct {
name string
num string
Expand Down Expand Up @@ -133,6 +139,48 @@ func TestNode_Get(t *testing.T) {
exp: &valD,
status: StatusMatchPartialPrefix,
},
{
name: "partial match -4 with current val",
num: "0",
exp: nil,
status: StatusMatchPartial,
},
{
name: "partial match -2 with current val",
num: "07",
exp: &valE,
status: StatusMatchPartial,
},
{
name: "partial match -1 with parent val",
num: "073",
exp: &valE,
status: StatusMatchPartial,
},
{
name: "partial match -2 with parent val",
num: "075",
exp: &valE,
status: StatusMatchPartialPrefix,
},
{
name: "full match with current val",
num: "0732",
exp: &valF,
status: StatusMatchFull,
},
{
name: "partial prefix match with parent value",
num: "0739",
exp: &valE,
status: StatusMatchPartialPrefix,
},
{
name: "prefix match with parent value",
num: "07325",
exp: &valF,
status: StatusMatchPrefix,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 712437f

Please sign in to comment.