Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rel, title, type, and hreflang link target attributes #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ func (b *Blog) JSONAPILinks() *Links {
},
},
},
"frenchTranslation": Link{
Href: fmt.Sprintf("https://example.com/fr-fr/blogs/%d", b.ID),
Rel: "alternate",
Title: b.Title,
Type: "text/html",
HrefLang: "fr-fr",
},
}
}

Expand Down
8 changes: 6 additions & 2 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,12 @@ func (l *Links) validate() (err error) {

// Link is used to represent a member of the `links` object.
type Link struct {
Href string `json:"href"`
Meta Meta `json:"meta,omitempty"`
Href string `json:"href"`
Rel string `json:"rel,omitempty"`
Title string `json:"title,omitempty"`
Type string `json:"type,omitempty"`
HrefLang string `json:"hreflang,omitempty"`
Meta Meta `json:"meta,omitempty"`
}

// Linkable is used to include document links in response data
Expand Down
57 changes: 57 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,63 @@ func TestSupportsLinkable(t *testing.T) {
t.Fatalf("Exepected value at '%s' to be a numeric (float64)", k)
}
}

translation, hasTranslation := links["frenchTranslation"]
if !hasTranslation {
t.Fatal("expect 'frenchTranslation' to be present")
}
translationMap, isMap := translation.(map[string]interface{})
if !isMap {
t.Fatal("Expected 'frenchTranslation' to contain a map")
}

alternateHref, hasHref := translationMap["href"]
if !hasHref {
t.Fatal("Expect 'alternate' to contain an 'href' key/value")
}
if _, isString := alternateHref.(string); !isString {
t.Fatal("Expected 'href' to contain a string")
}

alternateRel, hasRel := translationMap["rel"]
if !hasRel {
t.Fatal("Expect 'alternate' to contain an 'rel' key/value")
}
if str, isString := alternateRel.(string); !isString {
t.Fatal("Expected 'rel' to contain a string")
} else if str != "alternate" {
t.Fatal("Expected the 'rel' value to be 'alternate'")
}

alternateTitle, hasTitle := translationMap["title"]
if !hasTitle {
t.Fatal("Expect 'alternate' to contain an 'title' key/value")
}
if str, isString := alternateTitle.(string); !isString {
t.Fatal("Expected 'title' to contain a string")
} else if str != "Title 1" {
t.Fatal("Expected the 'title' value to be 'Title 1'")
}

alternateType, hasType := translationMap["type"]
if !hasType {
t.Fatal("Expect 'alternate' to contain an 'type' key/value")
}
if str, isString := alternateType.(string); !isString {
t.Fatal("Expected 'type' to contain a string")
} else if str != "text/html" {
t.Fatal("Expected the 'type' value to be 'text/html'")
}

alternateHrefLang, hasHrefLang := translationMap["hreflang"]
if !hasHrefLang {
t.Fatal("Expect 'alternate' to contain an 'hreflang' key/value")
}
if str, isString := alternateHrefLang.(string); !isString {
t.Fatal("Expected 'hreflang' to contain a string")
} else if str != "fr-fr" {
t.Fatal("Expected the 'hreflang' value to be 'fr-fr'")
}
}

func TestInvalidLinkable(t *testing.T) {
Expand Down