From 4242e5ec3ad674cccb7d8597d3c016b68ab563bd Mon Sep 17 00:00:00 2001 From: Erez Rokah Date: Wed, 14 Dec 2022 18:59:23 +0200 Subject: [PATCH 1/2] fix: Use json.Valid (#500) So we support more types like arrays, strings, etc. --- --- schema/json.go | 19 +++++++------------ schema/json_test.go | 6 ++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/schema/json.go b/schema/json.go index 0ca3d01ee7..bd3d4b55a5 100644 --- a/schema/json.go +++ b/schema/json.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "reflect" ) @@ -55,10 +56,8 @@ func (dst *JSON) Set(src interface{}) error { *dst = JSON{Bytes: []byte(""), Status: Null} return nil } - // validate this is a valid json string - err := json.Unmarshal([]byte(value), &struct{}{}) - if err != nil { - return err + if !json.Valid([]byte(value)) { + return fmt.Errorf("invalid json: %s", value) } *dst = JSON{Bytes: []byte(value), Status: Present} case *string: @@ -69,10 +68,8 @@ func (dst *JSON) Set(src interface{}) error { *dst = JSON{Bytes: []byte(""), Status: Null} return nil } - // validate this is a valid json - err := json.Unmarshal([]byte(*value), &struct{}{}) - if err != nil { - return err + if !json.Valid([]byte(*value)) { + return fmt.Errorf("invalid json: %s", *value) } *dst = JSON{Bytes: []byte(*value), Status: Present} } @@ -85,10 +82,8 @@ func (dst *JSON) Set(src interface{}) error { return nil } - // validate this is a valid json - err := json.Unmarshal(value, &struct{}{}) - if err != nil { - return err + if !json.Valid(value) { + return fmt.Errorf("invalid json: %s", value) } *dst = JSON{Bytes: value, Status: Present} } diff --git a/schema/json_test.go b/schema/json_test.go index 084342c685..8847036848 100644 --- a/schema/json_test.go +++ b/schema/json_test.go @@ -15,7 +15,13 @@ func TestJSONSet(t *testing.T) { }{ {source: "", result: JSON{Bytes: []byte(""), Status: Null}}, {source: "{}", result: JSON{Bytes: []byte("{}"), Status: Present}}, + {source: `"test"`, result: JSON{Bytes: []byte(`"test"`), Status: Present}}, + {source: "1", result: JSON{Bytes: []byte("1"), Status: Present}}, + {source: "[1, 2, 3]", result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}}, {source: []byte("{}"), result: JSON{Bytes: []byte("{}"), Status: Present}}, + {source: []byte(`"test"`), result: JSON{Bytes: []byte(`"test"`), Status: Present}}, + {source: []byte("1"), result: JSON{Bytes: []byte("1"), Status: Present}}, + {source: []byte("[1, 2, 3]"), result: JSON{Bytes: []byte("[1, 2, 3]"), Status: Present}}, {source: ([]byte)(nil), result: JSON{Status: Null}}, {source: (*string)(nil), result: JSON{Status: Null}}, From dafc85b1006689b5cee07e1015ffe4e007a8330d Mon Sep 17 00:00:00 2001 From: CloudQuery Bot <102256036+cq-bot@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:02:58 +0200 Subject: [PATCH 2/2] chore(main): Release v1.12.4 (#502) :robot: I have created a release *beep* *boop* --- ## [1.12.4](https://github.com/cloudquery/plugin-sdk/compare/v1.12.3...v1.12.4) (2022-12-14) ### Bug Fixes * Use json.Valid ([#500](https://github.com/cloudquery/plugin-sdk/issues/500)) ([4242e5e](https://github.com/cloudquery/plugin-sdk/commit/4242e5ec3ad674cccb7d8597d3c016b68ab563bd)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aabd0b1a67..f752f3e70f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.12.4](https://github.com/cloudquery/plugin-sdk/compare/v1.12.3...v1.12.4) (2022-12-14) + + +### Bug Fixes + +* Use json.Valid ([#500](https://github.com/cloudquery/plugin-sdk/issues/500)) ([4242e5e](https://github.com/cloudquery/plugin-sdk/commit/4242e5ec3ad674cccb7d8597d3c016b68ab563bd)) + ## [1.12.3](https://github.com/cloudquery/plugin-sdk/compare/v1.12.2...v1.12.3) (2022-12-14)