Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace json format with protobuf Struct #1192

Merged
merged 21 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile.tools
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ ARG protobuf=3.8.0
RUN wget -q https://github.com/protocolbuffers/protobuf/releases/download/v$protobuf/protoc-$protobuf-linux-x86_64.zip -O /tmp/protobuf.zip && \
mkdir /tmp/protobuf && \
unzip /tmp/protobuf.zip -d /tmp/protobuf && \
mv /tmp/protobuf/bin/protoc /usr/local/bin/protoc && \
mv /tmp/protobuf/bin/* /usr/local/bin/ && \
mv /tmp/protobuf/include/* /usr/local/include/ && \
rm -rf /tmp/*

RUN go install github.com/go-bindata/go-bindata/go-bindata
Expand Down
50 changes: 26 additions & 24 deletions protobuf/api/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion protobuf/api/event.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syntax = "proto3";

import "google/protobuf/struct.proto";
import "protobuf/types/event.proto";

package api;
Expand Down Expand Up @@ -45,7 +46,7 @@ message CreateEventRequest {
string key = 2;

// data is the data for the event.
string data = 3;
google.protobuf.Struct data = 3;
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}

// CreateEventResponse defines response for execution update.
Expand Down
79 changes: 41 additions & 38 deletions protobuf/api/execution.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions protobuf/api/execution.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
syntax = "proto3";

import "google/protobuf/struct.proto";
import "protobuf/types/execution.proto";

package api;
Expand Down Expand Up @@ -29,7 +30,7 @@ service Execution {
message CreateExecutionRequest {
string instanceHash = 1;
string taskKey = 2;
string inputs = 3;
google.protobuf.Struct inputs = 3;
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
repeated string tags = 4;
}

Expand Down Expand Up @@ -74,7 +75,7 @@ message UpdateExecutionRequest {
// result pass to execution
oneof result {
// outputs is a success result.
string outputs = 2;
google.protobuf.Struct outputs = 2;
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved

// error is an error result.
string error = 3;
Expand Down
44 changes: 44 additions & 0 deletions protobuf/convert/pbstruct.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package convert

import (
"bytes"
"encoding/json"
"sync"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)

var marshaler = jsonpb.Marshaler{EmitDefaults: true}

var bufPool = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}

// Marshal converts protobuf struct to map[string]interface{}.
func Marshal(in proto.Message, out interface{}) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

dec := json.NewDecoder(buf)
if err := marshaler.Marshal(buf, in); err != nil {
return err
}
return dec.Decode(out)
}

// Unmarshal converts map[string]interface{} to protobuf struct.
func Unmarshal(in interface{}, out proto.Message) error {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufPool.Put(buf)

enc := json.NewEncoder(buf)
if err := enc.Encode(in); err != nil {
return err
}
return jsonpb.Unmarshal(buf, out)
}
39 changes: 21 additions & 18 deletions protobuf/types/event.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion protobuf/types/event.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

import "google/protobuf/struct.proto";

package types;
option go_package = "github.com/mesg-foundation/engine/protobuf/types";

Expand All @@ -15,5 +17,5 @@ message Event {
string key = 3;

// data is the data for the event.
string data = 4;
google.protobuf.Struct data = 4;
NicolasMahe marked this conversation as resolved.
Show resolved Hide resolved
}
Loading