Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Commit 617b6a4

Browse files
authored
Merge pull request #83 from ericchiang/json-errors
*: fix deserialization of JSON errors
2 parents fc8e341 + 3790825 commit 617b6a4

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test-examples:
1515
generate: _output/kubernetes _output/bin/protoc _output/bin/gomvpkg _output/bin/protoc-gen-gofast _output/src/github.com/golang/protobuf
1616
./scripts/generate.sh
1717
go run scripts/register.go
18-
cp scripts/time.go.partial apis/meta/v1/time.go
18+
cp scripts/json.go.partial apis/meta/v1/json.go
1919

2020
.PHONY: verify-generate
2121
verify-generate: generate

apis/meta/v1/time.go renamed to apis/meta/v1/json.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"time"
66
)
77

8-
// JSON marshaling logic for the Time type. Need to make
9-
// third party resources JSON work.
8+
// JSON marshaling logic for the Time type so it can be used for custom
9+
// resources, which serialize to JSON.
10+
1011
func (t Time) MarshalJSON() ([]byte, error) {
1112
var seconds, nanos int64
1213
if t.Seconds != nil {
@@ -29,3 +30,19 @@ func (t *Time) UnmarshalJSON(p []byte) error {
2930
t.Nanos = &nanos
3031
return nil
3132
}
33+
34+
// Status must implement json.Unmarshaler for the codec to deserialize a JSON
35+
// payload into it.
36+
//
37+
// See https://github.com/ericchiang/k8s/issues/82
38+
39+
type jsonStatus Status
40+
41+
func (s *Status) UnmarshalJSON(data []byte) error {
42+
var j jsonStatus
43+
if err := json.Unmarshal(data, &j); err != nil {
44+
return err
45+
}
46+
*s = Status(j)
47+
return nil
48+
}

client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ func Test404(t *testing.T) {
210210
})
211211
}
212212

213+
func Test404JSON(t *testing.T) {
214+
withNamespace(t, func(client *k8s.Client, namespace string) {
215+
// Use a JSON object to ensure JSON payload errors deserialize correctly.
216+
var configMap configMapJSON
217+
err := client.Get(context.TODO(), namespace, "i-dont-exist", &configMap)
218+
if err == nil {
219+
t.Errorf("expected 404 error")
220+
return
221+
}
222+
apiErr, ok := err.(*k8s.APIError)
223+
if !ok {
224+
t.Errorf("error was not of type APIError: %T %v", err, err)
225+
return
226+
}
227+
if apiErr.Code != 404 {
228+
t.Errorf("expected 404 error code, got %d", apiErr.Code)
229+
}
230+
})
231+
}
232+
213233
func TestLabelSelector(t *testing.T) {
214234
withNamespace(t, func(client *k8s.Client, namespace string) {
215235
for i := 0; i < 5; i++ {

codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func unmarshal(data []byte, contentType string, i interface{}) error {
4848
// only decode into JSON of a protobuf message if the type
4949
// explicitly implements json.Unmarshaler
5050
if _, ok := i.(json.Unmarshaler); !ok {
51-
return errors.New("cannot decode json payload into protobuf object")
51+
return fmt.Errorf("cannot decode json payload into protobuf object %T", i)
5252
}
5353
}
5454
if err := json.Unmarshal(data, i); err != nil {

scripts/time.go.partial renamed to scripts/json.go.partial

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"time"
66
)
77

8-
// JSON marshaling logic for the Time type. Need to make
9-
// third party resources JSON work.
8+
// JSON marshaling logic for the Time type so it can be used for custom
9+
// resources, which serialize to JSON.
10+
1011
func (t Time) MarshalJSON() ([]byte, error) {
1112
var seconds, nanos int64
1213
if t.Seconds != nil {
@@ -29,3 +30,19 @@ func (t *Time) UnmarshalJSON(p []byte) error {
2930
t.Nanos = &nanos
3031
return nil
3132
}
33+
34+
// Status must implement json.Unmarshaler for the codec to deserialize a JSON
35+
// payload into it.
36+
//
37+
// See https://github.com/ericchiang/k8s/issues/82
38+
39+
type jsonStatus Status
40+
41+
func (s *Status) UnmarshalJSON(data []byte) error {
42+
var j jsonStatus
43+
if err := json.Unmarshal(data, &j); err != nil {
44+
return err
45+
}
46+
*s = Status(j)
47+
return nil
48+
}

0 commit comments

Comments
 (0)