helloworld_wrapper.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "github.com/golang/protobuf/proto"
  18. )
  19. func (x *HelloReply) Encode(d interface{}) ([]byte, error) {
  20. switch r := d.(type) {
  21. case map[string]interface{}:
  22. t, ok := r["message"]
  23. if ok {
  24. if v, ok := t.(string); ok {
  25. x.Message = v
  26. } else {
  27. fmt.Println("message is not string")
  28. }
  29. } else {
  30. // if required, return error
  31. fmt.Println("message is not found")
  32. }
  33. return proto.Marshal(x)
  34. default:
  35. return nil, fmt.Errorf("unsupported type %v, must be a map", d)
  36. }
  37. }
  38. func (x *HelloReply) Decode(b []byte) (interface{}, error) {
  39. err := proto.Unmarshal(b, x)
  40. if err != nil {
  41. return nil, err
  42. }
  43. result := make(map[string]interface{}, 1)
  44. result["message"] = x.Message
  45. return result, nil
  46. }
  47. func GetHelloReply() interface{} {
  48. return &HelloReply{}
  49. }
  50. func (x *HelloRequest) Encode(d interface{}) ([]byte, error) {
  51. switch r := d.(type) {
  52. case map[string]interface{}:
  53. t, ok := r["name"]
  54. if ok {
  55. if v, ok := t.(string); ok {
  56. x.Name = v
  57. } else {
  58. return nil, fmt.Errorf("name is not string")
  59. }
  60. } else {
  61. // if required, return error
  62. fmt.Println("message is not found")
  63. }
  64. return proto.Marshal(x)
  65. default:
  66. return nil, fmt.Errorf("unsupported type %v, must be a map", d)
  67. }
  68. }
  69. func (x *HelloRequest) Decode(b []byte) (interface{}, error) {
  70. err := proto.Unmarshal(b, x)
  71. if err != nil {
  72. return nil, err
  73. }
  74. result := make(map[string]interface{}, 1)
  75. result["name"] = x.Name
  76. return result, nil
  77. }
  78. func GetHelloRequest() interface{} {
  79. return &HelloRequest{}
  80. }