-
Notifications
You must be signed in to change notification settings - Fork 16
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
base: master
Are you sure you want to change the base?
Add Cast for JSON #265
Changes from 4 commits
dbc6393
b3fe074
5fe33e4
f671a3d
be6792c
bd72498
c487dd0
b6ddc9b
ec6efc8
a8de8cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
public function set($model, $key, $value, $attributes) | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
return [$key => new SpannerJsonType($value)]; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add input validation for JSON values. The 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
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
class SpannerJsonType extends PgJsonb | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
public function typeAnnotation() | ||||||||||||||||||||||||
{ | ||||||||||||||||||||||||
return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED; | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
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.📝 Committable suggestion