Makes Link.Param() no longer return an error

This commit is contained in:
Tom Hudson 2016-06-19 23:58:14 +01:00
parent 2d1ed05a44
commit 101119d3e5
2 changed files with 16 additions and 17 deletions

10
main.go
View File

@ -23,20 +23,20 @@ func (l Link) HasParam(key string) bool {
return false return false
} }
// Param returns the value of a parameter, or an error on failure // Param returns the value of a parameter if it exists
func (l Link) Param(key string) (string, error) { func (l Link) Param(key string) string {
for k, v := range l.Params { for k, v := range l.Params {
if key == k { if key == k {
return v, nil return v
} }
} }
return "", fmt.Errorf("Could not find param '%s'", key) return ""
} }
// String returns the string representation of a link // String returns the string representation of a link
func (l Link) String() string { func (l Link) String() string {
p := make([]string, len(l.Params)) p := make([]string, 0, len(l.Params))
for k, v := range l.Params { for k, v := range l.Params {
p = append(p, fmt.Sprintf("%s=\"%s\"", k, v)) p = append(p, fmt.Sprintf("%s=\"%s\"", k, v))
} }

View File

@ -51,17 +51,14 @@ func TestLinkMethods(t *testing.T) {
t.Errorf("Link should not have param 'foo'") t.Errorf("Link should not have param 'foo'")
} }
val, err := link.Param("pet") val := link.Param("pet")
if err != nil {
t.Errorf("Error value should be nil")
}
if val != "cat" { if val != "cat" {
t.Errorf("Link should have param pet=\"cat\"") t.Errorf("Link should have param pet=\"cat\"")
} }
_, err = link.Param("foo") val = link.Param("foo")
if err == nil { if val != "" {
t.Errorf("Error value should not be nil") t.Errorf("Link should not have value for param 'foo'")
} }
} }
@ -108,15 +105,13 @@ func TestLinkToString(t *testing.T) {
l := Link{ l := Link{
URL: "http://example.com/page/2", URL: "http://example.com/page/2",
Rel: "next", Rel: "next",
Params: map[string]string{
"foo": "bar",
},
} }
have := l.String() have := l.String()
want := "<http://example.com/page/2>; rel=\"next\""
if have != want {
t.Errorf("Want `%s`; have `%s`", want, have)
}
parsed := Parse(have) parsed := Parse(have)
if len(parsed) != 1 { if len(parsed) != 1 {
@ -130,6 +125,10 @@ func TestLinkToString(t *testing.T) {
if parsed[0].Rel != l.Rel { if parsed[0].Rel != l.Rel {
t.Errorf("Re-parsed link header should have matching rel, but has `%s`", parsed[0].Rel) t.Errorf("Re-parsed link header should have matching rel, but has `%s`", parsed[0].Rel)
} }
if parsed[0].Param("foo") != "bar" {
t.Errorf("Re-parsed link header should have foo=\"bar\" but doesn't")
}
} }
func TestLinksToString(t *testing.T) { func TestLinksToString(t *testing.T) {