Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Generating has methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hjagodzinski committed Jan 7, 2019
1 parent 3063b9d commit 38ab1dc
Show file tree
Hide file tree
Showing 43 changed files with 2,904 additions and 130 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -135,17 +135,19 @@ For each field a set of accessors is generated. The set of methods is different

* `required` / `optional`

get{FIELD}() // return field value
set{FIELD}($value) // set field value to $value
get{FIELD}() // return a field value
has{FIELD}() // check whether a field is set
set{FIELD}($value) // set a field value to $value

* `repeated`

append{FIELD}($value) // append $value value to field
append{FIELD}($value) // append $value to a field
clear{FIELD}() // empty field
get{FIELD}() // return array of field values
getAt{FIELD}($index) // return field value at $index index
getCount{FIELD}() // return number of field values
getIterator{FIELD}($index) // return ArrayIterator for field values
get{FIELD}() // return an array of field values
getAt{FIELD}($index) // return a field value at $index index
getCount{FIELD}() // return a number of field values
has{FIELD}() // check whether a field is set
getIterator{FIELD}() // return an ArrayIterator

{FIELD} is a camel cased field name.

Expand Down
42 changes: 38 additions & 4 deletions src/Allegro/Protobuf/Compiler/PhpGenerator.php
Expand Up @@ -226,19 +226,21 @@ private function _addMessageDescriptor(
$ignoredOneofs = array();

foreach ($descriptorProto->getField() as $fieldDescriptorProto) {
$oneofIndex = $fieldDescriptorProto->getOneofIndex();
if (!is_null($oneofIndex)) {
if ($fieldDescriptorProto->hasOneofIndex()) {
$oneofIndex = $fieldDescriptorProto->getOneofIndex();
if (!in_array($oneofIndex, $ignoredOneofs)) {
$oneofDescriptorProto = $descriptorProto->getOneofDeclAt($oneofIndex);
$name = $oneofDescriptorProto->getName();
Logger::warn("Ignoring '{$name}' field, "
Logger::warn("Ignoring '{$name}', "
. 'oneof is not supported (https://github.com/allegro/php-protobuf/issues/72).');
$ignoredOneofs[] = $oneofIndex;
}
} else {
$fieldDescriptor = new FieldDescriptor();
$fieldDescriptor->setName($fieldDescriptorProto->getName());
$fieldDescriptor->setDefault($fieldDescriptorProto->getDefaultValue());
if ($fieldDescriptorProto->hasDefaultValue()) {
$fieldDescriptor->setDefault($fieldDescriptorProto->getDefaultValue());
}
$fieldDescriptor->setLabel($fieldDescriptorProto->getLabel());
$fieldDescriptor->setNumber($fieldDescriptorProto->getNumber());
$fieldDescriptor->setType($fieldDescriptorProto->getType());
Expand Down Expand Up @@ -589,6 +591,22 @@ private function _describeRepeatedField(FieldDescriptor $field, CodeStringBuffer
)
->append('}');

$comment = new CommentStringBuffer(self::TAB, self::EOL);
$comment->append('Returns true if \'' . $field->getName() . '\' property is set, false otherwise')
->newline()
->appendParam('return', 'boolean');

$buffer->newline()
->append($comment)
->append('public function has' . $field->getCamelCaseName() . '()')
->append('{')
->append(
'return count($this->get(self::' . $field->getConstName() . ')) !== 0;',
false,
1
)
->append('}');

$comment = new CommentStringBuffer(self::TAB, self::EOL);
$comment->append('Returns \'' . $field->getName() . '\' iterator')
->newline()
Expand Down Expand Up @@ -714,6 +732,22 @@ private function _describeSingleField(FieldDescriptor $field, CodeStringBuffer $
}
$buffer->decreaseIdentation()
->append('}');

$comment = new CommentStringBuffer(self::TAB, self::EOL);
$comment->append('Returns true if \'' . $field->getName() . '\' property is set, false otherwise')
->newline()
->appendParam('return', 'boolean');

$buffer->newline()
->append($comment)
->append('public function has' . $field->getCamelCaseName() . '()')
->append('{')
->append(
'return $this->get(self::' . $field->getConstName() . ') !== null;',
false,
1
)
->append('}');
}

/**
Expand Down
74 changes: 72 additions & 2 deletions src/Google/Protobuf/Compiler/CodeGeneratorRequest.php
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from plugin.proto at 2016-07-08 14:22:37
* Auto generated from plugin.proto at 2019-01-07 11:34:00
*
* google.protobuf.compiler package
*/
Expand All @@ -15,6 +15,7 @@ class CodeGeneratorRequest extends \ProtobufMessage
const FILE_TO_GENERATE = 1;
const PARAMETER = 2;
const PROTO_FILE = 15;
const COMPILER_VERSION = 3;

/* @var array Field descriptors */
protected static $fields = array(
Expand All @@ -33,6 +34,11 @@ class CodeGeneratorRequest extends \ProtobufMessage
'repeated' => true,
'type' => '\Google\Protobuf\FileDescriptorProto'
),
self::COMPILER_VERSION => array(
'name' => 'compiler_version',
'required' => false,
'type' => '\Google\Protobuf\Compiler\Version'
),
);

/**
Expand All @@ -53,6 +59,7 @@ public function reset()
$this->values[self::FILE_TO_GENERATE] = array();
$this->values[self::PARAMETER] = null;
$this->values[self::PROTO_FILE] = array();
$this->values[self::COMPILER_VERSION] = null;
}

/**
Expand Down Expand Up @@ -97,6 +104,16 @@ public function getFileToGenerate()
return $this->get(self::FILE_TO_GENERATE);
}

/**
* Returns true if 'file_to_generate' property is set, false otherwise
*
* @return boolean
*/
public function hasFileToGenerate()
{
return count($this->get(self::FILE_TO_GENERATE)) !== 0;
}

/**
* Returns 'file_to_generate' iterator
*
Expand Down Expand Up @@ -148,7 +165,18 @@ public function setParameter($value)
*/
public function getParameter()
{
return $this->get(self::PARAMETER);
$value = $this->get(self::PARAMETER);
return $value === null ? (string)$value : $value;
}

/**
* Returns true if 'parameter' property is set, false otherwise
*
* @return boolean
*/
public function hasParameter()
{
return $this->get(self::PARAMETER) !== null;
}

/**
Expand Down Expand Up @@ -183,6 +211,16 @@ public function getProtoFile()
return $this->get(self::PROTO_FILE);
}

/**
* Returns true if 'proto_file' property is set, false otherwise
*
* @return boolean
*/
public function hasProtoFile()
{
return count($this->get(self::PROTO_FILE)) !== 0;
}

/**
* Returns 'proto_file' iterator
*
Expand Down Expand Up @@ -214,5 +252,37 @@ public function getProtoFileCount()
{
return $this->count(self::PROTO_FILE);
}

/**
* Sets value of 'compiler_version' property
*
* @param \Google\Protobuf\Compiler\Version $value Property value
*
* @return null
*/
public function setCompilerVersion(\Google\Protobuf\Compiler\Version $value=null)
{
return $this->set(self::COMPILER_VERSION, $value);
}

/**
* Returns value of 'compiler_version' property
*
* @return \Google\Protobuf\Compiler\Version
*/
public function getCompilerVersion()
{
return $this->get(self::COMPILER_VERSION);
}

/**
* Returns true if 'compiler_version' property is set, false otherwise
*
* @return boolean
*/
public function hasCompilerVersion()
{
return $this->get(self::COMPILER_VERSION) !== null;
}
}
}
25 changes: 23 additions & 2 deletions src/Google/Protobuf/Compiler/CodeGeneratorResponse.php
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from plugin.proto at 2016-07-08 14:22:37
* Auto generated from plugin.proto at 2019-01-07 11:34:00
*
* google.protobuf.compiler package
*/
Expand Down Expand Up @@ -77,7 +77,18 @@ public function setError($value)
*/
public function getError()
{
return $this->get(self::ERROR);
$value = $this->get(self::ERROR);
return $value === null ? (string)$value : $value;
}

/**
* Returns true if 'error' property is set, false otherwise
*
* @return boolean
*/
public function hasError()
{
return $this->get(self::ERROR) !== null;
}

/**
Expand Down Expand Up @@ -112,6 +123,16 @@ public function getFile()
return $this->get(self::FILE);
}

/**
* Returns true if 'file' property is set, false otherwise
*
* @return boolean
*/
public function hasFile()
{
return count($this->get(self::FILE)) !== 0;
}

/**
* Returns 'file' iterator
*
Expand Down
41 changes: 37 additions & 4 deletions src/Google/Protobuf/Compiler/CodeGeneratorResponse_File.php
@@ -1,6 +1,6 @@
<?php
/**
* Auto generated from plugin.proto at 2016-07-08 14:22:37
* Auto generated from plugin.proto at 2019-01-07 11:34:00
*
* google.protobuf.compiler package
*/
Expand Down Expand Up @@ -84,7 +84,18 @@ public function setName($value)
*/
public function getName()
{
return $this->get(self::NAME);
$value = $this->get(self::NAME);
return $value === null ? (string)$value : $value;
}

/**
* Returns true if 'name' property is set, false otherwise
*
* @return boolean
*/
public function hasName()
{
return $this->get(self::NAME) !== null;
}

/**
Expand All @@ -106,7 +117,18 @@ public function setInsertionPoint($value)
*/
public function getInsertionPoint()
{
return $this->get(self::INSERTION_POINT);
$value = $this->get(self::INSERTION_POINT);
return $value === null ? (string)$value : $value;
}

/**
* Returns true if 'insertion_point' property is set, false otherwise
*
* @return boolean
*/
public function hasInsertionPoint()
{
return $this->get(self::INSERTION_POINT) !== null;
}

/**
Expand All @@ -128,7 +150,18 @@ public function setContent($value)
*/
public function getContent()
{
return $this->get(self::CONTENT);
$value = $this->get(self::CONTENT);
return $value === null ? (string)$value : $value;
}

/**
* Returns true if 'content' property is set, false otherwise
*
* @return boolean
*/
public function hasContent()
{
return $this->get(self::CONTENT) !== null;
}
}
}

0 comments on commit 38ab1dc

Please sign in to comment.