Skip to content
This repository was archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
[#7] Initial support to custom links
Browse files Browse the repository at this point in the history
Signed-off-by: Wilson Júnior <[email protected]>
  • Loading branch information
wpjunior committed Jan 24, 2016
1 parent 29b7504 commit 758dca4
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 3 deletions.
6 changes: 6 additions & 0 deletions schemas/item_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,14 @@ func (schema *ItemSchema) String() string {
}

func (schema *ItemSchema) AttachDefaultLinks(baseUrl string) {
customLinks := schema.Links
defaultLinks := BuildDefaultLinks(schema.CollectionName)

schema.Links = &defaultLinks

if customLinks != nil {
schema.Links = schema.Links.ConcatenateLinks(customLinks)
}
schema.Links.ApplyBaseUrl(baseUrl)
}

Expand Down
75 changes: 75 additions & 0 deletions schemas/item_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,78 @@ func (s *S) TestNewItemSchemaWithGlobalCollectionName(c *check.C) {
c.Assert(err, check.IsNil)
c.Assert(itemSchema.GlobalCollectionName, check.Equals, true)
}

var (
DefaultLinks = Links{
&Link{Rel: "self", Href: "http://api.mysite.com/backstage-users/{id}"},
&Link{Rel: "item", Href: "http://api.mysite.com/backstage-users/{id}"},
&Link{Rel: "create", Href: "http://api.mysite.com/backstage-users", Method: "POST"},
&Link{Rel: "update", Href: "http://api.mysite.com/backstage-users/{id}", Method: "PUT"},
&Link{Rel: "delete", Href: "http://api.mysite.com/backstage-users/{id}", Method: "DELETE"},
&Link{Rel: "parent", Href: "http://api.mysite.com/backstage-users"},
}
)

func (s *S) TestAttachDefaultLinks(c *check.C) {
schema := `{
"collectionName": "backstage-users"
}`
itemSchema, err := NewItemSchemaFromReader(strings.NewReader(schema))
c.Assert(err, check.IsNil)
itemSchema.AttachDefaultLinks("http://api.mysite.com")

for i, expectedLink := range DefaultLinks {
link := *(*itemSchema.Links)[i]
c.Assert(link, check.DeepEquals, *expectedLink)
}
}

func (s *S) TestAttachDefaultLinksWithCustomLinks(c *check.C) {
schema := `{
"collectionName": "backstage-users",
"links": [
{"rel": "permissions", "href": "/backstage-permissions/{id}"}
]
}`
itemSchema, err := NewItemSchemaFromReader(strings.NewReader(schema))
c.Assert(err, check.IsNil)
itemSchema.AttachDefaultLinks("http://api.mysite.com")

lenDefaultLinks := len(DefaultLinks)
link := *(*itemSchema.Links)[lenDefaultLinks]
c.Assert(link, check.DeepEquals, Link{Rel: "permissions", Href: "http://api.mysite.com/backstage-permissions/{id}"})
}

func (s *S) TestAttachDefaultLinksWithCustomLinksWithAbsoluteLink(c *check.C) {
schema := `{
"collectionName": "backstage-users",
"links": [
{"rel": "logs", "href": "http://mylog-service/by-user/{id}"}
]
}`
itemSchema, err := NewItemSchemaFromReader(strings.NewReader(schema))
c.Assert(err, check.IsNil)
itemSchema.AttachDefaultLinks("http://api.mysite.com")

lenDefaultLinks := len(DefaultLinks)
link := *(*itemSchema.Links)[lenDefaultLinks]

c.Assert(link, check.DeepEquals, Link{Rel: "logs", Href: "http://mylog-service/by-user/{id}"})
}

func (s *S) TestAttachDefaultLinksWithCustomLinksWithTemplateLink(c *check.C) {
schema := `{
"collectionName": "backstage-users",
"links": [
{"rel": "view", "href": "{+url}"}
]
}`
itemSchema, err := NewItemSchemaFromReader(strings.NewReader(schema))
c.Assert(err, check.IsNil)
itemSchema.AttachDefaultLinks("http://api.mysite.com")

lenDefaultLinks := len(DefaultLinks)
link := *(*itemSchema.Links)[lenDefaultLinks]

c.Assert(link, check.DeepEquals, Link{Rel: "view", Href: "{+url}"})
}
38 changes: 35 additions & 3 deletions schemas/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schemas

import (
"fmt"
"net/url"
)

type Link struct {
Expand All @@ -17,12 +18,29 @@ type Link struct {

type Links []*Link

func (l *Links) ApplyBaseUrl(baseUrl string) {
for _, link := range *l {
link.Href = fmt.Sprintf("%s%s", baseUrl, link.Href)
func (l Links) ApplyBaseUrl(baseUrl string) {
for _, link := range l {
if isRelativeLink(link.Href) {
link.Href = fmt.Sprintf("%s%s", baseUrl, link.Href)
}
}
}

// ConcatenateLinks generate new links with merge with tailLinks
func (l Links) ConcatenateLinks(tailLinks *Links) *Links {
currentSize := len(l)
expandSize := len(*tailLinks)

newLinks := make(Links, currentSize+expandSize)
copy(newLinks, l)

for i, link := range *tailLinks {
newLinks[currentSize+i] = link
}

return &newLinks
}

func BuildDefaultLinks(collectionName string) Links {
collectionUrl := fmt.Sprintf("/%s", collectionName)
itemUrl := fmt.Sprintf("/%s/{id}", collectionName)
Expand All @@ -36,3 +54,17 @@ func BuildDefaultLinks(collectionName string) Links {
&Link{Rel: "parent", Href: collectionUrl},
}
}

func isRelativeLink(link string) bool {
url, err := url.Parse(link)

if err != nil {
return false
}

return url.Host == "" && url.Scheme == "" && !isUriTemplate(link)
}

func isUriTemplate(link string) bool {
return len(link) > 0 && link[0] == '{'
}

0 comments on commit 758dca4

Please sign in to comment.