File tree 2 files changed +48
-11
lines changed
2 files changed +48
-11
lines changed Original file line number Diff line number Diff line change @@ -91,18 +91,27 @@ type HALParser struct{}
91
91
92
92
// ParseLinks processes the links in a parsed response.
93
93
func (h HALParser ) ParseLinks (resp * Response ) error {
94
- hal := halBody {}
95
- if err := mapstructure .Decode (resp .Body , & hal ); err == nil {
96
- for rel , link := range hal .Links {
97
- if rel == "curies" {
98
- // TODO: handle curies at some point?
99
- continue
100
- }
94
+ entries := []interface {}{}
95
+ if l , ok := resp .Body .([]interface {}); ok {
96
+ entries = l
97
+ } else {
98
+ entries = append (entries , resp .Body )
99
+ }
101
100
102
- resp .Links [rel ] = append (resp .Links [rel ], & Link {
103
- Rel : rel ,
104
- URI : link .Href ,
105
- })
101
+ for _ , entry := range entries {
102
+ hal := halBody {}
103
+ if err := mapstructure .Decode (entry , & hal ); err == nil {
104
+ for rel , link := range hal .Links {
105
+ if rel == "curies" {
106
+ // TODO: handle curies at some point?
107
+ continue
108
+ }
109
+
110
+ resp .Links [rel ] = append (resp .Links [rel ], & Link {
111
+ Rel : rel ,
112
+ URI : link .Href ,
113
+ })
114
+ }
106
115
}
107
116
}
108
117
Original file line number Diff line number Diff line change @@ -70,6 +70,34 @@ func TestHALParser(t *testing.T) {
70
70
assert .Equal (t , r .Links ["item" ][0 ].URI , "/item" )
71
71
}
72
72
73
+ func TestHALParserArray (t * testing.T ) {
74
+ r := & Response {
75
+ Links : Links {},
76
+ Body : []interface {}{
77
+ map [string ]interface {}{
78
+ "_links" : map [string ]interface {}{
79
+ "self" : map [string ]interface {}{
80
+ "href" : "/one" ,
81
+ },
82
+ },
83
+ },
84
+ map [string ]interface {}{
85
+ "_links" : map [string ]interface {}{
86
+ "self" : map [string ]interface {}{
87
+ "href" : "/two" ,
88
+ },
89
+ },
90
+ },
91
+ },
92
+ }
93
+
94
+ p := HALParser {}
95
+ err := p .ParseLinks (r )
96
+ assert .NoError (t , err )
97
+ assert .Equal (t , r .Links ["self" ][0 ].URI , "/one" )
98
+ assert .Equal (t , r .Links ["self" ][1 ].URI , "/two" )
99
+ }
100
+
73
101
func TestTerrificallySimpleJSONParser (t * testing.T ) {
74
102
r := & Response {
75
103
Links : Links {},
You can’t perform that action at this time.
0 commit comments