-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from libp2p/feat/semver-mstream-matching
p2p/host: expose multistream function matching and add semver func
- Loading branch information
Showing
6 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package host | ||
|
||
import ( | ||
"strings" | ||
|
||
semver "github.com/coreos/go-semver/semver" | ||
) | ||
|
||
func MultistreamSemverMatcher(base string) (func(string) bool, error) { | ||
parts := strings.Split(base, "/") | ||
vers, err := semver.NewVersion(parts[len(parts)-1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return func(check string) bool { | ||
chparts := strings.Split(check, "/") | ||
if len(chparts) != len(parts) { | ||
return false | ||
} | ||
|
||
for i, v := range chparts[:len(chparts)-1] { | ||
if parts[i] != v { | ||
return false | ||
} | ||
} | ||
|
||
chvers, err := semver.NewVersion(chparts[len(chparts)-1]) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return vers.Major == chvers.Major && vers.Minor >= chvers.Minor | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package host | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSemverMatching(t *testing.T) { | ||
m, err := MultistreamSemverMatcher("/testing/4.3.5") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cases := map[string]bool{ | ||
"/testing/4.3.0": true, | ||
"/testing/4.3.7": true, | ||
"/testing/4.3.5": true, | ||
"/testing/4.2.7": true, | ||
"/testing/4.0.0": true, | ||
"/testing/5.0.0": false, | ||
"/cars/dogs/4.3.5": false, | ||
"/foo/1.0.0": false, | ||
"": false, | ||
"dogs": false, | ||
"/foo": false, | ||
"/foo/1.1.1.1": false, | ||
} | ||
|
||
for p, ok := range cases { | ||
if m(p) != ok { | ||
t.Fatalf("expected %s to be %t", p, ok) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters