From dbc6393a0f418011b1c03ed156dd34e9a39d2588 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 25 Feb 2025 11:48:07 +1030 Subject: [PATCH 01/10] Create SpannerJson.php --- src/Casts/SpannerJson.php | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/Casts/SpannerJson.php diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/Casts/SpannerJson.php @@ -0,0 +1 @@ + From b3fe074699f4e12a76d288205356fa5f7479bc08 Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 25 Feb 2025 11:51:32 +1030 Subject: [PATCH 02/10] Update SpannerJson.php --- src/Casts/SpannerJson.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index 8b13789..15228f3 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -1 +1,28 @@ + new SpannerJsonType($value)]; + } +} + +class SpannerJsonType extends PgJsonb +{ + public function typeAnnotation() + { + return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED; + } +} From 5fe33e43949b1ee5544d077da0b483aaafb8cdec Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 25 Feb 2025 11:54:23 +1030 Subject: [PATCH 03/10] Update README.md --- README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index decc4f2..048096a 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,6 @@ Please note that the following are not required, but are strongly recommended fo ## Unsupported features - STRUCT data types -- Inserting/Updating JSON data types ## Limitations @@ -235,6 +234,18 @@ When fetching rows, the library coverts the following column types Note that if you execute a query without QueryBuilder, it will not have these conversions. +### JSON Type + +In order to use JSON columns, use the provided Cast on your model as below + +```php +use Colopl\Spanner\Casts\SpannerJson; + +$casts = [ + 'json_column_name' => SpannerJson::class, +] +``` + ### Partitioned DML You can run partitioned DML as below. From f671a3db1e2610b7ec454878daaabdc128d8369b Mon Sep 17 00:00:00 2001 From: Matthew Date: Tue, 25 Feb 2025 11:55:24 +1030 Subject: [PATCH 04/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 048096a..35290fc 100644 --- a/README.md +++ b/README.md @@ -241,7 +241,7 @@ In order to use JSON columns, use the provided Cast on your model as below ```php use Colopl\Spanner\Casts\SpannerJson; -$casts = [ +protected $casts = [ 'json_column_name' => SpannerJson::class, ] ``` From be6792cc2cf5a186969795227f47abd132942619 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 2 Mar 2025 20:51:43 +1030 Subject: [PATCH 05/10] Update SpannerJson.php --- src/Casts/SpannerJson.php | 48 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index 15228f3..0358016 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -1,28 +1,38 @@ + */ class SpannerJson implements CastsAttributes { - public function get($model, $key, $value, $attributes) - { - return json_decode((string) $value, true); - } - - public function set($model, $key, $value, $attributes) - { - return [$key => new SpannerJsonType($value)]; - } -} + public function get($model, $key, $value, $attributes): array|null + { + if ($value === null) { + return null; + } -class SpannerJsonType extends PgJsonb -{ - public function typeAnnotation() - { - return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED; - } + if(is_array($value)) { + return $value; + } + + if(!is_string($value)) { + throw new \InvalidArgumentException('The given value must be an array, string or null.'); + } + + return json_decode($value, true); + } + + public function set($model, $key, $value, $attributes): array + { + if (!is_array($value) && !$value instanceof JsonSerializable && $value !== null && !is_string($value)) { + throw new \InvalidArgumentException('The given value must be an array, JsonSerializable, string or null.'); + } + + return [$key => new SpannerJsonType($value)]; + } } From bd72498bda02dca911bfc563c43f231fa3d94b0c Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 2 Mar 2025 21:03:30 +1030 Subject: [PATCH 06/10] Update src/Casts/SpannerJson.php Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/Casts/SpannerJson.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index 0358016..7b054a2 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -1,6 +1,6 @@ Date: Sun, 2 Mar 2025 21:05:23 +1030 Subject: [PATCH 07/10] Update SpannerJson.php --- src/Casts/SpannerJson.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index 7b054a2..8be2aed 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -2,6 +2,8 @@ namespace Colopl\Spanner\Casts; +use Google\Cloud\Spanner\PgJsonb; +use Google\Cloud\Spanner\V1\TypeAnnotationCode; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use JsonSerializable; @@ -16,11 +18,11 @@ public function get($model, $key, $value, $attributes): array|null return null; } - if(is_array($value)) { + if (is_array($value)) { return $value; } - if(!is_string($value)) { + if (!is_string($value)) { throw new \InvalidArgumentException('The given value must be an array, string or null.'); } @@ -36,3 +38,12 @@ public function set($model, $key, $value, $attributes): array return [$key => new SpannerJsonType($value)]; } } + + +class SpannerJsonType extends PgJsonb +{ + public function typeAnnotation(): int + { + return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED; + } +} From b6ddc9b273c65412e71c56628b6734a1f130d272 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 2 Mar 2025 23:23:57 +1030 Subject: [PATCH 08/10] Update SpannerJson.php --- src/Casts/SpannerJson.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index 8be2aed..a88bd21 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -5,14 +5,15 @@ use Google\Cloud\Spanner\PgJsonb; use Google\Cloud\Spanner\V1\TypeAnnotationCode; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; +use Illuminate\Database\Eloquent\Model; use JsonSerializable; -/** - * @implements CastsAttributes - */ +/** + * @implements CastsAttributes + * */ class SpannerJson implements CastsAttributes { - public function get($model, $key, $value, $attributes): array|null + public function get(Model $model, string $key, mixed $value, array $attributes) { if ($value === null) { return null; @@ -29,9 +30,14 @@ public function get($model, $key, $value, $attributes): array|null return json_decode($value, true); } - public function set($model, $key, $value, $attributes): array + public function set(Model $model, string $key, mixed $value, array $attributes) { - if (!is_array($value) && !$value instanceof JsonSerializable && $value !== null && !is_string($value)) { + if ( + !is_array($value) && + !$value instanceof JsonSerializable && + $value !== null && + !is_string($value) + ) { throw new \InvalidArgumentException('The given value must be an array, JsonSerializable, string or null.'); } From ec6efc89421975afd35c982d2963a5208151ed24 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 3 Mar 2025 15:30:37 +1030 Subject: [PATCH 09/10] Update SpannerJson.php --- src/Casts/SpannerJson.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Casts/SpannerJson.php b/src/Casts/SpannerJson.php index a88bd21..a88ccfe 100644 --- a/src/Casts/SpannerJson.php +++ b/src/Casts/SpannerJson.php @@ -6,7 +6,6 @@ use Google\Cloud\Spanner\V1\TypeAnnotationCode; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Database\Eloquent\Model; -use JsonSerializable; /** * @implements CastsAttributes @@ -15,16 +14,16 @@ class SpannerJson implements CastsAttributes { public function get(Model $model, string $key, mixed $value, array $attributes) { - if ($value === null) { + if($value === null) { return null; } - if (is_array($value)) { - return $value; + if($value instanceof SpannerJsonType) { + $value = (string) $value; } - if (!is_string($value)) { - throw new \InvalidArgumentException('The given value must be an array, string or null.'); + if(!is_string($value)) { + throw new \InvalidArgumentException('The given value must be a string, SpannerJsonType or null'); } return json_decode($value, true); @@ -34,7 +33,7 @@ public function set(Model $model, string $key, mixed $value, array $attributes) { if ( !is_array($value) && - !$value instanceof JsonSerializable && + !$value instanceof \JsonSerializable && $value !== null && !is_string($value) ) { From a8de8cfc2709435b9df48f65059f409b5535e882 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 3 Mar 2025 16:38:24 +1030 Subject: [PATCH 10/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 35290fc..d68b03d 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,7 @@ use Colopl\Spanner\Casts\SpannerJson; protected $casts = [ 'json_column_name' => SpannerJson::class, -] +]; ```