Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions endpoint.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package hyperdrive

import (
"fmt"
"net/http"
"strings"

"github.com/Masterminds/semver"
"github.com/gorilla/handlers"
)

Expand Down Expand Up @@ -63,14 +65,16 @@ type Endpointer interface {
GetName() string
GetDesc() string
GetPath() string
GetVersion() string
}

// Endpoint is a basic implementation of the Endpointer interface and
// can be used directly if desired.
type Endpoint struct {
EndpointName string
EndpointDesc string
EndpointPath string
EndpointName string
EndpointDesc string
EndpointPath string
EndpointVersion *semver.Version
}

// GetName satisfies part of the Endpointer interface and returns a
Expand All @@ -94,9 +98,36 @@ func (e *Endpoint) GetPath() string {
return e.EndpointPath
}

// GetVersion returns a string representing the version.
func (e *Endpoint) GetVersion() string {
var v = fmt.Sprintf("v%d", e.EndpointVersion.Major())

if (e.EndpointVersion.Major() >= 0 && e.EndpointVersion.Minor() != 0) || (e.EndpointVersion.Major() >= 0 && e.EndpointVersion.Patch() > 0) {
v = fmt.Sprintf("%s%s%d", v, ".", e.EndpointVersion.Minor())
}

if e.EndpointVersion.Patch() != 0 {
v = fmt.Sprintf("%s%s%d", v, ".", e.EndpointVersion.Patch())
}

if e.EndpointVersion.Prerelease() != "" {
v = fmt.Sprintf("%s%s%s", v, "-", e.EndpointVersion.Prerelease())
}

return v
}

// NewEndpoint creates an instance of Endpoint.
func NewEndpoint(name string, desc string, path string) *Endpoint {
return &Endpoint{EndpointName: name, EndpointDesc: desc, EndpointPath: path}
func NewEndpoint(name string, desc string, path string, version string) *Endpoint {
var (
v *semver.Version
err error
)
v, err = semver.NewVersion(version)
if err != nil {
v, _ = semver.NewVersion("1")
}
return &Endpoint{EndpointName: name, EndpointDesc: desc, EndpointPath: path, EndpointVersion: v}
}

// GetMethods returns a slice of the methods an Endpoint supports.
Expand Down
4 changes: 4 additions & 0 deletions endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (suite *HyperdriveTestSuite) TestGetPath() {
suite.Equal("/test", suite.TestEndpoint.GetPath(), "expects GetPath() to return Path")
}

func (suite *HyperdriveTestSuite) TestGetVersion() {
suite.Equal("v1.0.1", suite.TestEndpoint.GetVersion(), "expects GetVersion() to return Version")
}

func (suite *HyperdriveTestSuite) TestEndpointer() {
suite.Implements((*Endpointer)(nil), suite.TestEndpoint, "expects an implementation of hyperdrive.Endpointer interface")
}
Expand Down
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import:
version: ^1.2.0
- package: github.com/caarlos0/env
version: ^2.1.0
- package: github.com/Masterminds/semver
version: 1.2.2
testImport:
- package: github.com/stretchr/testify
version: ^1.1.4
Expand Down
2 changes: 1 addition & 1 deletion hyperdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type HyperdriveTestSuite struct {

func (suite *HyperdriveTestSuite) SetupTest() {
suite.TestAPI = NewAPI("Test API", "Test API Desc")
suite.TestEndpoint = NewEndpoint("Test", "Test Endpoint", "/test")
suite.TestEndpoint = NewEndpoint("Test", "Test Endpoint", "/test", "1.0.1")
suite.TestHandler = NewMethodHandler(suite.TestEndpoint)
suite.TestRoot = NewRootResource(suite.TestAPI)
suite.TestEndpointRepresentation = Representation{"name": "Test", "desc": "Test Endpoint", "path": "/test", "methods": []string{"OPTIONS"}}
Expand Down