stringModel.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package client
  2. import (
  3. "context"
  4. "github.com/longjoy/micro-go-course/section35/zipkin-kit/pb"
  5. "github.com/longjoy/micro-go-course/section35/zipkin-kit/string-service/endpoint"
  6. "github.com/pkg/errors"
  7. )
  8. func EncodeGRPCStringRequest(_ context.Context, r interface{}) (interface{}, error) {
  9. req := r.(endpoint.StringRequest)
  10. return &pb.StringRequest{
  11. RequestType: string(req.RequestType),
  12. A: string(req.A),
  13. B: string(req.B),
  14. }, nil
  15. }
  16. func DecodeGRPCStringRequest(ctx context.Context, r interface{}) (interface{}, error) {
  17. req := r.(*pb.StringRequest)
  18. return endpoint.StringRequest{
  19. RequestType: string(req.RequestType),
  20. A: string(req.A),
  21. B: string(req.B),
  22. }, nil
  23. }
  24. func EncodeGRPCStringResponse(_ context.Context, r interface{}) (interface{}, error) {
  25. resp := r.(endpoint.StringResponse)
  26. if resp.Error != nil {
  27. return &pb.StringResponse{
  28. Result: string(resp.Result),
  29. Err: resp.Error.Error(),
  30. }, nil
  31. }
  32. return &pb.StringResponse{
  33. Result: string(resp.Result),
  34. Err: "",
  35. }, nil
  36. }
  37. func DecodeGRPCStringResponse(_ context.Context, r interface{}) (interface{}, error) {
  38. resp := r.(*pb.StringResponse)
  39. return endpoint.StringResponse{
  40. Result: string(resp.Result),
  41. Error: errors.New(resp.Err),
  42. }, nil
  43. }