Files
cybertron/pkg/encoder/encoder.go
Varnit Goyal f20af81520 Tp 55555/integrate document service client (#5)
* TP-55555 | document client and kafka integration

* TP-55555 | introduce service concept refactor code
2024-07-27 17:00:47 +05:30

29 lines
574 B
Go

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")
}