How to serialise payment details in google protocol buffers? - protocol-buffers

In particular I'm trying to successfully return a payment object to the bitcoin client,
this line of code:
required bytes serialized_payment_details = 4;
in the PaymentRequest message of .proto is required but I don't know how to generate the serialised payment details or even what it means to be honest?
Thanks in advance for any help :)

all that does is declare that field 4 should hold a blob - a sequence of raw data. No meaning, translation or intent is provided for that, so all processing must be done externally to protocol buffers. As for how to serialize it: that comes down to bitcoin and whatever bitcoin library / tools you are using.

The answer was:
serialized_payment_details = PaymentDetailsObject.SerializeToString()

Related

Go-Mail multiple recipients and attachment

I am trying to make an email service using go-mail library and made it working. However there are few challenges that i am unable to solve
I have the struct for form data-
type SendMailBody struct {
EmailTo []string `form:"sendTo"`
Subject string `form:"subject"`
Body string `form:"body"`
}
The form data that i am sending to the API is
subject: Notification
sendTo:["abc#gmail.com", "xyz#gmail.com"]
body:You have been notified
Challenges-
If I pass a single email in "sendTO", It is working fine. But after passing the slice of emails, it is unable to send emails to the recepients. How can i make it work?
If I pass the attachment through Form data, how can I attach it with the mail. In documentation, it is mentioned that we can do it like that "m.Attach("/tmp/image.jpg")" . But how should i do it if i pass the attachment via form data in API
Please guide me through that.
Some more details are needed to help here. In particular which go-mail are you using?
For 1.)
If you refer to https://github.com/wneessen/go-mail, using Msg.To() should work fine with multiple recipient. See the documentation at: https://pkg.go.dev/github.com/wneessen/go-mail#Msg.To
If you refer to https://github.com/go-mail/mail, there is Message.SetAddressHeader() (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetAddressHeader) which does not support multiple recipient addresses. You would need to use Message.SetHeaders() for the "To"-header instead (https://pkg.go.dev/github.com/go-mail/mail?utm_source=godoc#Message.SetHeaders).
For 2.)
This totally depends on how you read the attachment data (and again also on the go-mail library you are using). https://github.com/wneessen/go-mail has different ways of attaching and embedding files (i. e. from a local file, from embedFS, from an io.Reader...)

What is the `data` object passed to a Codec's `decode` method?

I'd like to write a Codec plugin to enable LogStash to decode a binary data format.
The official documentation for writing a Codec shows that I need to define a decode method that accepts a single parameter: a variable called data.
I'm new to both LogStash and Ruby. Having worked mostly with statically typed languages, I'm unsure how to learn more about the data variable. I assume that it's analogous to an InputStream-type object, allowing me to read data as it becomes available, but I'm not sure.
Questions:
What type is the data object? What methods does it have?
How do Ruby developers typically go about investigating variables like this? I'm not sure I see a way to figure it out without writing a skeleton plugin and dumping a string representation of data to STDOUT.
Thanks!
The documentation for writing an input plugin hints at this. From the run() method section:
data = $stdin.sysread(16384)
#codec.decode(data) do |event|
decorate(event)
event.set("host", #host) if !event.include?("host")
queue << event
end
The data variable is a Ruby String, which is being used as a buffer of arbitrary bytes. I have verified this by creating a skeleton plugin and inspecting the value at runtime.
This seems to be cause for caution: the bytes provided to your codec's decode method are not guaranteed to be a complete event.

Updating data from protobuf

I'm building a microservice system with multiple disconnected components, and I'm currently trying to find out how to implement knowing which fields on an object should be updated based on the protobuf data provided.
The flow is this:
The client sends a JSON-request to an API.
The API translates the JSON-data into a protobuf struct, which is then sent along to the microservice responsible for handling it.
The microservice receives the data from the API and performs any action on it, in this case, I'm trying to change a single value in a MySQL table, such as a client's email address.
Now, the problem I have is that since protobuf (understandably) doesn't allow pointers, the protobuf object will contain zero-values for everything not provided. This means that if a customer wants to update their email address, I can't know if they also set IncludeInMailLists to false - or if it was simply not provided (having its zero-value) and shouldn't change.
The question is: how will I - from the protobuf object - know if a value is expressively set to 0, or just not provided?
My current solution is pretty much having a special UpdateCustomer-object which also has an array of Fields specifying which fields the microservice should care about, but it feels like bad solution.
Someone must have solved this better already. How should I implement it?
Protobufs field masks are one way.
https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.FieldMask
https://github.com/golang/protobuf/issues/225
But if you are using grpc then there's a (sort of) built in way.
Grpc wrappers
Since proto3 (protobufs v3) there's been no distinction between a primitive that is not set, and a primitive that's been set to the "zero value" (false, 0, "", etc).
Instead you can use objects or in protobufs language a "message", as objects can be nil / null. You've not mentioned what language you are working in but hopefully these examples make sense.
Given an RPC service such as:
import "google/protobuf/wrappers.proto";
service Users {
rpc UpdateUser(UpdateUserRequest) returns (UpdateUserResponse)
}
message UpdateUserRequest {
int32 user_id = 1;
google.protobuf.StringValue email = 2;
}
message UpdateUserResponse {}
Note the import "google/protobuf/wrappers.proto"; is important.
It given you access to the google protobufs wrappers source code here. These are not objects that have methods that allow you to test for presence.
Grpc generated code in java gives you methods such as .hasEmail() which returns true if the value is present. The getter on an unset value will still return you the zero value. I think the golang version uses pointers that you can test for nil instead of an explicit hasX() method.
More info / discussion in this github issue

How to update the "Replacement Value" in ReplaceText Processor using Rest API?

I need to know how to update the values in nifi processors using Rest API.
https://nifi.apache.org/docs/nifi-docs/rest-api/index.html
For example: I have used below processor structure
GetFile>SplitText>ExtractText>ReplaceText>ConvertJSONToSQL>PUTSQL.
I have passed following inputs for above processors.,
FileLocation(GetFile).
validation(ExtractText).
ReplacementValue(ReplaceText).
DBCP ConnectionPool,username and pwd for SQL.
I just need to use nifi rest api client to write above inputs into processors.
For example : If I give Processor name and input file in Rest API Client then it will write into processor.
Please stop me if anything i'm doing wrong.
Help Appreciated and Tell me any other ways is possible?
Mahen,
You can issue a PUT request to /processors/{id} and provide the new value of the "Replacement Value" property. You'll need to provide JSON body in the request to do this, and you can see the structure by expanding the endpoint noted above on the documentation link you provided, then clicking ProcessorEntity > ProcessorDTO > ProcessorConfigDTO to see the pop-up dialogs with the element listing and examples. You can also quickly get the current values of the processor by issuing a GET request to /processors/{id}.

How to know which type of data coming from back-end in ajax?

Working in front-end we never know the back-end language so how can I know whether the
data coming from back-end is in json or in text or in html or in xml. We don't have an authority or access to back-end language.
Some languages declare this in the first line or 2... Why don't you just read the first few lines or the code?
Many languages will allow you to parse XML, not ideal to wrap it in a catch but it would work. However, you neglected to state what language you are using.
However, it may be worth while agreeing on a format, something like XML which you can then de-serialize ?
You can check Content-Type in Response Headers to know the response data type.

Resources