Tp 55555/integrate document service client (#5)
* TP-55555 | document client and kafka integration * TP-55555 | introduce service concept refactor code
This commit is contained in:
28
pkg/encoder/encoder.go
Normal file
28
pkg/encoder/encoder.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package encoder
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
type Encoder interface {
|
||||
Encode(v interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
type JsonEncoder struct{}
|
||||
type ProtoEncoder struct{}
|
||||
|
||||
var JsonEncoderInstance = &JsonEncoder{}
|
||||
var ProtoEncoderInstance = &ProtoEncoder{}
|
||||
|
||||
func (j *JsonEncoder) Encode(v interface{}) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
func (p *ProtoEncoder) Encode(v interface{}) ([]byte, error) {
|
||||
if message, ok := v.(proto.Message); ok {
|
||||
return proto.Marshal(message)
|
||||
}
|
||||
return nil, fmt.Errorf("not a proto message")
|
||||
}
|
||||
Reference in New Issue
Block a user