Skip to content

Commit f60451c

Browse files
committed
Add rel, title, type, and hreflang target attrs
1 parent 1e07b10 commit f60451c

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

models_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ func (b *Blog) JSONAPILinks() *Links {
9494
},
9595
},
9696
},
97+
"frenchTranslation": Link{
98+
Href: fmt.Sprintf("https://example.com/fr-fr/blogs/%d", b.ID),
99+
Rel: "alternate",
100+
Title: b.Title,
101+
Type: "text/html",
102+
HrefLang: "fr-fr",
103+
},
97104
}
98105
}
99106

node.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,12 @@ func (l *Links) validate() (err error) {
8787

8888
// Link is used to represent a member of the `links` object.
8989
type Link struct {
90-
Href string `json:"href"`
91-
Meta Meta `json:"meta,omitempty"`
90+
Href string `json:"href"`
91+
Rel string `json:"rel,omitempty"`
92+
Title string `json:"title,omitempty"`
93+
Type string `json:"type,omitempty"`
94+
HrefLang string `json:"hreflang,omitempty"`
95+
Meta Meta `json:"meta,omitempty"`
9296
}
9397

9498
// Linkable is used to include document links in response data

response_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,63 @@ func TestSupportsLinkable(t *testing.T) {
649649
t.Fatalf("Exepected value at '%s' to be a numeric (float64)", k)
650650
}
651651
}
652+
653+
translation, hasTranslation := links["frenchTranslation"]
654+
if !hasTranslation {
655+
t.Fatal("expect 'frenchTranslation' to be present")
656+
}
657+
translationMap, isMap := translation.(map[string]interface{})
658+
if !isMap {
659+
t.Fatal("Expected 'frenchTranslation' to contain a map")
660+
}
661+
662+
alternateHref, hasHref := translationMap["href"]
663+
if !hasHref {
664+
t.Fatal("Expect 'alternate' to contain an 'href' key/value")
665+
}
666+
if _, isString := alternateHref.(string); !isString {
667+
t.Fatal("Expected 'href' to contain a string")
668+
}
669+
670+
alternateRel, hasRel := translationMap["rel"]
671+
if !hasRel {
672+
t.Fatal("Expect 'alternate' to contain an 'rel' key/value")
673+
}
674+
if str, isString := alternateRel.(string); !isString {
675+
t.Fatal("Expected 'rel' to contain a string")
676+
} else if str != "alternate" {
677+
t.Fatal("Expected the 'rel' value to be 'alternate'")
678+
}
679+
680+
alternateTitle, hasTitle := translationMap["title"]
681+
if !hasTitle {
682+
t.Fatal("Expect 'alternate' to contain an 'title' key/value")
683+
}
684+
if str, isString := alternateTitle.(string); !isString {
685+
t.Fatal("Expected 'title' to contain a string")
686+
} else if str != "Title 1" {
687+
t.Fatal("Expected the 'title' value to be 'Title 1'")
688+
}
689+
690+
alternateType, hasType := translationMap["type"]
691+
if !hasType {
692+
t.Fatal("Expect 'alternate' to contain an 'type' key/value")
693+
}
694+
if str, isString := alternateType.(string); !isString {
695+
t.Fatal("Expected 'type' to contain a string")
696+
} else if str != "text/html" {
697+
t.Fatal("Expected the 'type' value to be 'text/html'")
698+
}
699+
700+
alternateHrefLang, hasHrefLang := translationMap["hreflang"]
701+
if !hasHrefLang {
702+
t.Fatal("Expect 'alternate' to contain an 'hreflang' key/value")
703+
}
704+
if str, isString := alternateHrefLang.(string); !isString {
705+
t.Fatal("Expected 'hreflang' to contain a string")
706+
} else if str != "fr-fr" {
707+
t.Fatal("Expected the 'hreflang' value to be 'fr-fr'")
708+
}
652709
}
653710

654711
func TestInvalidLinkable(t *testing.T) {

0 commit comments

Comments
 (0)