myformat.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "encoding/json"
  17. "fmt"
  18. "github.com/mitchellh/mapstructure"
  19. )
  20. type Hobbies struct {
  21. Indoor []string `json:"indoor"`
  22. Outdoor []string `json:"outdoor"`
  23. }
  24. type Sample struct {
  25. Id int64 `json:"id"`
  26. Name string `json:"name"`
  27. Age int64 `json:"age"`
  28. Hobbies Hobbies `json:"hobbies"`
  29. }
  30. func (x *Sample) GetSchemaJson() string {
  31. // return a static schema
  32. return `{
  33. "id": {
  34. "type": "bigint"
  35. },
  36. "name": {
  37. "type": "string"
  38. },
  39. "age": {
  40. "type": "bigint"
  41. },
  42. "hobbies": {
  43. "type": "struct",
  44. "properties": {
  45. "indoor": {
  46. "type": "array",
  47. "items": {
  48. "type": "string"
  49. }
  50. },
  51. "outdoor": {
  52. "type": "array",
  53. "items": {
  54. "type": "string"
  55. }
  56. }
  57. }
  58. }
  59. }`
  60. }
  61. func (x *Sample) Encode(d interface{}) ([]byte, error) {
  62. switch r := d.(type) {
  63. case map[string]interface{}:
  64. result := &Sample{}
  65. err := MapToStructStrict(r, result)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return json.Marshal(result)
  70. default:
  71. return nil, fmt.Errorf("unsupported type %v, must be a map", d)
  72. }
  73. }
  74. func (x *Sample) Decode(b []byte) (interface{}, error) {
  75. result := &Sample{}
  76. // check error
  77. err := json.Unmarshal(b, &result)
  78. if err != nil {
  79. return nil, err
  80. }
  81. // convert struct to map
  82. hobbyMap := make(map[string]interface{}, 2)
  83. hobbyMap["indoor"] = result.Hobbies.Indoor
  84. hobbyMap["outdoor"] = result.Hobbies.Outdoor
  85. resultMap := make(map[string]interface{}, 4)
  86. resultMap["id"] = result.Id
  87. resultMap["name"] = result.Name
  88. resultMap["age"] = result.Age
  89. resultMap["hobbies"] = hobbyMap
  90. return resultMap, err
  91. }
  92. func MapToStructStrict(input, output interface{}) error {
  93. config := &mapstructure.DecoderConfig{
  94. ErrorUnused: true,
  95. TagName: "json",
  96. Result: output,
  97. }
  98. decoder, err := mapstructure.NewDecoder(config)
  99. if err != nil {
  100. return err
  101. }
  102. return decoder.Decode(input)
  103. }
  104. func GetSample() interface{} {
  105. return &Sample{}
  106. }