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
27 changes: 19 additions & 8 deletions jsonapi/control_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,30 @@ type ControlURI struct {
}

func (u *ControlURI) UnmarshalText(text []byte) error {
if len(text) == 0 {
return fmt.Errorf("Control URI should not be empty.")
}
if text[len(text)-1] == '/' {
return fmt.Errorf("Control URI should not contains trailing slash.")
}
if a, err := url.ParseRequestURI(string(text[:])); err != nil {
if a, err := ParseControlURI(string(text[:])); err != nil {
return err
} else {
u.URL = *a
*u = *a
}
return nil
}

func (u ControlURI) MarshalJSON() ([]byte, error) {
return json.Marshal(u.String())
}

func ParseControlURI(text string) (*ControlURI, error) {
if len(text) == 0 {
return nil, fmt.Errorf("Control URI should not be empty.")
}
if text[len(text)-1] == '/' {
return nil, fmt.Errorf("Control URI should not contains trailing slash.")
}
if u, err := url.ParseRequestURI(text); err != nil {
return nil, err
} else {
return &ControlURI{
URL: *u,
}, nil
}
}
7 changes: 7 additions & 0 deletions jsonapi_test/control_uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package jsonapi_test

import (
"net/url"
"testing"

"github.com/nextmn/json-api/jsonapi"
Expand All @@ -31,4 +32,10 @@ func TestControlURI(t *testing.T) {
if err := u.UnmarshalText([]byte("http://[fd00::1]:8000")); err != nil {
t.Errorf("URI with an IPv6 address and a port should be accepted")
}

u.UnmarshalText([]byte("http://example.org"))
cmp, _ := url.ParseRequestURI("http://example.org")
if u.URL != *cmp {
t.Errorf("Valid ControlURI should be unmarshaled the same as ParseRequestURI does")
}
}
1 change: 0 additions & 1 deletion jsonapi_test/message_with_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestMessageWithError(t *testing.T) {
if err != nil {
t.Errorf("Could not marshal MessageWithError to json")
}
fmt.Println(string(j1))

if !bytes.Equal(j1, j2) {
t.Errorf("Result of marshaling MessageWithError to json is incorrect")
Expand Down
Loading