File tree 3 files changed +26
-9
lines changed
3 files changed +26
-9
lines changed Original file line number Diff line number Diff line change @@ -16,19 +16,30 @@ type ControlURI struct {
16
16
}
17
17
18
18
func (u * ControlURI ) UnmarshalText (text []byte ) error {
19
- if len (text ) == 0 {
20
- return fmt .Errorf ("Control URI should not be empty." )
21
- }
22
- if text [len (text )- 1 ] == '/' {
23
- return fmt .Errorf ("Control URI should not contains trailing slash." )
24
- }
25
- if a , err := url .ParseRequestURI (string (text [:])); err != nil {
19
+ if a , err := ParseControlURI (string (text [:])); err != nil {
26
20
return err
27
21
} else {
28
- u . URL = * a
22
+ * u = * a
29
23
}
30
24
return nil
31
25
}
26
+
32
27
func (u ControlURI ) MarshalJSON () ([]byte , error ) {
33
28
return json .Marshal (u .String ())
34
29
}
30
+
31
+ func ParseControlURI (text string ) (* ControlURI , error ) {
32
+ if len (text ) == 0 {
33
+ return nil , fmt .Errorf ("Control URI should not be empty." )
34
+ }
35
+ if text [len (text )- 1 ] == '/' {
36
+ return nil , fmt .Errorf ("Control URI should not contains trailing slash." )
37
+ }
38
+ if u , err := url .ParseRequestURI (text ); err != nil {
39
+ return nil , err
40
+ } else {
41
+ return & ControlURI {
42
+ URL : * u ,
43
+ }, nil
44
+ }
45
+ }
Original file line number Diff line number Diff line change 6
6
package jsonapi_test
7
7
8
8
import (
9
+ "net/url"
9
10
"testing"
10
11
11
12
"github.com/nextmn/json-api/jsonapi"
@@ -31,4 +32,10 @@ func TestControlURI(t *testing.T) {
31
32
if err := u .UnmarshalText ([]byte ("http://[fd00::1]:8000" )); err != nil {
32
33
t .Errorf ("URI with an IPv6 address and a port should be accepted" )
33
34
}
35
+
36
+ u .UnmarshalText ([]byte ("http://example.org" ))
37
+ cmp , _ := url .ParseRequestURI ("http://example.org" )
38
+ if u .URL != * cmp {
39
+ t .Errorf ("Valid ControlURI should be unmarshaled the same as ParseRequestURI does" )
40
+ }
34
41
}
Original file line number Diff line number Diff line change @@ -36,7 +36,6 @@ func TestMessageWithError(t *testing.T) {
36
36
if err != nil {
37
37
t .Errorf ("Could not marshal MessageWithError to json" )
38
38
}
39
- fmt .Println (string (j1 ))
40
39
41
40
if ! bytes .Equal (j1 , j2 ) {
42
41
t .Errorf ("Result of marshaling MessageWithError to json is incorrect" )
You can’t perform that action at this time.
0 commit comments