I am implementing ProtocolBuffers in Swift 3, and I followed the instructions on this page https://github.com/alexeyxo/protobuf-swift
It compiles successfully but the problem is that the syntax of the code is wrong for swift 3, my protocol version is -> libprotoc 2.6.1
This is an example of my .proto
message AptitudDto{
optional int32 Id = 1;
optional string Nombre = 2;
optional string ImagenUrl = 3;
}
And i tried with this
syntax = "proto2";
message AptitudDto{
optional int32 Id = 1;
optional string Nombre = 2;
optional string ImagenUrl = 3;
}
They all give me an errors in swift 3.
You mention version 2.6.1, but there are more recent versions. If you're looking for support for Swift 3, consider upgrading.
Related
I would like to create this protobuf schema in google cloud pub/sub console.
I have a little local script where I successfully serialized and deserialized data with this schema with protobuf.
syntax = "proto2";
package mypackage;
message VideoImpression {
optional string user_id = 1;
optional string candidate_id = 2;
optional int64 event_timestamp = 3;
}
message VideoImpressionsList {
repeated VideoImpression video_impressions = 1;
}
When I save the schema I get this error:
Too many message types specified in schema definition.
I tried splitting both messages into seperate schema definitions but then it would complaint that e.g. when saving message VideoImpressionsList:
"VideoImpression" is not defined.
How can I make pub/sub to accept my above schema with two message types defined?
Thanks for any help
If you want to use one message type inside another, you should define one inside the other like this:
syntax = "proto2";
package mypackage;
message VideoImpressionsList {
message VideoImpression {
optional string user_id = 1;
optional string candidate_id = 2;
optional int64 event_timestamp = 3;
}
repeated VideoImpression video_impressions = 1;
}
I am new in using Protobuf. I have a server client communication (UDP) in C++. Now I use Protobuf to send a message which contains some Information to the Server.
package Test;
message vName{
required int32 name = 1;
}
message vNat{
required int32 nat = 1;
}
message vTan{
required int32 tan = 1;
}
message Test{
enum Type { vName = 1; vNAT = 2; vTAN = 3;}
required Type type = 1;
optional vName name = 2;
optional vNat nat = 2;
optional vTan tan = 2;
}
Now i want only send the Information which is set. For example Type is 1. Then how can i access or set the name?
Can anybody make an small snippet that i can understand how to use it?
I apologize for my english skills :D
Protobuf version: 2.5.0
OS: Windows
Enviroment: Visual Studio
Language: C++
From https://developers.google.com/protocol-buffers/docs/techniques#union
You may also want to have an enum field that identifies which message is filled in, so that you can switch on it:
message OneMessage {
enum Type { FOO = 1; BAR = 2; BAZ = 3; }
// Identifies which field is filled in.
required Type type = 1;
// One of the following will be filled in.
optional Foo foo = 2;
optional Bar bar = 3;
optional Baz baz = 4;
}
How can I use this in the Code? I think this is what I want. Have anybody an idea where i can find an example?
It sounds like what you're looking for is this, instead of using optional fields and a workaround-enum.
I'm currently using protobuf for a project (C++), when I invoke field_count, it correctly returns 5 fields, for that message, but 0 for extensions. I did extend that message, on another file, something along these lines:
File A:
package alpha.proto;
message msg {
optional ping ping_field = 10;
optional pong pong_field = 20;
extensions 100 to max;
}
File B:
package beta.proto;
import "fileA.proto";
message delta_request {
required int32 num = 10
}
extend alpha.proto.msg {
optional delta_request delta_request_field = 110;
}
...
Does anyone know what the cause for returning zero as extension_count could be? Thank you.
I just migrated my project from Swift 2.2 to Swift 3.0 with Xcode 8 beta.
I have something similar to the following code (you can paste this into a playground):
import Foundation
let datesWithCount: [(Date, Int)] = [(Date(), 1), (Date(), 2), (Date(), 3)]
let dates: [Date] = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1).map {
return $0.0
}
In Swift 2.2 this compiled fine. However, with Swift 3.0 I get the error
Ambiguous use of 'prefix'
The only way to get this to compile in Swift 3.0 is to split out the map into a separate line:
let sortedDatesWithCount = datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)
let mappedDates = sortedDatesWithCount.map {
return $0.0
}
BTW, in the actual code I'm returning NSNotification objects from the map not Dates but the error is the same. I just used Date here for making the example simple.
Is there any way to get this to compile as a one liner?
UPDATE: Created a JIRA for the Swift project.
It works if you make the ArraySlice into an Array before passing it to map:
let dates: [Date] = Array(datesWithCount.sorted {
$0.0 < $1.0
}.prefix(1)).map { return $0.0 }
This looks like a type inference bug in the compiler.
I am following the tutorial for protocol buffers and I keep running into different errors while compiling. my addressbook.proto file is in /Users/flexmaster411/protobuffer
protoc -I=/Users/flexmaster411/protobuffer --python_out= /Users/flexmaster411/protobuffer/addressbook.proto /Users/flexmaster411/protobuffer
I keep getting the following error even though I have syntax = "proto3" on my proto file
[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
Not sure if I have correctly done the destination folders set up which is causing this or not Any help appreciated
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2; // Unique ID number for this person.
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
// Our address book file is just one of these.
message AddressBook {
repeated Person people = 1;
}
It looks like you reversed the order of the parameters. /Users/flexmaster411/protobuffer is your output directory, so it should appear with --python_out. Since you specified it second, protoc thinks you're telling it that /Users/flexmaster411/protobuffer is an input. So it's trying to open a directory and then parse it as a .proto file. Amusingly, read() on a directory returns no data, which protoc interprets as a perfectly valid .proto file that simply doesn't declare anything! But it then gives you a warning because this empty file doesn't have any syntax line.
I think what you meant to type is:
protoc -I=/Users/flexmaster411/protobuffer --python_out=/Users/flexmaster411/protobuffer /Users/flexmaster411/protobuffer/addressbook.proto