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

No error when using a not initialized message #43

Open
Nicien opened this issue May 9, 2014 · 2 comments
Open

No error when using a not initialized message #43

Nicien opened this issue May 9, 2014 · 2 comments

Comments

@Nicien
Copy link

Nicien commented May 9, 2014

When using a get*() on a not initialized member of type 'message':
- message is ignored and NO error is throw during serialisation

// proto message:
message Foo {
required int32 a = 1;
}

message Container {
optional Foo foo = 1;
}

// php code:
$c = new Container();
$c->getFoo()->setA(0);
$c->serializeToString(); // no error, empty output. why ?

adding: $c->setFoo( new Foo() ); before calling getFoo() fix the problem...

I propose you to choose between two features:

  • or an error message in getFoo(): "Please, initialize foo before using it"
  • or, like c++ protocol buffer : automatically initialize messages when getFoo() is called for the first time (better)

The actual situation is disturbing.

@chobie
Copy link
Owner

chobie commented May 9, 2014

Basically, getter method doesn't modify object. and it will return pseudo object when specified field does not initialized.

This specification is convenient for some kind of operation like below url. (I remember this spec came from c++ implementation.)
https://github.com/chobie/protoc-gen-php/blob/master/src/protocolbuffers/generator/php/FileGenerator.php#L36

$c->serializeToString(); // no error, empty output. why ?

foo field has optional attribute. getter method doesn't change current message so it will output null value.

could you use mutableFoo() method instead of getFoo() if you want to modify uninitialized child message?

<?php
require "autoload.php";

$c = new Container();
$c->mutableFoo()->setA(0);

var_dump($c->serializeToString());

The actual situation is disturbing.

Certainly. How do you think about this solution?

  • throwing an exception when user calls setter method from pseudo message.

@Nicien
Copy link
Author

Nicien commented May 10, 2014

Tanks for your answer.
mutableFoo() do exactly what i expected.

It follows the protocol buffer spec:

  • "Note that you can call mutable_email() even if email is not already set; it will be initialized to an empty string automatically."
    Great :)
  • "throwing an exception when user calls setter method from pseudo message"
    I like this solution because an error will be throw "soon enough". I mean before serialization.

I almost forgot: your library is great ! The only thing missing is a ready to use package, or a tutorial. This tutorial could explain in one page how to install php-protocolbuffers + protoc-gen-php + compozer + use composer.

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

No branches or pull requests

2 participants