-
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
Open
matthewjumpsoffbuildings
wants to merge
10
commits into
colopl:master
Choose a base branch
from
matthewjumpsoffbuildings:patch-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Cast for JSON #265
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dbc6393
Create SpannerJson.php
matthewjumpsoffbuildings b3fe074
Update SpannerJson.php
matthewjumpsoffbuildings 5fe33e4
Update README.md
matthewjumpsoffbuildings f671a3d
Update README.md
matthewjumpsoffbuildings be6792c
Update SpannerJson.php
matthewjumpsoffbuildings bd72498
Update src/Casts/SpannerJson.php
matthewjumpsoffbuildings c487dd0
Update SpannerJson.php
matthewjumpsoffbuildings b6ddc9b
Update SpannerJson.php
matthewjumpsoffbuildings ec6efc8
Update SpannerJson.php
matthewjumpsoffbuildings a8de8cf
Update README.md
matthewjumpsoffbuildings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace Colopl\Spanner\Casts; | ||
|
||
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<mixed, mixed> | ||
* */ | ||
class SpannerJson implements CastsAttributes | ||
{ | ||
public function get(Model $model, string $key, mixed $value, array $attributes) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
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 $model, string $key, mixed $value, array $attributes) | ||
{ | ||
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)]; | ||
} | ||
} | ||
|
||
|
||
class SpannerJsonType extends PgJsonb | ||
{ | ||
public function typeAnnotation(): int | ||
{ | ||
return TypeAnnotationCode::TYPE_ANNOTATION_CODE_UNSPECIFIED; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
❓ Verification inconclusive
Missing import for SpannerJsonType class.
The
SpannerJsonType
class is used but not imported. This will cause a runtime error.+use Colopl\Spanner\Query\Grammar\SpannerJsonType; namespace Colopl\Spanner\Casts;
Please verify the correct namespace and path for the
SpannerJsonType
class in your codebase.🏁 Script executed:
Length of output: 129
Verify Missing Import for SpannerJsonType
The code instantiates a new
SpannerJsonType($value)
on line 36 ofsrc/Casts/SpannerJson.php
without an import. We attempted to verify its existence using a search for a class declaration but did not find a matching definition. Please manually verify whether theSpannerJsonType
class exists in the codebase:use Colopl\Spanner\Query\Grammar\SpannerJsonType;
is unconfirmed).