Skip to content

Add Cast for JSON #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;

protected $casts = [
'json_column_name' => SpannerJson::class,
]
```


### Partitioned DML
You can run partitioned DML as below.
Expand Down
28 changes: 28 additions & 0 deletions src/Casts/SpannerJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Colopl\Spanner\Casts;

use Google\Cloud\Spanner\PgJsonb;
use Google\Cloud\Spanner\V1\TypeAnnotationCode;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class SpannerJson implements CastsAttributes
{
public function get($model, $key, $value, $attributes)
{
return json_decode((string) $value, true);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for JSON decoding.

The get method should handle potential JSON decoding errors and null values explicitly.

 public function get($model, $key, $value, $attributes)
 {
-    return json_decode((string) $value, true);
+    if ($value === null) {
+        return null;
+    }
+    $decoded = json_decode((string) $value, true);
+    if (json_last_error() !== JSON_ERROR_NONE) {
+        throw new \JsonException(json_last_error_msg());
+    }
+    return $decoded;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function get($model, $key, $value, $attributes)
{
return json_decode((string) $value, true);
}
public function get($model, $key, $value, $attributes)
{
if ($value === null) {
return null;
}
$decoded = json_decode((string) $value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \JsonException(json_last_error_msg());
}
return $decoded;
}


public function set($model, $key, $value, $attributes)
{
return [$key => new SpannerJsonType($value)];
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add input validation for JSON values.

The set method should validate the input value to ensure it can be encoded as JSON.

 public function set($model, $key, $value, $attributes)
 {
+    if ($value !== null && !is_array($value) && !is_object($value)) {
+        throw new \InvalidArgumentException('The given value must be array, object or null.');
+    }
     return [$key => new SpannerJsonType($value)];
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function set($model, $key, $value, $attributes)
{
return [$key => new SpannerJsonType($value)];
}
public function set($model, $key, $value, $attributes)
{
if ($value !== null && !is_array($value) && !is_object($value)) {
throw new \InvalidArgumentException('The given value must be array, object or null.');
}
return [$key => new SpannerJsonType($value)];
}

}

class SpannerJsonType extends PgJsonb
{
public function typeAnnotation()
{
return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED;
}
}
Loading