From be0090294d6d64b222af75b8e57dda772c30f014 Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 2 Feb 2024 14:04:17 +0100 Subject: [PATCH 1/3] Remove usage of custom JSON unmarshaler --- resty.go | 21 --------------------- resty_test.go | 26 -------------------------- server_client.go | 2 -- store_client.go | 2 -- 4 files changed, 51 deletions(-) delete mode 100644 resty.go delete mode 100644 resty_test.go diff --git a/resty.go b/resty.go deleted file mode 100644 index 92aa9c1..0000000 --- a/resty.go +++ /dev/null @@ -1,21 +0,0 @@ -package easclient - -import ( - "github.com/go-json-experiment/json" - "gopkg.in/resty.v1" -) - -func copyRestyClient(c *resty.Client) *resty.Client { - // dereference the pointer and copy the value - cc := *c - return &cc -} - -func adaptRestyClient(c *resty.Client) { - c.JSONUnmarshal = func(data []byte, v interface{}) error { - opts := []json.Options{ - json.MatchCaseInsensitiveNames(false), - } - return unmarshalJSON(data, v, opts...) - } -} diff --git a/resty_test.go b/resty_test.go deleted file mode 100644 index a98c4da..0000000 --- a/resty_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package easclient - -import ( - "errors" - "testing" - - "github.com/stretchr/testify/require" - "gopkg.in/resty.v1" -) - -func Test_copyRestyClient(t *testing.T) { - t.Run("returns valid copy", func(t *testing.T) { - original := resty.New() - copied := copyRestyClient(original) - - require.NotSame(t, original, copied) - - t.Run("copy modifications do not affect original", func(t *testing.T) { - copied.JSONUnmarshal = func(data []byte, v interface{}) error { - return errors.New("this is some error") - } - - require.NotSame(t, original.JSONUnmarshal, copied.JSONUnmarshal) - }) - }) -} diff --git a/server_client.go b/server_client.go index 5f10755..bb31475 100644 --- a/server_client.go +++ b/server_client.go @@ -8,7 +8,5 @@ type ServerClient struct { // NewServerClient creates a new client for server interaction. func NewServerClient(c *resty.Client) *ServerClient { - c = copyRestyClient(c) - adaptRestyClient(c) return &ServerClient{c: c} } diff --git a/store_client.go b/store_client.go index 7bcc36f..52e810d 100644 --- a/store_client.go +++ b/store_client.go @@ -13,8 +13,6 @@ type StoreClient struct { // NewStoreClient creates a new client for store interaction. func NewStoreClient(c *resty.Client) *StoreClient { - c = copyRestyClient(c) - adaptRestyClient(c) return &StoreClient{c: c} } From 16dd023f3d4dd6cdb1d0d75a4287438d34041ab2 Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 2 Feb 2024 14:04:41 +0100 Subject: [PATCH 2/3] Remove json helpers and tests --- json.go | 11 ------ json_test.go | 96 ---------------------------------------------------- 2 files changed, 107 deletions(-) delete mode 100644 json.go delete mode 100644 json_test.go diff --git a/json.go b/json.go deleted file mode 100644 index 0059016..0000000 --- a/json.go +++ /dev/null @@ -1,11 +0,0 @@ -package easclient - -import "github.com/go-json-experiment/json" - -// unmarshalJSON is a wrapper around the json library we want to use for unmarshaling. -// -// Since the std library does not handle our edge cases, a specialized library is used. -// Have a look at the tests for details. -func unmarshalJSON(data []byte, v any, opts ...json.Options) error { - return json.Unmarshal(data, v, opts...) -} diff --git a/json_test.go b/json_test.go deleted file mode 100644 index 6fc84b9..0000000 --- a/json_test.go +++ /dev/null @@ -1,96 +0,0 @@ -package easclient - -import ( - stdjson "encoding/json" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -// TestUnmarshalOfSimilarKeys contains tests which show a challenge when dealing with the -// JSON representation of EAS responses: -// -// Standard fields are sometimes mixed with custom fields in the same -// object. The std lib's json package does not strictly enforce that only the field -// with a matching case is being used. Have a look at the tests and these links for details: -// -// - https://github.com/golang/go/issues/14750 -// - https://github.com/go-json-experiment/json -func TestUnmarshalOfSimilarKeys(t *testing.T) { - t.Run("std lib does unmarshal from exact key", func(t *testing.T) { - t.Run("uses last key 1", func(t *testing.T) { - attachmentStr := `{ - "id": "0dd018f8-bf23-455d-8214-44e76b24e5db", - "Id": "00000000-0000-0000-0000-000000000000" - }` - - attachment := RecordAttachment{} - - err := stdjson.Unmarshal([]byte(attachmentStr), &attachment) - require.NoError(t, err) - - // This test asserts that the std lib behaves "weired" - given that the Id struct field - // should be unmarshalled from the JSON field "id" and not "Id". - // - // In practice however, the std lib will use the last key it finds, accepting all case-variations. - assert.Equal(t, "00000000-0000-0000-0000-000000000000", attachment.Id.String()) - }) - - t.Run("uses last key 2", func(t *testing.T) { - attachmentStr := `{ - "Id": "00000000-0000-0000-0000-000000000000", - "id": "0dd018f8-bf23-455d-8214-44e76b24e5db" - }` - - attachment := RecordAttachment{} - - err := stdjson.Unmarshal([]byte(attachmentStr), &attachment) - require.NoError(t, err) - - // This test asserts that the std lib behaves "weired" - given that the Id struct field - // should be unmarshalled from the JSON field "id" and not "Id". - // - // In practice however, the std lib will use the last key it finds, accepting all case-variations. - assert.Equal(t, "0dd018f8-bf23-455d-8214-44e76b24e5db", attachment.Id.String()) - }) - }) - - t.Run("v2 json lib does unmarshal from exact key", func(t *testing.T) { - t.Run("uses correct key 1", func(t *testing.T) { - attachmentStr := `{ - "id": "0dd018f8-bf23-455d-8214-44e76b24e5db", - "Id": "00000000-0000-0000-0000-000000000000" - }` - - attachment := RecordAttachment{} - - err := unmarshalJSON([]byte(attachmentStr), &attachment) - require.NoError(t, err) - - // This test asserts that the std lib behaves "weired" - given that the Id struct field - // should be unmarshalled from the JSON field "id" and not "Id". - // - // In practice however, the std lib will use the last key it finds, accepting all case-variations. - assert.Equal(t, "0dd018f8-bf23-455d-8214-44e76b24e5db", attachment.Id.String()) - }) - - t.Run("uses correct key 2", func(t *testing.T) { - attachmentStr := `{ - "Id": "00000000-0000-0000-0000-000000000000", - "id": "0dd018f8-bf23-455d-8214-44e76b24e5db" - }` - - attachment := RecordAttachment{} - - err := unmarshalJSON([]byte(attachmentStr), &attachment) - require.NoError(t, err) - - // This test asserts that the std lib behaves "weired" - given that the Id struct field - // should be unmarshalled from the JSON field "id" and not "Id". - // - // In practice however, the std lib will use the last key it finds, accepting all case-variations. - assert.Equal(t, "0dd018f8-bf23-455d-8214-44e76b24e5db", attachment.Id.String()) - }) - }) -} From acd630b2b1d6892f19ba7f569b0e99bee4b45ea8 Mon Sep 17 00:00:00 2001 From: fabiante Date: Fri, 2 Feb 2024 14:05:06 +0100 Subject: [PATCH 3/3] Remove dependency on github.com/go-json-experiment/json --- go.mod | 1 - go.sum | 2 -- 2 files changed, 3 deletions(-) diff --git a/go.mod b/go.mod index b0fcea7..d2ad4f2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/DEXPRO-Solutions-GmbH/easclient go 1.21.4 require ( - github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0 github.com/google/uuid v1.6.0 github.com/joho/godotenv v1.5.1 github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 602a075..6b64391 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,5 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0 h1:ymLjT4f35nQbASLnvxEde4XOBL+Sn7rFuV+FOJqkljg= -github.com/go-json-experiment/json v0.0.0-20231102232822-2e55bd4e08b0/go.mod h1:6daplAwHHGbUGib4990V3Il26O0OC4aRyvewaaAihaA= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=