how to convert protobuf readable request to proto message - binary-data

I am playing with google api for educational purpose to learn protobuf connection. But i am getting 400 Bad Request when i sending post request. I hope so my protobuf message have problem and don't understand how to i write 0: 0 message in proto file. I am using PHP Protobuf library to serialization data.
This is readable/decoded grpc post request data:
0: 0
0: 0
}
1: 0x80c2:0x3a2f
2: this is test
3: 5
7 {
9: nG86YfStD4Of9QPB_L_QDg:28441478
15: 20089
}
8 {
1 {
1: 5
}
}
9 {
1: 0
2: 1
}
11: 1
12: 1
14: 0
16 {
I created this proto messages by following above grpc request data:
syntax = "proto3";
enum firstZero {
COUNT = 0;
}
message submit {
string pid = 1;
string rtext = 2;
int32 str = 3;
.unknown_auto_data unknown = 7;
.unknown_auto_data_eight unknown_one = 8;
.unknown_auto_data_nine unknown_two = 9;
int32 unknown_int = 11;
int32 unknown_int_one = 12;
int32 unknown_int_two = 14;
string unknown_string = 16;
}
message unknown_auto_data {
string auto_data = 9;
int32 auto_int = 15;
}
message unknown_auto_data_eight {
.unknown_repeated_one unknown_re_one = 1;
}
message unknown_repeated_one {
int32 unknown_rep = 1;
}
message unknown_auto_data_nine {
int32 unknown_it = 1;
int32 unknown_itt = 2;
}
Is my proto messages are correct? or How will i write it?
Also my request model data in php:
class RequestModel {
public $jsonData = [
"pid" => "0x80c2:0x3a2f",
"rtext" => "Awesome, This is test.",
"str" => "5"
];
public function getData() {
return json_encode($this->jsonData);
}
}
Is there (online/software/method) any tools to decode grpc/protobuf request/response to readable data like above json like data?
\x01\x00\x00\x05\xc0\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\xcdVM\x88\x1be\x18\xced\xdb\xed:E\xba.......

To convert binary protobuf data to a readable form, use protoc --decode_raw. The output will not have any field names, as those are not part of the wire format. Here is an example from https://github.com/pawitp/protobuf-decoder, which also provides an online-service to do the same thing.
echo -en "\x0a\x2f\x0a\x08\x4a\x6f\x68\x6e\x20\x44\x6f\
\x65\x10\x01\x1a\x10\x6a\x6f\x68\x6e\x40\x65\x78\x61\x6d\
\x70\x6c\x65\x2e\x63\x6f\x6d\x22\x0f\x0a\x0b\x31\x31\x31\
\x2d\x32\x32\x32\x2d\x33\x33\x33\x10\x01\x0a\x1e\x0a\x08\
\x4a\x61\x6e\x65\x20\x44\x6f\x65\x10\x02\x1a\x10\x6a\x61\
\x6e\x65\x40\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d"\
| protoc --decode_raw
1 {
1: "John Doe"
2: 1
3: "john#example.com"
4 {
1: "111-222-333"
2: 1
}
}
1 {
1: "Jane Doe"
2: 2
3: "jane#example.com"
}
If you have the .proto file for your data, use protoc --decode instead.
echo -en "\x0a\x2f\x0a\x08\x4a\x6f\x68\x6e\x20\x44\x6f\
\x65\x10\x01\x1a\x10\x6a\x6f\x68\x6e\x40\x65\x78\x61\x6d\
\x70\x6c\x65\x2e\x63\x6f\x6d\x22\x0f\x0a\x0b\x31\x31\x31\
\x2d\x32\x32\x32\x2d\x33\x33\x33\x10\x01\x0a\x1e\x0a\x08\
\x4a\x61\x6e\x65\x20\x44\x6f\x65\x10\x02\x1a\x10\x6a\x61\
\x6e\x65\x40\x65\x78\x61\x6d\x70\x6c\x65\x2e\x63\x6f\x6d"\
| protoc --decode=AddressBook addresses.proto
addresses {
name: "John Doe"
id: 1
email: "john#example.com"
numbers {
number: "111-222-333"
id: 1
}
}
addresses {
name: "Jane Doe"
id: 2
email: "jane#example.com"
}
Where addresses.proto looks like this:
syntax = "proto3";
message AddressBook {
repeated Address addresses = 1;
}
message Address {
string name = 1;
int32 id = 2;
string email = 3;
repeated Number numbers = 4;
}
message Number {
string number = 1;
int32 id = 2;
}
You can also use protoc to encode from the textual representation to the binary variant:
echo 'addresses { name: "Mike", email: "mike#mike.us" }' | protoc --encode=AddressBook addresses.proto > addressbook.bin
cat addressbook.bin | protoc --decode=AddressBook addresses.proto
addresses {
name: "Mike"
email: "mike#mike.us"
}

Related

Can I include blank lines into the file when using YamlDotNet

Is there a way for me to include empty lines into the parsed YAML file using YamlDotNet? What I have currently when I parse a file like this:
node1: "1.0"
node2: "some text"
node3: "string"
I end up with this as the result:
node1: "1.0"
node2: "some text"
node3: "string"
Is there a way for me to configure the parser to not ignore blank lines?
Briefly, I'm using the YamlDotNet Parser class like so:
var input = File.OpenText(file);
var parser = new Parser(_input);
public bool Read()
{
Value = null;
Path = null;
var hasMore = _parser.MoveNext();
if (!hasMore)
{
return false;
}
parser.Current.Accept(this);
LineNumber = _parser.Current.Start.Line;
return true;
}
And in a separate class:
while (reader.Read())
{
}
EDIT:
This doesn't only happen with blank lines, it also happens when I have a line break after a dash:
Before:
-
name: Mark McGwire
hr: 65
avg: 0.278
After:
- name: Mark McGwire
hr: 65
avg: 0.278

String Newline not displaying in Doors

I have csv file containing some data like:
374,Test Comment multiplelines \n Here's the 2nd line,Other_Data
Where 374 is the object ID from doors, then some commentary and then some other data.
I have a piece of code that reads the data from the CSV file, stores it in the appropriate variables and then writes it to the doors Object.
Module Openend_module = edit("path_to_mod", true,true,true)
Object o ;
Column c;
string attrib;
string oneLine ;
string OBJECT_ID = "";
string Comment = "";
String Other_data = "";
int offset;
string split_text(string s)
{
if (findPlainText(s, sub, offset, len, false))
{
return s[0 : offset -1]
}
else
{
return ""
}
}
Stream input = read("Path_to_Input.txt");
input >> oneLine
OBJECT_ID = split_text(oneLine)
oneLine = oneLine[offset+1:]
Comment = split_text(oneLine)
Other_data = oneLine[offset+1:]
When using print Comment the output in the DXL console is : Test Comment multiplelines \n Here's the 2nd line
for o in Opened_Module do
{
if (o."Absolute Number"""==OBJECT_ID ){
attrib = "Result_Comment " 2
o.attrib = Comment
}
}
But after writing to the doors object, the \n is not taken into consideration and the result is as follows:
I've tried putting the string inside a Buffer and using stringOf() but the escape character just disappeared.
I've also tried adding \r\n and \\n to the input csv text but still no luck
This isn't the most efficient way of handling this, but I have a relatively straightforward fix.
I would suggest adding the following:
Module Openend_module = edit("path_to_mod", true,true,true)
Object o ;
Column c;
string attrib;
string oneLine ;
string OBJECT_ID = "";
string Comment = "";
String Other_data = "";
int offset;
string split_text(string s)
{
if (findPlainText(s, sub, offset, len, false))
{
return s[0 : offset -1]
}
else
{
return ""
}
}
Stream input = read("Path_to_Input.txt");
input >> oneLine
OBJECT_ID = split_text(oneLine)
oneLine = oneLine[offset+1:]
Comment = split_text(oneLine)
Other_data = oneLine[offset+1:]
//Modification to comment string
int x
int y
while ( findPlainText ( Comment , "\\n" , x , y , false ) ) {
Comment = ( Comment [ 0 : x - 1 ] ) "\n" ( Comment [ x + 2 : ] )
}
This will run the comment string through a parser, replacing string "\n" with the char '\n'. Be aware- this will ignore any trailing spaces at the end of a line.
Let me know if that helps.

How Create Sequence number in Controller action?

I am working on a project which is an E-health patient Gateway.
I want to generate a number (int type) against a submit form from View to Controller (When phlebotomist receives the blood sample he will check the box and click on submit button). This number is the patient's blood sample number that will be generated in my controller and will be saved in the database. I am using the logic below to create the number but it always gives me zero.
Kindly let me know how I can create numbers one by one in sequence and store in database?
public ActionResult AddSampleTest(int Pid, int Tid, int Stid, string Comment ,string testDate,int DotorID)
{
int sampleNumber=1;
Random rnd = new Random();
int[] sample = new int[50000];
rnd.Next();
for (int ctr = 1; ctr <= sample.Length; ctr++)
{
sampleNumber=sample[ctr + 1];
}
string sampleno = sampleNumber.ToString();
DateTime TestDate = Convert.ToDateTime(testDate);
int Recomended_Test_DoctorID = DotorID;
// Update the Sample Status
PatientTest objpatientTest = db.PatientTests.Where(x => x.PatientID == Pid && x.Testid == Tid && x.SubTestId == Stid && x.Date==TestDate &&x.DoctorId== Recomended_Test_DoctorID).FirstOrDefault();
objpatientTest.SampleStatus = "Sample Received";
objpatientTest.SampleNumber = sampleno;
db.SaveChanges();
var name = User.Identity.Name;
int Lid = db.LabAttendantRecords.Where(x => x.Name == name).Select(x => x.User_id).FirstOrDefault();
LabTestSample obj = new LabTestSample();
obj.labtestid = Tid;
obj.PatientId = Pid;
obj.subtestid = Stid;
obj.labAtendid = Lid;
obj.date = DateTime.Now.Date;
obj.sampleReceived = true;
obj.Comment = sampleno;
db.LabTestSamples.Add(obj);
db.SaveChanges();
return RedirectToAction("TestSampleNumber", "LabAttendantDashboard", new { sampleno});
// Use This for add Result
return RedirectToAction("SubTest", "LabAttendantDashboard", new { Pid, Tid, Stid });
}
sampleNumber is always 0 because each element in sample is 0.
If you want it to be a random number you need to do something like :
Random rnd = new Random();
int sampleNumber = rnd.Next();
string sampleno = sampleNumber.ToString();
If you need to be sure that the sample number is unique, you should use a Guid :
string sampleno = Guid.NewGuid().ToString();

How can I properly parse an email address with name?

I'm reading email headers (in Node.js, for those keeping score) and they are VARY varied. E-mail addresses in the to field look like:
"Jake Smart" <jake#smart.com>, jack#smart.com, "Development, Business" <bizdev#smart.com>
and a variety of other formats. Is there any way to parse all of this out?
Here's my first stab:
Run a split() on - to break up the different people into an array
For each item, see if there's a < or ".
If there's a <, then parse out the email
If there's a ", then parse out the name
For the name, if there's a ,, then split to get Last, First names.
If I first do a split on the ,, then the Development, Business will cause a split error. Spaces are also inconsistent. Plus, there may be more e-mail address formats that come through in headers that I haven't seen before. Is there any way (or maybe an awesome Node.js library) that will do all of this for me?
There's a npm module for this - mimelib (or mimelib-noiconv if you are on windows or don't want to compile node-iconv)
npm install mimelib-noiconv
And the usage would be:
var mimelib = require("mimelib-noiconv");
var addressStr = 'jack#smart.com, "Development, Business" <bizdev#smart.com>';
var addresses = mimelib.parseAddresses(addressStr);
console.log(addresses);
// [{ address: 'jack#smart.com', name: '' },
// { address: 'bizdev#smart.com', name: 'Development, Business' }]
The actual formatting for that is pretty complicated, but here is a regex that works. I can't promise it always will work though. https://www.rfc-editor.org/rfc/rfc2822#page-15
const str = "...";
const pat = /(?:"([^"]+)")? ?<?(.*?#[^>,]+)>?,? ?/g;
let m;
while (m = pat.exec(str)) {
const name = m[1];
const mail = m[2];
// Do whatever you need.
}
I'd try and do it all in one iteration (performance). Just threw it together (limited testing):
var header = "\"Jake Smart\" <jake#smart.com>, jack#smart.com, \"Development, Business\" <bizdev#smart.com>";
alert (header);
var info = [];
var current = [];
var state = -1;
var temp = "";
for (var i = 0; i < header.length + 1; i++) {
var c = header[i];
if (state == 0) {
if (c == "\"") {
current.push(temp);
temp = "";
state = -1;
} else {
temp += c;
}
} else if (state == 1) {
if (c == ">") {
current.push(temp);
info.push (current);
current = [];
temp = "";
state = -1;
} else {
temp += c;
}
} else {
if (c == "<"){
state = 1;
} else if (c == "\"") {
state = 0;
}
}
}
alert ("INFO: \n" + info);
For something complete, you should port this to JS: http://cpansearch.perl.org/src/RJBS/Email-Address-1.895/lib/Email/Address.pm
It gives you all the parts you need. The tricky bit is just the set of regexps at the start.

count number of lines in file - Scala

How would I go about counting the number of lines in a text file similar to wc -l on the unix command line in scala?
io.Source.fromFile("file.txt").getLines.size
Note that getLines returns an Iterator[String] so you aren't actually reading the whole file into memory.
Cribbing from another answer I posted:
def lineCount(f: java.io.File): Int = {
val src = io.Source.fromFile(f)
try {
src.getLines.size
} finally {
src.close()
}
}
Or, using scala-arm:
import resource._
def firstLine(f: java.io.File): Int = {
managed(io.Source.fromFile(f)) acquireAndGet { src =>
src.getLines.size
}
}
val source = Source.fromFile(new File("file")).getLines
var n = 1 ; while (source.hasNext) { printf("%d> %s", n, source.next) ; n += 1 }
val source = Source.fromFile(new File("file")).getLines
for ((line, n) <- source zipWithIndex) { printf("%d> %s", (n + 1), line) }

Resources