Skip to content

Replace Jison implementation for PathTemplates#203

Merged
michaelbausor merged 13 commits intogoogleapis:masterfrom
michaelbausor:path-templates
Aug 16, 2018
Merged

Replace Jison implementation for PathTemplates#203
michaelbausor merged 13 commits intogoogleapis:masterfrom
michaelbausor:path-templates

Conversation

@michaelbausor
Copy link
Copy Markdown
Contributor

Important changes in this PR:

  • Removes Jison (YAY!)
  • Adds new implementation for resource templates under the ResourceTemplate directory
    • The new implementation makes a hard distinction between absolute resource templates (must have leading slash, may have verb) and relative resource templates (never leading slash, never verb)
  • PathTemplate is refactored to use this new implementation, but retains the ability to contain either an absolute or relative template
  • Makes the following breaking changes in PathTemplate:
    • Removes count method and no longer implements countable interface
    • Retains leading '/' if it was present in the original $path parsed (previously this was dropped)
    • No longer support verbs that contain '/'
  • Minor changes to PathTemplate
    • Exception messages
    • String representation of * no longer changed to {$0=*}
  • Makes ValidationException a RuntimeException
  • Adds lots of test cases

When reviewing, please pay close attention to PathTemplateTest, to make sure that there are no regressions in PathTemplate behaviour, other than the expected ones.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Aug 9, 2018

Codecov Report

Merging #203 into master will increase coverage by 0.55%.
The diff coverage is 93.7%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #203      +/-   ##
=========================================
+ Coverage   84.14%   84.7%   +0.55%     
=========================================
  Files          63      63              
  Lines        2826    2353     -473     
=========================================
- Hits         2378    1993     -385     
+ Misses        448     360      -88
Impacted Files Coverage Δ
src/ApiCore/RequestBuilder.php 93.6% <100%> (ø) ⬆️
...Core/ResourceTemplate/RelativeResourceTemplate.php 100% <100%> (ø)
src/ApiCore/ResourceTemplate/Segment.php 82.22% <82.22%> (ø)
src/ApiCore/PathTemplate.php 86.66% <83.33%> (-12.35%) ⬇️
src/ApiCore/ResourceTemplate/Parser.php 91.42% <91.42%> (ø)
...Core/ResourceTemplate/AbsoluteResourceTemplate.php 97.14% <97.14%> (ø)
src/ApiCore/Serializer.php 91.89% <0%> (-1.05%) ⬇️
src/ApiCore/GapicClientTrait.php 88.88% <0%> (-0.44%) ⬇️
src/ApiCore/Middleware/AgentHeaderMiddleware.php
src/ApiCore/AgentHeaderDescriptor.php
... and 8 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4097cbe...d5b1332. Read the comment docs.

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

LGTM with minor nits

@@ -0,0 +1,143 @@
<?php
/*
* Copyright 2018, Google Inc.

This comment was marked as spam.

This comment was marked as spam.

public function __toString()
{
if ($this->hasValue()) {
return $this->value;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

* ValidationException represents a local error (i.e. not during an RPC call).
*/
class ValidationException extends Exception
class ValidationException extends RuntimeException

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


public static function isValidBinding($literal)
{
return preg_match("-^[^/=\\{\\}\\\\]+$-", $literal) === 1;

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@jdpedrie
Copy link
Copy Markdown
Contributor

Cloud Firestore supports special characters in paths which are not allowed by the jison parser or by this new parser. I added a workaround (googleapis/google-cloud-php#1122) for this a couple of months ago.

When I made that change, we had been hoping it would be a temporary workaround pending this work we knew you had on your agenda. Would it be possible to add support for optional special characters in paths?

See also #197.

@michaelbausor
Copy link
Copy Markdown
Contributor Author

@jdpedrie Yes, I think we can add such support. Are there any more detailed requirements? E.g., are there restrictions on the first character of the file name? If so, we would like to enforce them by not allowing the special characters.

@jdpedrie
Copy link
Copy Markdown
Contributor

@michaelbausor I think the only constraints for Firestore are listed under "Constraints on document IDs" found here. My tests confirm that special characters are, for instance, allowed as the first character of an ID.

@michaelbausor
Copy link
Copy Markdown
Contributor Author

Updated, PTAL

Copy link
Copy Markdown
Contributor

@tmatsuo tmatsuo left a comment

Choose a reason for hiding this comment

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

Still looking good!

Copy link
Copy Markdown
Contributor

@jdpedrie jdpedrie left a comment

Choose a reason for hiding this comment

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

looks good! nice work with the comprehensive tests. :)

@@ -32,10 +32,11 @@
namespace Google\ApiCore;

use Exception;

This comment was marked as spam.

@dwsupplee
Copy link
Copy Markdown
Contributor

Running through a review today :). Really happy to see this!

@michaelbausor michaelbausor merged commit 1c74d0f into googleapis:master Aug 16, 2018
@michaelbausor michaelbausor deleted the path-templates branch August 16, 2018 16:31
Copy link
Copy Markdown
Contributor

@dwsupplee dwsupplee left a comment

Choose a reason for hiding this comment

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

This is a really solid refactor. Nice work :)

/**
* Parses a path into an array of segments.
*
* @param $path

This comment was marked as spam.

This comment was marked as spam.

private static function parse($segmentString)
{
if ($segmentString === '*') {
return new Segment(Segment::WILDCARD_SEGMENT, null, null);

This comment was marked as spam.

This comment was marked as spam.

*/
private static function parseLiteralFromPath($literal, $path, &$index)
{
if (strlen($path) < ($index + strlen($literal))) {

This comment was marked as spam.

This comment was marked as spam.

* @param string $literal
* @return bool
*/
public static function isValidLiteral($literal)

This comment was marked as spam.

This comment was marked as spam.

* @param $binding
* @return bool
*/
public static function isValidBinding($binding)

This comment was marked as spam.

This comment was marked as spam.

* @param Segment[] $segments
* @return int
*/
private static function countDoubleWildcards($segments)

This comment was marked as spam.

This comment was marked as spam.

foreach ($nestedKeySegmentTuples as list($nestedKey, $nestedSegment)) {
/** @var Segment $nestedSegment */
// Nested variables are not allowed
assert($nestedSegment->getSegmentType() !== Segment::VARIABLE_SEGMENT);

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

// match by examining the difference in count between the template segments and the
// path pieces.

if (count($pathPieces) < count($flattenedKeySegmentTuples)) {

This comment was marked as spam.

This comment was marked as spam.

/** @var RelativeResourceTemplate|null */
private $template;

public function __construct($segmentType, $key, $value, RelativeResourceTemplate $template = null)

This comment was marked as spam.

This comment was marked as spam.

/**
* @expectedException \Google\ApiCore\ValidationException
*/
public function testMatchWildcardWithColonInMiddle()

This comment was marked as spam.

This comment was marked as spam.

@michaelbausor michaelbausor mentioned this pull request Aug 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants