-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Sotirios Mantziaris <[email protected]>
- Loading branch information
Showing
31 changed files
with
839 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package protobuf | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
const ( | ||
// Type definition. | ||
Type string = "application/x-protobuf" | ||
// TypeGoogle definition. | ||
TypeGoogle string = "application/x-google-protobuf" | ||
) | ||
|
||
// Decode a protobuf input in the form of a reader. | ||
func Decode(data io.Reader, v interface{}) error { | ||
b, err := ioutil.ReadAll(data) | ||
if err != nil { | ||
return err | ||
} | ||
return DecodeRaw(b, v) | ||
} | ||
|
||
// DecodeRaw a protobuf input in the form of a byte slice. | ||
func DecodeRaw(data []byte, v interface{}) error { | ||
return proto.Unmarshal(data, v.(proto.Message)) | ||
} | ||
|
||
// Encode a model to protobuf. | ||
func Encode(v interface{}) ([]byte, error) { | ||
return proto.Marshal(v.(proto.Message)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package protobuf | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/golang/protobuf/proto" | ||
) | ||
|
||
func TestEncodeDecode(t *testing.T) { | ||
test := Test{ | ||
Label: proto.String("hello"), | ||
Type: proto.Int32(17), | ||
Reps: []int64{1, 2, 3}, | ||
} | ||
test2 := Test{} | ||
test3 := Test{} | ||
|
||
b, err := Encode(&test) | ||
assert.NoError(t, err) | ||
err = DecodeRaw(b, &test2) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.GetLabel(), test2.GetLabel()) | ||
assert.Equal(t, test.GetType(), test2.GetType()) | ||
assert.Equal(t, test.GetReps(), test2.GetReps()) | ||
|
||
r := bytes.NewReader(b) | ||
err = Decode(r, &test3) | ||
assert.NoError(t, err) | ||
assert.Equal(t, test.GetLabel(), test3.GetLabel()) | ||
assert.Equal(t, test.GetType(), test3.GetType()) | ||
assert.Equal(t, test.GetReps(), test3.GetReps()) | ||
} | ||
|
||
func TestDecodeError(t *testing.T) { | ||
test := Test{} | ||
err := Decode(errReader(0), &test) | ||
assert.Error(t, err) | ||
} | ||
|
||
func TestProtobuf(t *testing.T) { | ||
test := Test{ | ||
Label: proto.String("hello"), | ||
Type: proto.Int32(17), | ||
Reps: []int64{1, 2, 3}, | ||
} | ||
|
||
test1 := Test{ | ||
Type: nil, | ||
} | ||
|
||
assert.Equal(t, "", test1.GetLabel()) | ||
assert.Equal(t, int32(0), test1.GetType()) | ||
assert.Equal(t, []int64([]int64(nil)), test1.GetReps()) | ||
|
||
test.XXX_DiscardUnknown() | ||
test.XXX_Merge(&test1) | ||
assert.Equal(t, "label:\"hello\" type:17 reps:1 reps:2 reps:3 ", test.String()) | ||
b, c := test.Descriptor() | ||
assert.NotEmpty(t, b) | ||
assert.Len(t, c, 1) | ||
} | ||
|
||
type errReader int | ||
|
||
func (errReader) Read(p []byte) (n int, err error) { | ||
return 0, errors.New("test error") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
syntax = "proto2"; | ||
package protobuf; | ||
|
||
message Test { | ||
required string label = 1; | ||
optional int32 type = 2; | ||
repeated int64 reps = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
|
||
curl -i -X POST http://localhost:50000 | ||
curl -i -H "Content-Type: application/json" -X POST http://localhost:50000 --data '{"firstname":"John","lastname":"Doe"}' |
Oops, something went wrong.