Creating grpc client request with repeated fields - go

I have proto file like this:
message StartAssignmentRequest {
string additional_comment = 3;
repeated RideSlip slips = 4;
}
message RideSlip{
string slip_name = 2;
string slip_ext = 3;
string slip_link = 4;
}
Now I want to create its request and I am doing something like this:
req := &api.StartAssignmentRequest{
AdditionalComment:"AdditionalComment",
Slips: &api.RideSlip[],
}
but not having idea how I can send RideSlip data properly.

Protobuffer (both 2 and 3) repeated fields are compiled to slices in Go.
Just append to it:
req := &api.StartAssignmentRequest{
AdditionalComment: "AdditionalComment",
}
req.Slips = append(req.Slips, &api.RideSlip{
SlipName: "foo",
SlipExt: "bar",
SlipLink: "https://stackoverflow.com",
})
Or assign to it a literal value:
req := &api.StartAssignmentRequest{
AdditionalComment: "AdditionalComment",
Slips: []*api.RideSlip{
{
SlipName: "foo",
SlipExt: "bar",
SlipLink: "https://stackoverflow.com",
},
},
}

Related

Convert string in struct to []string

I have a struct as below:
type TourData struct {
ArtistID int //artist ID
RelationID string //key for relations
City string
Country string
TourDates []string
}
type MyRelation struct {
ID int `json:"id"`
DatesLocations map[string][]string `json:"datesLocations"`
}
which contains this data from a csv file:
1,nagoya-japan,Nagoya,Japan,
1,penrose-new_zealand,Penrose,New_Zealand,
1,dunedin-new_zealand,Dunedin,New_Zealand,
2,playa_del_carmen-mexico,Playa Del Carmen,Mexico,
2,papeete-french_polynesia,Papeete,French_Polynesia,
MyRelations is populated from an API which contains:
"index": [
{
"id": 1,
"datesLocations": {
"dunedin-new_zealand": [
"10-02-2020"
],
"nagoya-japan": [
"30-01-2019"
],
"penrose-new_zealand": [
"07-02-2020"
]
}
},
{
"id": 2,
"datesLocations": {
"papeete-french_polynesia": [
"16-11-2019"
],
"playa_del_carmen-mexico": [
"05-12-2019",
"06-12-2019",
"07-12-2019",
"08-12-2019",
"09-12-2019"
]
}
}
The dates come from another struct. The code I have used to populate this struct is as below:
var oneRecord TourData
var allRecords []TourData
for _, each := range csvData {
oneRecord.ArtistID, _ = strconv.Atoi(each[0])
oneRecord.RelationID = each[1]
oneRecord.City = each[2]
oneRecord.Country = each[3]
oneRecord.TourDates = Relations.Index[oneRecord.ArtistID-1].DatesLocations[each[1]]
allRecords = append(allRecords, oneRecord)
}
jsondata, err := json.Marshal(allRecords) // convert to JSON
json.Unmarshal(jsondata, &TourThings)
I need to group all the 1s together then the 2s etc. I thought to create another struct, and populate from this one but not having much luck - any ideas?
To clarify I would want say TourData.City to equal:
[Nagoya,Penrose,Dunedin]
[Playa Del Carmen, Papeete]
At the moment if I was to print TourData[0].City I would get Nagoya.
I have tried creating another struct to be populated from the TourData struct with the following fields:
type TourDataArrays struct {
ArtistID int
City []string
Country []string
TourDates [][]string
}
and then populate the struct using the code below:
var tourRecord TourDataArrays
var tourRecords []TourDataArrays
for i := 0; i < len(Relations.Index); i++ {
for j := 0; j < len(allRecords); j++ {
if allRecords[i].ArtistID == i+1 {
tourRecord.City = append(tourRecord.City, allRecords[j].City)
}
}
tourRecords = append(tourRecords, tourRecord)
}
However this is adding all the cities to one array i.e
[Nagoya, Penrose, Dunedin, Playa Del Carmen, Papeete].
If I understand your requirements correctly you needed to declare city as a string array as well. (And Country to go with it).
Check out this solution : https://go.dev/play/p/osgkbfWV3c5
Note I have not deduped country and derived city and country from one field in the Json.

Convert non typical json string to JSON

I'm fetching from a random API url and I'm getting a response like this one:
"key='jio3298', age=24, key='oijf032', age=62". How can I turn this non-json string into a list of JSON objects (i.e [{'key': 'jio3298', age: 24}, {'key':'oijf032', 'age':62}]) in an efficient way using JavaScript? I did get this code problem in an interview (one of the part of the problem. I needed that list to loop and filter based on a condition) and it seems my answer was at the very least slow.
you should use
String.prototype.split()
to create an array and then loop on this arry and create pears of key and value.
here an expmple:
const parser = (input) => {
input = input.split(",");
let keyAgeList = [],
output = [];
for (let i of input) {
i = i.replace("'", "");
i = i.replace(" ", "");
i = i.split("=");
if (i[0] === "key") {
keyAgeList[0] = i[1];
} else {
keyAgeList[1] = i[1];
}
if (keyAgeList.length > 1) {
let x = {
key: keyAgeList[0].replace("'", ""),
age: keyAgeList[1].replace("'", ""),
};
output.push(x);
keyAgeList = [];
}
}
console.log(JSON.stringify(output))
return JSON.stringify(output);
};
parser("key='jio3298', age=24, key='oijf032', age=62")

Cannot decode Caesar-Encrypted messages

I have to write a function for school that decodes Caesar-Encrypted messages. Our teacher gave us the code that encodes the messages, and asked us to write one based on the givenb function that decodes any encrypted message. (The key/shift wasn't given but was explicitly not 3 as in the the encode example, we should figure out ourselves. With online tools i know the key is 17 but for the purpose ofthe question i chose the given example with the known shift of 3)
The given Function with the Shift 3:
func main () {
var textN string = "Wenn man jetzt diesen Text Cäsar-Codieren möchte, dann macht man das so!" // Variable was given by tacher
var textC string = "Zhqq pdq mhwdw glhvhq Whbw Fävdu-Frglhuhq pöfkwh, gdqq pdfkw pdq gdv vr!" // Variable was given by tacher
fmt.Println(textN)
encode(textN)
decode(textC)
}
// The given Function with the shift 3:
func encode (text string){
var ergebnis string
for _,w:=range(text){
if w>='A' && w<='Z' {
if w+3>'Z'{
ergebnis += string((w+3)-'Z'+'A')
} else {
ergebnis += string(w+3)
}
} else if w>='a' && w<='z'{
if w+3>'z' {
ergebnis += string((w+3)-'z'+'a')
} else {
ergebnis += string(w+3)
}
} else {
ergebnis += string(w)
}
}
fmt.Println(ergebnis)
}
// My decode funtion with the "backwards shift" 3:
func decode (text string) {
var ergebnis string
for _,w:=range(text){
if w >='A' && w <='Z' {
if w-3 < 'A' {
// fmt.Println(string(w-3)) // Search for Mistakes made by me
ergebnis += string((w-3)+'Z'-'A')
} else {
// fmt.Println(string(w-3)) // Search for Mistakes made by me
ergebnis += string(w-3)
}
} else if w>='a' && w<='z'{
if w-3 < 'a' {
// fmt.Println(string(w-3)) // Search for Mistakes made by me
ergebnis += string((w-3)+'z'-'a')
} else {
// fmt.Println(string(w-3)) // Search for Mistakes made by me
ergebnis += string(w-3)
}
} else{
ergebnis += string(w)
}
}
fmt.Println(ergebnis)
}
Now the problem is: textN isn't equal to decode(textC), my code seems to fail at lowercase letters that shifted back 3 letters don't become the decoded letters they should be. I saw this at "z" and "x" and i dont have a clue why. I tried highering/lowering the shift but that didn't work, changed plusses and minusses and bigger lower signs. I don't know what to try and am thankful in advance.
there is one solution you can use, i can provide other slower one, but more interesting if you want.
func Chipher(input string, shift int) string {
bts := []rune(input)
var cursor *rune
// i em using it like this to remove repetition but its little advanced
// it may make you suspicious to teacher
shiftFn := func(start, end rune) bool {
r := *cursor
// not in range we cannot shift it
if r < start || r > end {
return false
}
res := start + (r-start+rune(shift))%(end-start)
if res < start {
res += end - start + 1
}
*cursor = res
return true
}
for i := range bts {
cursor = &bts[i]
// this is little trick, if one of expresions returns true, expressions
// after will not get executed as result would be true anyway
_ = shiftFn('a', 'z') || shiftFn('A', 'Z') || shiftFn('0', '9')
}
return string(bts)
}
now this is all beautiful but we have to do tests as well
func TestChipher(t *testing.T) {
testCases := []struct {
desc string
input, output string
shift int
}{
{
desc: "simple shift",
input: "abcd",
shift: 1,
output: "bcde",
},
{
desc: "negative shift",
input: "abcd",
shift: -1,
output: "zabc",
},
{
desc: "numbers",
input: "0123",
shift: 1,
output: "1234",
},
{
desc: "capital letters",
input: "ABCD",
shift: 1,
output: "BCDE",
},
{
desc: "big shift",
input: "ABCD",
shift: 1000,
output: "ABCD",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
res := Chipher(tC.input, tC.shift)
if res != tC.output {
t.Errorf("\n%s :result\n%s :expected", res, tC.output)
}
})
}
}
hope you will learn new things from this

How to check the value returned by a gRPC endpoint?

I am writing an application in Go that implements a gRPC API.
Here is the ProtoBuff specification that describes my endpoint's response message:
rpc MyEndpoint(RequestMessage) returns (ResponseMessage) {
option (google.api.http) = {
post: "/MyEndpoint"
body: "*"
};
}
message ResponseMessage {
message SubMessage {
int32 a = 1;
map<string, int32> b = 2;
}
string c = 1;
map<string, SubMessage> d = 2;
}
I am writing a test case to verify the response from the endpoint is what I expected it to be.
Here is my code to do that:
expectedResponse := ResponseMessage{
C: "string1",
D: map[string]*ResponseMessage_SubMessage{
"string2": &ResponseMessage_SubMessage{
A: 1,
B: map[string]int32{
"string3": 2,
},
},
},
}
fmt.Println("responseFromEndpoint = ", responseFromEndpoint)
fmt.Println("expectedResponse = ", expectedResponse)
assert.Equal(t, expectedResponse, responseFromEndpoint) // This is line #230 in file my_test_file.go
When I run this test-case, I get this output:
responseFromEndpoint = map[c:string1 d:map[string2:map[a:1 b:map[string3:2]]]]
expectedResponse = {{{} [] [] <nil>} 0 [] string1 map[string2:a:1 b:{key:"string3" value:2}]}
And the test case fails like this:
MyTestClass/MyTestCase: my_test_file.go:230:
Error Trace: my_test_file.go:230
Error: Not equal:
expected: ResponseMessage(ResponseMessage{state:impl.MessageState{NoUnkeyedLiterals:pragma.NoUnkeyedLiterals{}, DoNotCompare:pragma.DoNotCompare{}, DoNotCopy:pragma.DoNotCopy{}, atomicMessageInfo:(*impl.MessageInfo)(nil)}, sizeCache:0, unknownFields:[]uint8(nil), Name:"string1", SubMessages:map[string]*ResponseMessage_SubMessage{"string2":(*ResponseMessage_SubMessage)(0xc000098180)}})
actual : map[string]interface {}(map[string]interface {}{"c":"string1", "d":map[string]interface {}{"string2":map[string]interface {}{"a":1, "b":map[string]interface {}{"string3":2}}}})
How can I modify my test case so that I can ensure the endpoint responded with the data that I expected it to?

How to get custom field value in pb.go

There's a defined .proto file. syntax: proto3.
syntax = "proto3";
package proto;
import "google/protobuf/descriptor.proto";
extend google.protobuf.ServiceOptions {
string api_prefix = 50001;
}
extend google.protobuf.MethodOptions {
string api_url = 50002;
string api_method = 50003;
}
option go_package = "proto;proto";
service Foo {
option (api_prefix) = "/math";
rpc Add(MathRequest) returns (MathResponse) {
option (api_url) = "/:number1/:number2";
option (api_method) = "PUT";
}
}
message MathRequest {
string number1 = 1;
string number2 = 2;
}
message MathResponse {
string number = 1;
}
And there's a .pb.go file generated by protoc --go_out=. *.proto
How can I get api_url option value in services, and api_url option value in RPC method?
I got a solution myself.
var fd protoreflect.FileDescriptor
fd = pb.File_math_proto
s := fd.Services().Get(0)
m := s.Methods().Get(0).Options()
n := proto.GetExtension(m, pb.E_Http)
e := n.(*pb.HttpRule)

Resources