From 3d88aed3831d40ca5bcab9bb4a8be77c5f670e31 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 09:28:30 +0200 Subject: [PATCH 1/7] chore: add Makefile with test command --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..456d79e --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +GOPATH=$(shell go env GOPATH) + +.PHONY: test +test: + @echo "==> Running tests" + go test -v ./... From ad489cf88f40eb8c7e6e74fc9105341982f52215 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 09:28:59 +0200 Subject: [PATCH 2/7] docs: add testing documentation block --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index e69de29..198158d 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,7 @@ +# lot-sh/core + +## Testing + +```shell +make test +``` From 444ba88d09612e92a276d056eca1e61944119e83 Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 09:32:47 +0200 Subject: [PATCH 3/7] refactor: reallocate scheme type to a proper isolate context This prevents name collisions the exposed constants and functions and keeps the logic isolate according to the context of scheme --- resolver_factory.go | 9 ++++-- resolver_factory_test.go | 9 ++++-- resource.go | 3 +- resource_factory.go | 11 +++++--- resource_factory_test.go | 9 ++++-- scheme_type.go => scheme/scheme_type.go | 28 +++++++++++-------- .../scheme_type_test.go | 3 +- 7 files changed, 46 insertions(+), 26 deletions(-) rename scheme_type.go => scheme/scheme_type.go (72%) rename scheme_type_test.go => scheme/scheme_type_test.go (99%) diff --git a/resolver_factory.go b/resolver_factory.go index 4297ba7..557907e 100644 --- a/resolver_factory.go +++ b/resolver_factory.go @@ -1,16 +1,19 @@ package core -import "errors" +import ( + "errors" + "github.com/lot-sh/core/scheme" +) // ResolverFactory returns the implementation of Resolver // which can handle the SchemeType passed as argument // // • when there is not a Resolver associated to the SchemeType // passed as argument it will return a nil Resolver and an error -func ResolverFactory(st SchemeType) (Resolver, error) { +func ResolverFactory(st scheme.SchemeType) (Resolver, error) { var resolver Resolver switch st { - case HTTP, HTTPS: + case scheme.HTTP, scheme.HTTPS: resolver = &HTTPResolver{} return resolver, nil } diff --git a/resolver_factory_test.go b/resolver_factory_test.go index 4088556..139c001 100644 --- a/resolver_factory_test.go +++ b/resolver_factory_test.go @@ -1,16 +1,19 @@ package core -import "testing" +import ( + "testing" + "github.com/lot-sh/core/scheme" +) func TestResolverFactoryShouldWorksWhenPassedHTTPSchemeType(t *testing.T) { - _, err := ResolverFactory(HTTP) + _, err := ResolverFactory(scheme.HTTP) if err != nil { t.Error("Error should be no returned by ResolverFactory when passed a HTTP SchemeType") } } func TestResolverFactoryShouldReturnErrorWhenPassedUNKNOWNSchemeType(t *testing.T) { - _, err := ResolverFactory(UNKNOWN) + _, err := ResolverFactory(scheme.UNKNOWN) if err == nil { t.Error("Error should be returned by ResolverFactory when passed a UNKNONW SchemeType") } diff --git a/resource.go b/resource.go index 43cfd4c..6a74b8b 100644 --- a/resource.go +++ b/resource.go @@ -3,13 +3,14 @@ package core import ( "fmt" "io" + "github.com/lot-sh/core/scheme" ) // Resource struct is the data of the principal resource of // this application, which is pieces of code and they origin type Resource struct { Locator string - Scheme SchemeType + Scheme scheme.SchemeType } func (r *Resource) String() string { diff --git a/resource_factory.go b/resource_factory.go index 061bec5..cdc3092 100644 --- a/resource_factory.go +++ b/resource_factory.go @@ -1,15 +1,18 @@ package core -import "errors" +import ( + "errors" + "github.com/lot-sh/core/scheme" +) // ResourceFactory function returns a Resource given a locator func ResourceFactory(locator string) (Resource, error) { - var scheme SchemeType = GetSchemeTypeFrom(locator) + var sch scheme.SchemeType = scheme.GetSchemeTypeFrom(locator) var resource Resource = Resource{ locator, - scheme, + sch, } - if scheme == UNKNOWN { + if sch == scheme.UNKNOWN { return resource, errors.New("Unknown scheme, the locator may be a invalid one") } return resource, nil diff --git a/resource_factory_test.go b/resource_factory_test.go index bb871cd..ffa29ec 100644 --- a/resource_factory_test.go +++ b/resource_factory_test.go @@ -1,6 +1,9 @@ package core -import "testing" +import ( + "testing" + "github.com/lot-sh/core/scheme" +) func TestResourceFactoryShouldWorkAsExpected(t *testing.T) { locator := "lot:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX" @@ -8,8 +11,8 @@ func TestResourceFactoryShouldWorkAsExpected(t *testing.T) { if err != nil { t.Error(err) } - if resource.Scheme != LOT { - t.Errorf("the scheme of %s should be %s, found %s", locator, LOT, resource.Scheme) + if resource.Scheme != scheme.LOT { + t.Errorf("the scheme of %s should be %s, found %s", locator, scheme.LOT, resource.Scheme) } if resource.Locator != locator { t.Errorf("the locator should be %s, found %s", locator, resource.Locator) diff --git a/scheme_type.go b/scheme/scheme_type.go similarity index 72% rename from scheme_type.go rename to scheme/scheme_type.go index 7af067e..1cc8063 100644 --- a/scheme_type.go +++ b/scheme/scheme_type.go @@ -1,4 +1,4 @@ -package core +package scheme import "regexp" @@ -18,19 +18,25 @@ const ( UNKNOWN ) +// SEPARATOR defines where the scheme definition +// ends in a formated string +const SEPARATOR = ":" + +// ListNames string naming mapping for each SchemeType +var ListNames = []string{ + "ftp", + "gist", + "http", + "https", + "ipfs", + "lot", +} + func (st SchemeType) String() string { - listNames := []string{ - "ftp", - "gist", - "http", - "https", - "ipfs", - "lot", - } - if st < 0 || st >= SchemeType(len(listNames)) { + if st < 0 || st >= SchemeType(len(ListNames)) { return "unknown" } - return listNames[st] + return ListNames[st] } // GetSchemeTypeFrom try to identify the scheme of the locator diff --git a/scheme_type_test.go b/scheme/scheme_type_test.go similarity index 99% rename from scheme_type_test.go rename to scheme/scheme_type_test.go index e23efaf..c150d32 100644 --- a/scheme_type_test.go +++ b/scheme/scheme_type_test.go @@ -1,4 +1,4 @@ -package core +package scheme import "testing" @@ -32,3 +32,4 @@ func TestGetSchemeTypeFromShouldWorkAsExpected(t *testing.T) { t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorUnknown, GetSchemeTypeFrom(locatorUnknown)) } } + From e41d63f7b4892d3488c35aa9c604226110db01ba Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 09:39:20 +0200 Subject: [PATCH 4/7] test: add missing test scheme/TestCastingString --- scheme/scheme_type_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scheme/scheme_type_test.go b/scheme/scheme_type_test.go index c150d32..c3a49d8 100644 --- a/scheme/scheme_type_test.go +++ b/scheme/scheme_type_test.go @@ -33,3 +33,16 @@ func TestGetSchemeTypeFromShouldWorkAsExpected(t *testing.T) { } } + +func TestCastingString(t *testing.T) { + actual := FTP.String() + expected := "ftp" + if actual != "ftp" { + t.Errorf("The SchemeType %s should be casted as string with the value of %s, instead got %s", FTP, expected, actual) + } + actual = HTTP.String() + expected = "http" + if actual != "http" { + t.Errorf("The SchemeType %s should be casted as string with the value of %s, instead got %s", HTTP, expected, actual) + } +} \ No newline at end of file From a083360b02fbe63d2e5516117068fb966cb963cc Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Wed, 3 Jun 2020 10:52:12 +0200 Subject: [PATCH 5/7] feat: add resource locator logic --- resource/locator.go | 66 ++++++++++++++++++ resource/locator_test.go | 143 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 resource/locator.go create mode 100644 resource/locator_test.go diff --git a/resource/locator.go b/resource/locator.go new file mode 100644 index 0000000..b3d87c4 --- /dev/null +++ b/resource/locator.go @@ -0,0 +1,66 @@ +package resource + +import ( + "fmt" + "strings" + "github.com/lot-sh/core/scheme" +) + +// Locator represents the minimal locator functionality +// For more details visit the RFC +// https://github.com/lot-sh/docs/blob/master/rfcs/002-resource-locator.md +type Locator struct { + scheme scheme.SchemeType + path string +} + +// NewLocator instance a locator from a given string which parse +// and ensure if the string is well formated +func NewLocator(strloc string) (*Locator, error) { + res := Locator{} + parts := strings.Split(strloc, scheme.SEPARATOR) + res.scheme = scheme.GetSchemeTypeFrom(parts[0]) + + if res.scheme == scheme.UNKNOWN { + return nil, fmt.Errorf("Failure schema detection when parsing %s", strloc) + } + + partsLen := len(parts) + if partsLen != 2 { + if partsLen == 1 { + return nil, fmt.Errorf("Malformatted locator missing path when parsing %s", strloc) + } + + return nil, fmt.Errorf("Malformatted locator when parsing %s", strloc) + } + + res.path = parts[1] + + return &res, nil +} + +// Scheme gets the scheme type with string format +func (l *Locator) Scheme() string { + return l.scheme.String() +} + +// Path returns the part of the locator wich contains +// the identification of a resource under the specified scheme +func (l *Locator) Path() string { + return l.path +} + +// Tag returns the name of version of a resource +func (l *Locator) Tag() string { + parts := strings.Split(l.path, "@") + if len(parts) > 1 { + return parts[1] + } + return "" +} + +// String format representation +func (l *Locator) String() string { + s := []string{l.Scheme(), l.Path()} + return strings.Join(s, scheme.SEPARATOR) +} \ No newline at end of file diff --git a/resource/locator_test.go b/resource/locator_test.go new file mode 100644 index 0000000..f25f84b --- /dev/null +++ b/resource/locator_test.go @@ -0,0 +1,143 @@ +package resource + +import ( + "testing" + "strings" + "fmt" +) + +var suitePassMsg = ` +PASS: %s +-> Input: %s` + +var suiteFailMsg = ` +FAIL: %s +-> Input: %s +-> Actual %s +-> Expected: %s` + + +func TestLocatorShouldParseAndFormatWithoutErrors(t *testing.T) { + type suiteExpected struct { + scheme string + path string + tag string + } + suites := []struct{ + message string + input string + expected suiteExpected + }{ + { + "Regular http resource", + "http://example.com/testfile", + suiteExpected{ + "http", "//example.com/testfile", "", + }, + }, { + "Regular ftp resource", + "ftp://example.com/testfile?test#foo", + suiteExpected{ + "ftp", "//example.com/testfile?test#foo", "", + }, + }, { + "With tag", + "lot:rubeniskov/semver-patcher@v1.0.1", + suiteExpected{ + "lot", "rubeniskov/semver-patcher@v1.0.1", "v1.0.1", + }, + }, { + "With ID", + "lot:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", + suiteExpected{ + "lot", "QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", "", + }, + }, + } + + for _, suite := range suites { + loc, err := NewLocator(suite.input) + if err != nil { + t.Error(err) + } + if loc.Scheme() != suite.expected.scheme { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected scheme", suite.message), + suite.input, + loc.Scheme(), + suite.expected.scheme, + ) + } + if loc.Path() != suite.expected.path { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected path", suite.message), + loc.Path(), + suite.expected.path, + ) + } + if loc.Tag() != suite.expected.tag { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected tag", suite.message), + loc.Path(), + suite.expected.tag, + ) + } + t.Logf( + suitePassMsg, + fmt.Sprintf( + "%s should parse without errors (%s)", + suite.message, + loc, + ), + suite.input, + ) + } +} + +func TestLocatorShouldRaiseErrorScheme(t *testing.T) { + suites := []struct{ + message string + input string + expected string + }{ + { + "Missing scheme", + "//example.com/testfile", + "Failure schema detection when parsing //example.com/testfile", + }, { + "Wrong scheme", + "scheme:", + "Failure schema detection when parsing scheme:", + }, { + "Missing path", + "ftp", + "Malformatted locator missing path when parsing ftp", + }, { + "Wrong path", + "ftp:test:", + "Malformatted locator when parsing ftp:test:", + }, + } + + for _, suite := range suites { + _, err := NewLocator(suite.input) + if err == nil { + t.Error(err) + } + if !strings.Contains(err.Error(), suite.expected) { + t.Errorf(suiteFailMsg, suite.message, suite.input, err.Error(), suite.expected) + } + t.Logf( + suitePassMsg, + fmt.Sprintf( + "%s should raise error with the espected message (%s)", + suite.message, + err.Error(), + ), + suite.input, + ) + } +} \ No newline at end of file From 9d2569fb8bbb65ca922bede7ffb2321b794c4e8b Mon Sep 17 00:00:00 2001 From: rubeniskov Date: Thu, 11 Jun 2020 20:25:00 +0200 Subject: [PATCH 6/7] style: applied golang standard format --- resource/locator.go | 21 ++-- resource/locator_test.go | 243 ++++++++++++++++++------------------- scheme/scheme_type.go | 2 +- scheme/scheme_type_test.go | 3 +- 4 files changed, 134 insertions(+), 135 deletions(-) diff --git a/resource/locator.go b/resource/locator.go index b3d87c4..b90bf5b 100644 --- a/resource/locator.go +++ b/resource/locator.go @@ -3,6 +3,7 @@ package resource import ( "fmt" "strings" + "github.com/lot-sh/core/scheme" ) @@ -11,26 +12,26 @@ import ( // https://github.com/lot-sh/docs/blob/master/rfcs/002-resource-locator.md type Locator struct { scheme scheme.SchemeType - path string + path string } -// NewLocator instance a locator from a given string which parse +// NewLocator instance a locator from a given string which parse // and ensure if the string is well formated func NewLocator(strloc string) (*Locator, error) { - res := Locator{} + res := Locator{} parts := strings.Split(strloc, scheme.SEPARATOR) res.scheme = scheme.GetSchemeTypeFrom(parts[0]) - + if res.scheme == scheme.UNKNOWN { return nil, fmt.Errorf("Failure schema detection when parsing %s", strloc) } - + partsLen := len(parts) if partsLen != 2 { if partsLen == 1 { return nil, fmt.Errorf("Malformatted locator missing path when parsing %s", strloc) - } - + } + return nil, fmt.Errorf("Malformatted locator when parsing %s", strloc) } @@ -44,8 +45,8 @@ func (l *Locator) Scheme() string { return l.scheme.String() } -// Path returns the part of the locator wich contains -// the identification of a resource under the specified scheme +// Path returns the part of the locator wich contains +// the identification of a resource under the specified scheme func (l *Locator) Path() string { return l.path } @@ -63,4 +64,4 @@ func (l *Locator) Tag() string { func (l *Locator) String() string { s := []string{l.Scheme(), l.Path()} return strings.Join(s, scheme.SEPARATOR) -} \ No newline at end of file +} diff --git a/resource/locator_test.go b/resource/locator_test.go index f25f84b..2d94fb0 100644 --- a/resource/locator_test.go +++ b/resource/locator_test.go @@ -1,9 +1,9 @@ package resource import ( - "testing" - "strings" - "fmt" + "fmt" + "strings" + "testing" ) var suitePassMsg = ` @@ -16,128 +16,127 @@ FAIL: %s -> Actual %s -> Expected: %s` - func TestLocatorShouldParseAndFormatWithoutErrors(t *testing.T) { - type suiteExpected struct { - scheme string - path string - tag string - } - suites := []struct{ - message string - input string - expected suiteExpected - }{ - { - "Regular http resource", - "http://example.com/testfile", - suiteExpected{ - "http", "//example.com/testfile", "", - }, - }, { - "Regular ftp resource", - "ftp://example.com/testfile?test#foo", - suiteExpected{ - "ftp", "//example.com/testfile?test#foo", "", - }, - }, { - "With tag", - "lot:rubeniskov/semver-patcher@v1.0.1", - suiteExpected{ - "lot", "rubeniskov/semver-patcher@v1.0.1", "v1.0.1", - }, - }, { - "With ID", - "lot:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", - suiteExpected{ - "lot", "QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", "", - }, - }, - } + type suiteExpected struct { + scheme string + path string + tag string + } + suites := []struct { + message string + input string + expected suiteExpected + }{ + { + "Regular http resource", + "http://example.com/testfile", + suiteExpected{ + "http", "//example.com/testfile", "", + }, + }, { + "Regular ftp resource", + "ftp://example.com/testfile?test#foo", + suiteExpected{ + "ftp", "//example.com/testfile?test#foo", "", + }, + }, { + "With tag", + "lot:rubeniskov/semver-patcher@v1.0.1", + suiteExpected{ + "lot", "rubeniskov/semver-patcher@v1.0.1", "v1.0.1", + }, + }, { + "With ID", + "lot:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", + suiteExpected{ + "lot", "QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX", "", + }, + }, + } - for _, suite := range suites { - loc, err := NewLocator(suite.input) - if err != nil { - t.Error(err) - } - if loc.Scheme() != suite.expected.scheme { - t.Errorf( - suiteFailMsg, - fmt.Sprintf("%s: Wrong expected scheme", suite.message), - suite.input, - loc.Scheme(), - suite.expected.scheme, - ) - } - if loc.Path() != suite.expected.path { - t.Errorf( - suiteFailMsg, - fmt.Sprintf("%s: Wrong expected path", suite.message), - loc.Path(), - suite.expected.path, - ) - } - if loc.Tag() != suite.expected.tag { - t.Errorf( - suiteFailMsg, - fmt.Sprintf("%s: Wrong expected tag", suite.message), - loc.Path(), - suite.expected.tag, - ) - } - t.Logf( - suitePassMsg, - fmt.Sprintf( - "%s should parse without errors (%s)", - suite.message, - loc, - ), - suite.input, - ) - } + for _, suite := range suites { + loc, err := NewLocator(suite.input) + if err != nil { + t.Error(err) + } + if loc.Scheme() != suite.expected.scheme { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected scheme", suite.message), + suite.input, + loc.Scheme(), + suite.expected.scheme, + ) + } + if loc.Path() != suite.expected.path { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected path", suite.message), + loc.Path(), + suite.expected.path, + ) + } + if loc.Tag() != suite.expected.tag { + t.Errorf( + suiteFailMsg, + fmt.Sprintf("%s: Wrong expected tag", suite.message), + loc.Path(), + suite.expected.tag, + ) + } + t.Logf( + suitePassMsg, + fmt.Sprintf( + "%s should parse without errors (%s)", + suite.message, + loc, + ), + suite.input, + ) + } } func TestLocatorShouldRaiseErrorScheme(t *testing.T) { - suites := []struct{ - message string - input string - expected string - }{ - { - "Missing scheme", - "//example.com/testfile", - "Failure schema detection when parsing //example.com/testfile", - }, { - "Wrong scheme", - "scheme:", - "Failure schema detection when parsing scheme:", - }, { - "Missing path", - "ftp", - "Malformatted locator missing path when parsing ftp", - }, { - "Wrong path", - "ftp:test:", - "Malformatted locator when parsing ftp:test:", - }, - } + suites := []struct { + message string + input string + expected string + }{ + { + "Missing scheme", + "//example.com/testfile", + "Failure schema detection when parsing //example.com/testfile", + }, { + "Wrong scheme", + "scheme:", + "Failure schema detection when parsing scheme:", + }, { + "Missing path", + "ftp", + "Malformatted locator missing path when parsing ftp", + }, { + "Wrong path", + "ftp:test:", + "Malformatted locator when parsing ftp:test:", + }, + } - for _, suite := range suites { - _, err := NewLocator(suite.input) - if err == nil { - t.Error(err) - } - if !strings.Contains(err.Error(), suite.expected) { - t.Errorf(suiteFailMsg, suite.message, suite.input, err.Error(), suite.expected) - } - t.Logf( - suitePassMsg, - fmt.Sprintf( - "%s should raise error with the espected message (%s)", - suite.message, - err.Error(), - ), - suite.input, - ) - } -} \ No newline at end of file + for _, suite := range suites { + _, err := NewLocator(suite.input) + if err == nil { + t.Error(err) + } + if !strings.Contains(err.Error(), suite.expected) { + t.Errorf(suiteFailMsg, suite.message, suite.input, err.Error(), suite.expected) + } + t.Logf( + suitePassMsg, + fmt.Sprintf( + "%s should raise error with the espected message (%s)", + suite.message, + err.Error(), + ), + suite.input, + ) + } +} diff --git a/scheme/scheme_type.go b/scheme/scheme_type.go index 1cc8063..8457886 100644 --- a/scheme/scheme_type.go +++ b/scheme/scheme_type.go @@ -18,7 +18,7 @@ const ( UNKNOWN ) -// SEPARATOR defines where the scheme definition +// SEPARATOR defines where the scheme definition // ends in a formated string const SEPARATOR = ":" diff --git a/scheme/scheme_type_test.go b/scheme/scheme_type_test.go index c3a49d8..9ac072f 100644 --- a/scheme/scheme_type_test.go +++ b/scheme/scheme_type_test.go @@ -33,7 +33,6 @@ func TestGetSchemeTypeFromShouldWorkAsExpected(t *testing.T) { } } - func TestCastingString(t *testing.T) { actual := FTP.String() expected := "ftp" @@ -45,4 +44,4 @@ func TestCastingString(t *testing.T) { if actual != "http" { t.Errorf("The SchemeType %s should be casted as string with the value of %s, instead got %s", HTTP, expected, actual) } -} \ No newline at end of file +} From 0300cef7681a1f78388779622b33645389598302 Mon Sep 17 00:00:00 2001 From: "Miguel Osorio @Kelvur" Date: Tue, 16 Jun 2020 08:39:17 +0200 Subject: [PATCH 7/7] refactor: rename SchemeType to scheme.Type so the name is less redundant --- resolver_factory.go | 9 +++++---- resolver_factory_test.go | 8 ++++---- resource.go | 2 +- resource/locator.go | 4 ++-- resource_factory.go | 5 +++-- scheme/scheme_type.go | 20 ++++++++++---------- scheme/scheme_type_test.go | 34 +++++++++++++++++----------------- 7 files changed, 42 insertions(+), 40 deletions(-) diff --git a/resolver_factory.go b/resolver_factory.go index 557907e..14fefa2 100644 --- a/resolver_factory.go +++ b/resolver_factory.go @@ -2,20 +2,21 @@ package core import ( "errors" + "github.com/lot-sh/core/scheme" ) // ResolverFactory returns the implementation of Resolver -// which can handle the SchemeType passed as argument +// which can handle the scheme.Type passed as argument // -// • when there is not a Resolver associated to the SchemeType +// • when there is not a Resolver associated to the scheme.Type // passed as argument it will return a nil Resolver and an error -func ResolverFactory(st scheme.SchemeType) (Resolver, error) { +func ResolverFactory(st scheme.Type) (Resolver, error) { var resolver Resolver switch st { case scheme.HTTP, scheme.HTTPS: resolver = &HTTPResolver{} return resolver, nil } - return resolver, errors.New("There is not implementation known which supports the given SchemeType") + return resolver, errors.New("There is not implementation known which supports the given scheme.Type") } diff --git a/resolver_factory_test.go b/resolver_factory_test.go index 139c001..dd06766 100644 --- a/resolver_factory_test.go +++ b/resolver_factory_test.go @@ -5,16 +5,16 @@ import ( "github.com/lot-sh/core/scheme" ) -func TestResolverFactoryShouldWorksWhenPassedHTTPSchemeType(t *testing.T) { +func TestResolverFactoryShouldWorksWhenPassedHTTPType(t *testing.T) { _, err := ResolverFactory(scheme.HTTP) if err != nil { - t.Error("Error should be no returned by ResolverFactory when passed a HTTP SchemeType") + t.Error("Error should be no returned by ResolverFactory when passed a HTTP Type") } } -func TestResolverFactoryShouldReturnErrorWhenPassedUNKNOWNSchemeType(t *testing.T) { +func TestResolverFactoryShouldReturnErrorWhenPassedUNKNOWNType(t *testing.T) { _, err := ResolverFactory(scheme.UNKNOWN) if err == nil { - t.Error("Error should be returned by ResolverFactory when passed a UNKNONW SchemeType") + t.Error("Error should be returned by ResolverFactory when passed a UNKNONW Type") } } diff --git a/resource.go b/resource.go index 6a74b8b..7226810 100644 --- a/resource.go +++ b/resource.go @@ -10,7 +10,7 @@ import ( // this application, which is pieces of code and they origin type Resource struct { Locator string - Scheme scheme.SchemeType + Scheme scheme.Type } func (r *Resource) String() string { diff --git a/resource/locator.go b/resource/locator.go index b90bf5b..62966cd 100644 --- a/resource/locator.go +++ b/resource/locator.go @@ -11,7 +11,7 @@ import ( // For more details visit the RFC // https://github.com/lot-sh/docs/blob/master/rfcs/002-resource-locator.md type Locator struct { - scheme scheme.SchemeType + scheme scheme.Type path string } @@ -20,7 +20,7 @@ type Locator struct { func NewLocator(strloc string) (*Locator, error) { res := Locator{} parts := strings.Split(strloc, scheme.SEPARATOR) - res.scheme = scheme.GetSchemeTypeFrom(parts[0]) + res.scheme = scheme.GetTypeFrom(parts[0]) if res.scheme == scheme.UNKNOWN { return nil, fmt.Errorf("Failure schema detection when parsing %s", strloc) diff --git a/resource_factory.go b/resource_factory.go index cdc3092..fdf243d 100644 --- a/resource_factory.go +++ b/resource_factory.go @@ -2,18 +2,19 @@ package core import ( "errors" + "github.com/lot-sh/core/scheme" ) // ResourceFactory function returns a Resource given a locator func ResourceFactory(locator string) (Resource, error) { - var sch scheme.SchemeType = scheme.GetSchemeTypeFrom(locator) + var sch scheme.Type = scheme.GetTypeFrom(locator) var resource Resource = Resource{ locator, sch, } if sch == scheme.UNKNOWN { - return resource, errors.New("Unknown scheme, the locator may be a invalid one") + return resource, errors.New("Unknown scheme.Type, the locator may be a invalid one") } return resource, nil } diff --git a/scheme/scheme_type.go b/scheme/scheme_type.go index 8457886..092fc31 100644 --- a/scheme/scheme_type.go +++ b/scheme/scheme_type.go @@ -2,14 +2,14 @@ package scheme import "regexp" -// SchemeType type -type SchemeType int +// Type type +type Type int // Scheme types supported by this program. Also // UNKNOWN is defined to be used when the scheme is unknown // https://tools.ietf.org/html/rfc3986#section-3 const ( - FTP SchemeType = iota + FTP Type = iota GIST HTTP HTTPS @@ -22,7 +22,7 @@ const ( // ends in a formated string const SEPARATOR = ":" -// ListNames string naming mapping for each SchemeType +// ListNames string naming mapping for each Type var ListNames = []string{ "ftp", "gist", @@ -32,17 +32,17 @@ var ListNames = []string{ "lot", } -func (st SchemeType) String() string { - if st < 0 || st >= SchemeType(len(ListNames)) { +func (st Type) String() string { + if st < 0 || st >= Type(len(ListNames)) { return "unknown" } return ListNames[st] } -// GetSchemeTypeFrom try to identify the scheme of the locator -// and returns the SchemeType which correspond, can return the -// SchemeType.UNKNOWN in case it cannot identify the scheme -func GetSchemeTypeFrom(locator string) SchemeType { +// GetTypeFrom try to identify the scheme of the locator +// and returns the Type which correspond, can return the +// Type.UNKNOWN in case it cannot identify the scheme +func GetTypeFrom(locator string) Type { re := regexp.MustCompile(`^[a-z]*`) switch string(re.Find([]byte(locator))) { case "ftp": diff --git a/scheme/scheme_type_test.go b/scheme/scheme_type_test.go index 9ac072f..e5197bd 100644 --- a/scheme/scheme_type_test.go +++ b/scheme/scheme_type_test.go @@ -2,34 +2,34 @@ package scheme import "testing" -func TestGetSchemeTypeFromShouldWorkAsExpected(t *testing.T) { +func TestGetTypeFromShouldWorkAsExpected(t *testing.T) { locatorFTP := "ftp://username:pass@example.com" - if GetSchemeTypeFrom(locatorFTP) != FTP { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorFTP, GetSchemeTypeFrom(locatorFTP)) + if GetTypeFrom(locatorFTP) != FTP { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorFTP, GetTypeFrom(locatorFTP)) } locatorGist := "gist:username/path/to/file.sh" - if GetSchemeTypeFrom(locatorGist) != GIST { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorGist, GetSchemeTypeFrom(locatorGist)) + if GetTypeFrom(locatorGist) != GIST { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorGist, GetTypeFrom(locatorGist)) } locatorHTTP := "http://thelifeofbrian.com/the/best/film.sh" - if GetSchemeTypeFrom(locatorHTTP) != HTTP { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorHTTP, GetSchemeTypeFrom(locatorHTTP)) + if GetTypeFrom(locatorHTTP) != HTTP { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorHTTP, GetTypeFrom(locatorHTTP)) } locatorHTTPS := "https://gist.githubusercontent.com/Kelvur/896265085d32db2c3ab065ea0995b0a3/raw/62df0ad25eed20cdfc27235e8c39cbbbdf967ed3/2020_content.md" - if GetSchemeTypeFrom(locatorHTTPS) != HTTPS { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorHTTPS, GetSchemeTypeFrom(locatorHTTPS)) + if GetTypeFrom(locatorHTTPS) != HTTPS { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorHTTPS, GetTypeFrom(locatorHTTPS)) } locatorIPFS := "ipfs:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX" - if GetSchemeTypeFrom(locatorIPFS) != IPFS { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorIPFS, GetSchemeTypeFrom(locatorIPFS)) + if GetTypeFrom(locatorIPFS) != IPFS { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorIPFS, GetTypeFrom(locatorIPFS)) } locatorLot := "lot:QmT5NvUtoM5nWFfrQdVrFtvGfKFmG7AHE8P34isapyhCxX" - if GetSchemeTypeFrom(locatorLot) != LOT { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorLot, GetSchemeTypeFrom(locatorLot)) + if GetTypeFrom(locatorLot) != LOT { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorLot, GetTypeFrom(locatorLot)) } locatorUnknown := "monty://john.cleese/dead/parrot" - if GetSchemeTypeFrom(locatorUnknown) != UNKNOWN { - t.Errorf("The SchemeType of %s should be FTP, instead got %s", locatorUnknown, GetSchemeTypeFrom(locatorUnknown)) + if GetTypeFrom(locatorUnknown) != UNKNOWN { + t.Errorf("The Type of %s should be FTP, instead got %s", locatorUnknown, GetTypeFrom(locatorUnknown)) } } @@ -37,11 +37,11 @@ func TestCastingString(t *testing.T) { actual := FTP.String() expected := "ftp" if actual != "ftp" { - t.Errorf("The SchemeType %s should be casted as string with the value of %s, instead got %s", FTP, expected, actual) + t.Errorf("The Type %s should be casted as string with the value of %s, instead got %s", FTP, expected, actual) } actual = HTTP.String() expected = "http" if actual != "http" { - t.Errorf("The SchemeType %s should be casted as string with the value of %s, instead got %s", HTTP, expected, actual) + t.Errorf("The Type %s should be casted as string with the value of %s, instead got %s", HTTP, expected, actual) } }