私はログインが入力された登録フォームを持っています。入力を開始すると、ログインが利用可能かどうかがチェックされるサーバーにテキストがすぐに送信されます。Protobufのデフォルト値
回答は、それ以上のアクションを生成するかどうかに応じて、0または1、真または偽でなければなりません。 0/falseはデフォルトのフィールド値であり、送信されず、フィールドは空のままであり、フィールドはまったくありません(フィールドがデフォルト値と等しくない場合に限り、ワイヤで送信されます)。
どうすればいいですか?私は明示的に0または1の答えを得る必要があります。もちろん、私は文字列を使用しますが、それは何か間違っています。
message InputChecking {
string login = 1;
int32 loginStatus = 2;
string mail = 3;
int32 mailStatus = 4;
}
message RegistrationRequest {
...
}
message WrapperMessage {
oneof msg {
InputChecking mes_inputChecking = 1;
RegistrationRequest mes_registrationRequest = 2;
}
}
た.cpp
WrapperMessage wm; // protobuf message, is filled with data from the server
const google::protobuf::FieldDescriptor* inputCheckingField = wm.GetDescriptor()->FindFieldByName("mes_inputChecking");
if (wm.GetReflection()->HasField(wm, inputCheckingField)) // if inputCheckingField is
{
// It says that such a field is, when he receives a message from the server with loginStatus = 0, but there are no fields
const google::protobuf::FieldDescriptor* loginStatusField = wm.mes_inputchecking().GetDescriptor()->FindFieldByName("loginStatus");
if (wm.mes_inputchecking().GetReflection()->HasField(wm.mes_inputchecking(), loginStatusField))
{
// It is only called when the login is different from 0
Log("Login status = " + wm.mes_inputchecking().loginstatus());
}
}
あなたが望む振る舞いについて正確には分かっていません。 boolが必要な場合は、値がワイヤで送信されているかどうかは関係ありません。受信した値が「送信済み」の値と同じであるだけです。トライステートが必要な場合は、列挙型を使用します。これはproto2かproto3ですか? –
@RichardHodges私は一般的に値0を得ておらず、彼にはフィールドはありません(loginStatusField)。これはproto3です。列挙型を試してみます – Vadim
同じ問題、ListFieldsメソッドは値がデフォルト値と等しい場合はフィールドを返しません https://github.com/google/protobuf/issues/1772 – Vadim