transformer.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Code generated by goctl. DO NOT EDIT!
  2. // Source: transform.proto
  3. //go:generate mockgen -destination ./transformer_mock.go -package transformer -source $GOFILE
  4. package transformer
  5. import (
  6. "context"
  7. transform "shorturl/rpc/transform/pb"
  8. "github.com/tal-tech/go-zero/core/jsonx"
  9. "github.com/tal-tech/go-zero/zrpc"
  10. )
  11. type (
  12. Transformer interface {
  13. Expand(ctx context.Context, in *ExpandReq) (*ExpandResp, error)
  14. Shorten(ctx context.Context, in *ShortenReq) (*ShortenResp, error)
  15. }
  16. defaultTransformer struct {
  17. cli zrpc.Client
  18. }
  19. )
  20. func NewTransformer(cli zrpc.Client) Transformer {
  21. return &defaultTransformer{
  22. cli: cli,
  23. }
  24. }
  25. func (m *defaultTransformer) Expand(ctx context.Context, in *ExpandReq) (*ExpandResp, error) {
  26. var request transform.ExpandReq
  27. bts, err := jsonx.Marshal(in)
  28. if err != nil {
  29. return nil, errJsonConvert
  30. }
  31. err = jsonx.Unmarshal(bts, &request)
  32. if err != nil {
  33. return nil, errJsonConvert
  34. }
  35. client := transform.NewTransformerClient(m.cli.Conn())
  36. resp, err := client.Expand(ctx, &request)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var ret ExpandResp
  41. bts, err = jsonx.Marshal(resp)
  42. if err != nil {
  43. return nil, errJsonConvert
  44. }
  45. err = jsonx.Unmarshal(bts, &ret)
  46. if err != nil {
  47. return nil, errJsonConvert
  48. }
  49. return &ret, nil
  50. }
  51. func (m *defaultTransformer) Shorten(ctx context.Context, in *ShortenReq) (*ShortenResp, error) {
  52. var request transform.ShortenReq
  53. bts, err := jsonx.Marshal(in)
  54. if err != nil {
  55. return nil, errJsonConvert
  56. }
  57. err = jsonx.Unmarshal(bts, &request)
  58. if err != nil {
  59. return nil, errJsonConvert
  60. }
  61. client := transform.NewTransformerClient(m.cli.Conn())
  62. resp, err := client.Shorten(ctx, &request)
  63. if err != nil {
  64. return nil, err
  65. }
  66. var ret ShortenResp
  67. bts, err = jsonx.Marshal(resp)
  68. if err != nil {
  69. return nil, errJsonConvert
  70. }
  71. err = jsonx.Unmarshal(bts, &ret)
  72. if err != nil {
  73. return nil, errJsonConvert
  74. }
  75. return &ret, nil
  76. }