Skip to content
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

feat: consolidate schema options formatting #241

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# v8.3.0 (2024-09-02)
# v8.3.0 (2024-11-08)

- consolidate schema options formatting (#241)
- add support for invisible columns (#240)
- add support for change streams using Blueprint (#230)
- add support for snapshot queries (#215)
Expand Down
29 changes: 29 additions & 0 deletions src/Concerns/SharedGrammarCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

namespace Colopl\Spanner\Concerns;

use BackedEnum;
use Illuminate\Database\Grammar;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

trait SharedGrammarCalls
{
Expand All @@ -42,4 +45,30 @@ protected function wrapValue($value)
return '`' . str_replace('`', '``', $value) . '`';
}

/**
* @param array<string, scalar|BackedEnum> $options
* @param string $delimiter
* @return string
*/
protected function formatOptions(array $options, string $delimiter = '='): string
{
$mapped = Arr::map($options, function (int|float|bool|string|BackedEnum $v, string $k) use ($delimiter): string {
return Str::snake($k) . $delimiter . $this->formatOptionValue($v);
});
return implode(', ', $mapped);
}

/**
* @param scalar|BackedEnum $value
* @return string
*/
protected function formatOptionValue(mixed $value): string
{
return match (true) {
is_bool($value) => $value ? 'true' : 'false',
is_string($value) => $this->quoteString($value),
$value instanceof BackedEnum => $this->formatOptionValue($value->value),
default => (string) $value,
};
}
}
22 changes: 4 additions & 18 deletions src/Schema/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,7 @@ public function compileDropSequenceIfExists(Blueprint $blueprint, object $comman
*/
protected function formatSequenceOptions(mixed $definition): string
{
$optionAsStrings = Arr::map($definition->getOptions(), function (mixed $v, string $k): string {
return Str::snake($k) . '=' . (is_string($v) ? $this->quoteString($v) : $v);
});
return 'options (' . implode(', ', $optionAsStrings) . ')';
return 'options (' . $this->formatOptions($definition->getOptions()) . ')';
}

/**
Expand Down Expand Up @@ -330,20 +327,9 @@ protected function formatChangeStreamOptions(ChangeStreamDefinition $definition)
{
$options = $definition->getOptions();

if ($options === []) {
return '';
}

$optionAsStrings = Arr::map($options, function (mixed $v, string $k): string {
return Str::snake($k) . '=' . match (true) {
$v === true => 'true',
$v === false => 'false',
is_string($v) => $this->quoteString($v),
$v instanceof ChangeStreamValueCaptureType => $this->quoteString($v->value),
default => throw new LogicException('Unsupported option value: ' . $v),
};
});
return 'options (' . implode(', ', $optionAsStrings) . ')';
return $options !== []
? 'options (' . $this->formatOptions($options) . ')'
: '';
}

/**
Expand Down
Loading