/** * @param \Protobuf\Compiler\Entity $entity * @param \google\protobuf\FieldDescriptorProto $field * * @return string[] */ protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field) { $name = $field->getName(); $label = $field->getLabel(); $tag = $field->getNumber(); $isRequired = $label === Label::LABEL_REQUIRED(); if (!$isRequired) { return []; } $class = $entity->getNamespacedName(); $format = 'Field "%s#%s" (tag %s) is required but has no value.'; $message = var_export(sprintf($format, $class, $name, $tag), true); $body[] = 'if ($this->' . $name . ' === null) {'; $body[] = ' throw new \\UnexpectedValueException(' . $message . ');'; $body[] = '}'; $body[] = null; return $body; }
/** * @param \Protobuf\Compiler\Entity $entity * @param google\protobuf\FieldDescriptorProto $field * * @return \Zend\Code\Generator\GeneratorInterface */ protected function generateSetterMethod(Entity $entity, FieldDescriptorProto $field) { $body = []; $fieldName = $field->getName(); $fieldType = $field->getType(); $fieldLabel = $field->getLabel(); $typeHint = $this->getTypeHint($field); if ($fieldType === Type::TYPE_BYTES() && $fieldLabel !== Label::LABEL_REPEATED()) { $body[] = 'if ($value !== null && ! $value instanceof \\Protobuf\\Stream) {'; $body[] = ' $value = \\Protobuf\\Stream::wrap($value);'; $body[] = '}'; $body[] = null; $typeHint = null; } $body[] = '$this->' . $fieldName . ' = $value;'; $methodName = $this->getAccessorName('set', $field); $method = MethodGenerator::fromArray(['name' => $methodName, 'body' => implode(PHP_EOL, $body), 'parameters' => [['name' => 'value', 'type' => $typeHint]], 'docblock' => ['shortDescription' => "Set '{$fieldName}' value", 'tags' => [['name' => 'param', 'description' => $this->getDocBlockType($field) . ' $value']]]]); $method->getDocblock()->setWordWrap(false); if ($fieldLabel !== Label::LABEL_REQUIRED()) { $method->getParameters()['value']->setDefaultValue(null); } return $method; }
public static function isPacked(\google\protobuf\FieldDescriptorProto $field) { return self::LABEL_REPEATED == $field->getLabel() && $field->getOptions()->getPacked(); }
/** * @param \Protobuf\Compiler\Entity $entity * @param \google\protobuf\FieldDescriptorProto $field * * @return string[] */ public function generateFieldWriteStatement(Entity $entity, FieldDescriptorProto $field) { $body = []; $name = $field->getName(); $type = $field->getType(); $rule = $field->getLabel(); $tag = $field->getNumber(); $options = $field->getOptions(); $variable = $this->targetVar ?: '$this->' . $name; $isPack = $options ? $options->getPacked() : false; $wire = $isPack ? WireFormat::WIRE_LENGTH : WireFormat::getWireType($type->value(), null); $key = WireFormat::getFieldKey($tag, $wire); if ($rule === Label::LABEL_REPEATED() && $isPack) { $itemValSttm = $type === Type::TYPE_ENUM() ? '$val->value()' : '$val'; $body[] = '$innerSize = 0;'; $body[] = '$calculator = $sizeContext->getSizeCalculator();'; $body[] = null; $body[] = 'foreach (' . $variable . ' as $val) {'; $body[] = ' $innerSize += ' . $this->generateValueSizeStatement($type->value(), $itemValSttm) . ';'; $body[] = '}'; $body[] = null; $body[] = '$writer->writeVarint($stream, ' . $key . ');'; $body[] = '$writer->writeVarint($stream, $innerSize);'; $body[] = null; $body[] = 'foreach (' . $variable . ' as $val) {'; $body[] = ' ' . $this->generateWriteScalarStatement($type->value(), $itemValSttm) . ';'; $body[] = '}'; return $body; } if ($type === Type::TYPE_MESSAGE() && $rule === Label::LABEL_REPEATED()) { $body[] = 'foreach (' . $variable . ' as $val) {'; $body[] = ' $writer->writeVarint($stream, ' . $key . ');'; $body[] = ' $writer->writeVarint($stream, $val->serializedSize($sizeContext));'; $body[] = ' $val->writeTo($context);'; $body[] = '}'; return $body; } if ($type === Type::TYPE_ENUM() && $rule === LABEL::LABEL_REPEATED()) { $body[] = 'foreach (' . $variable . ' as $val) {'; $body[] = ' $writer->writeVarint($stream, ' . $key . ');'; $body[] = ' ' . $this->generateWriteScalarStatement($type->value(), '$val->value()') . ';'; $body[] = '}'; return $body; } if ($rule === Label::LABEL_REPEATED()) { $body[] = 'foreach (' . $variable . ' as $val) {'; $body[] = ' $writer->writeVarint($stream, ' . $key . ');'; $body[] = ' ' . $this->generateWriteScalarStatement($type->value(), '$val') . ';'; $body[] = '}'; return $body; } if ($type === Type::TYPE_ENUM()) { $body[] = sprintf('$writer->writeVarint($stream, %s);', $key); $body[] = $this->generateWriteScalarStatement($type->value(), $variable . '->value()') . ';'; return $body; } if ($type !== Type::TYPE_MESSAGE()) { $body[] = sprintf('$writer->writeVarint($stream, %s);', $key); $body[] = $this->generateWriteScalarStatement($type->value(), $variable) . ';'; return $body; } $body[] = '$writer->writeVarint($stream, ' . $key . ');'; $body[] = '$writer->writeVarint($stream, ' . $variable . '->serializedSize($sizeContext));'; $body[] = $variable . '->writeTo($context);'; return $body; }
protected function generatePublicField(proto\FieldDescriptorProto $field, $ns, $indent) { $cmt = $this->compiler->getComment($ns . '.' . $field->getNumber(), "{$indent} * "); if ($cmt) { $cmt = "\n" . $cmt . "\n{$indent} *"; } if ($field->getLabel() === Protobuf::RULE_REPEATED) { $s[] = "/** {$cmt} @var " . $this->getJavaDocType($field) . "[] " . ($cmt ? "\n{$indent}" : '') . " */"; $s[] = 'public $' . $field->getName() . " = array();"; } else { $s[] = "/** {$cmt} @var " . $this->getJavaDocType($field) . ($cmt ? "\n{$indent}" : '') . " */"; $default = 'null'; if ($field->hasDefaultValue()) { switch ($field->getType()) { case Protobuf::TYPE_BOOL: $bool = filter_var($field->getDefaultValue(), FILTER_VALIDATE_BOOLEAN); $default = $bool ? 'true' : 'false'; break; case Protobuf::TYPE_STRING: $default = '"' . addcslashes($field->getDefaultValue(), '"\\') . '"'; break; case Protobuf::TYPE_ENUM: $default = '\\' . $this->normalizeNS($field->getTypeName()) . '::' . $field->getDefaultValue(); break; default: // Numbers $default = $field->getDefaultValue(); } } $s[] = 'public $' . $field->getName() . ' = ' . $default . ';'; } $s[] = ""; return $indent . implode(PHP_EOL . $indent, $s); }
/** * @param \Protobuf\Compiler\Entity $entity * @param \google\protobuf\FieldDescriptorProto $field * * @return string[] */ public function generateFieldReadStatement(Entity $entity, FieldDescriptorProto $field) { $body = []; $reference = null; $type = $field->getType(); $name = $field->getName(); $rule = $field->getLabel(); $tag = $field->getNumber(); $options = $field->getOptions(); $isPack = $options ? $options->getPacked() : false; $variable = $this->targetVar ?: '$this->' . $name; $breakSttm = $this->getBreakStatement($variable); if ($field->hasTypeName()) { $typeName = $field->getTypeName(); $typeEntity = $this->getEntity($typeName); $reference = $typeEntity->getNamespacedName(); } if (!$isPack) { $body[] = sprintf('\\Protobuf\\WireFormat::assertWireType($wire, %s);', $type->value()); $body[] = null; } if ($rule === Label::LABEL_REPEATED() && $isPack) { $readSttm = $type === Type::TYPE_ENUM() ? $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . ')' : $this->generateReadScalarStatement($type->value()); $body[] = '$innerSize = $reader->readVarint($stream);'; $body[] = '$innerLimit = $stream->tell() + $innerSize;'; $body[] = null; $body[] = 'if (' . $variable . ' === null) {'; $body[] = ' ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();'; $body[] = '}'; $body[] = null; $body[] = 'while ($stream->tell() < $innerLimit) {'; $body[] = ' ' . $variable . '->add(' . $readSttm . ');'; $body[] = '}'; $body[] = null; $body[] = $breakSttm; return $body; } if ($type === Type::TYPE_MESSAGE() && $rule === Label::LABEL_REPEATED()) { $body[] = '$innerSize = $reader->readVarint($stream);'; $body[] = '$innerMessage = new ' . $reference . '();'; $body[] = null; $body[] = 'if (' . $variable . ' === null) {'; $body[] = ' ' . $variable . ' = new \\Protobuf\\MessageCollection();'; $body[] = '}'; $body[] = null; $body[] = $variable . '->add($innerMessage);'; $body[] = null; $body[] = '$context->setLength($innerSize);'; $body[] = '$innerMessage->readFrom($context);'; $body[] = '$context->setLength($length);'; $body[] = null; $body[] = $breakSttm; return $body; } if ($type === Type::TYPE_ENUM() && $rule === LABEL::LABEL_REPEATED()) { $body[] = 'if (' . $variable . ' === null) {'; $body[] = ' ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();'; $body[] = '}'; $body[] = null; $body[] = $variable . '->add(' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . '));'; $body[] = null; $body[] = $breakSttm; return $body; } if ($type === Type::TYPE_MESSAGE()) { $body[] = '$innerSize = $reader->readVarint($stream);'; $body[] = '$innerMessage = new ' . $reference . '();'; $body[] = null; $body[] = $variable . ' = $innerMessage;'; $body[] = null; $body[] = '$context->setLength($innerSize);'; $body[] = '$innerMessage->readFrom($context);'; $body[] = '$context->setLength($length);'; $body[] = null; $body[] = $breakSttm; return $body; } if ($type === Type::TYPE_ENUM()) { $body[] = $variable . ' = ' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . ');'; $body[] = null; $body[] = $breakSttm; return $body; } if ($rule !== LABEL::LABEL_REPEATED()) { $body[] = $variable . ' = ' . $this->generateReadScalarStatement($type->value()) . ';'; $body[] = null; $body[] = $breakSttm; return $body; } $body[] = 'if (' . $variable . ' === null) {'; $body[] = ' ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();'; $body[] = '}'; $body[] = null; $body[] = $variable . '->add(' . $this->generateReadScalarStatement($type->value()) . ');'; $body[] = null; $body[] = $breakSttm; return $body; }
/** * Obtain the rule for the given field (repeated, optional, required). * * @param \google\protobuf\FieldDescriptorProto $field * * @return string */ protected function getFieldLabelName(FieldDescriptorProto $field) { $label = $field->getLabel()->value(); $name = Field::getLabelName($label); return $name ?: 'unknown'; }
public function generateField(proto\FieldDescriptorProto $field) { $reference = 'null'; if ($field->hasTypeName()) { $reference = $field->getTypeName(); if (substr($reference, 0, 1) !== '.') { throw new \RuntimeException('Only fully qualified names are supported: ' . $reference); } $reference = "'" . $this->normalizeNS($reference) . "'"; } $default = 'null'; if ($field->hasDefaultValue()) { switch ($field->getType()) { case Protobuf::TYPE_BOOL: $default = $field->getDefaultValue() ? 'true' : 'false'; break; case Protobuf::TYPE_STRING: $default = '"' . addcslashes($field->getDefaultValue(), '"\\') . '"'; break; case Protobuf::TYPE_ENUM: $default = $this->normalizeNS($field->getTypeName()) . '.' . $field->getDefaultValue(); break; default: // Numbers $default = $field->getDefaultValue(); } } $data = array("'" . $field->getName() . "'", $field->getLabel(), $field->getType(), $reference, $default, '{}'); return '[' . implode(', ', $data) . ']'; }
/** * @param \google\protobuf\FieldDescriptorProto $field * * @return string[] */ protected function generateFieldBody(FieldDescriptorProto $field) { $lines = []; $name = $field->getName(); $number = $field->getNumber(); $typeName = $field->getTypeName(); $extendee = $field->getExtendee(); $type = $field->getType()->name(); $label = $field->getLabel()->name(); $default = $this->getDefaultFieldValue($field); $values = ['number' => var_export($number, true), 'name' => var_export($name, true), 'type' => '\\google\\protobuf\\FieldDescriptorProto\\Type::' . $type . '()', 'label' => '\\google\\protobuf\\FieldDescriptorProto\\Label::' . $label . '()']; if ($extendee) { $values['extendee'] = var_export($extendee, true); } if ($typeName) { $values['type_name'] = var_export($typeName, true); } if ($field->hasDefaultValue()) { $values['default_value'] = $default; } $lines[] = '\\google\\protobuf\\FieldDescriptorProto::fromArray(['; $lines = array_merge($lines, $this->generateArrayLines($values)); $lines[] = ']),'; return $lines; }