@@ -3,6 +3,7 @@ package utils
3
3
import (
4
4
"github.com/everFinance/goar/types"
5
5
"github.com/hamba/avro"
6
+ "github.com/linkedin/goavro/v2"
6
7
)
7
8
8
9
func TagsEncode (tags []types.Tag ) []types.Tag {
@@ -42,7 +43,7 @@ func TagsDecode(base64Tags []types.Tag) ([]types.Tag, error) {
42
43
}
43
44
44
45
// using bundle tx, avro serialize
45
- func SerializeTags (tags []types.Tag ) ([]byte , error ) {
46
+ func SerializeTags1 (tags []types.Tag ) ([]byte , error ) {
46
47
if len (tags ) == 0 {
47
48
return make ([]byte , 0 ), nil
48
49
}
@@ -55,7 +56,7 @@ func SerializeTags(tags []types.Tag) ([]byte, error) {
55
56
return avro .Marshal (tagsParser , tags )
56
57
}
57
58
58
- func DeserializeTags (data []byte ) ([]types.Tag , error ) {
59
+ func DeserializeTags1 (data []byte ) ([]types.Tag , error ) {
59
60
tagsParser , err := avro .Parse (`{"type": "array", "items": {"type": "record", "name": "Tag", "fields": [{"name": "name", "type": "string"}, {"name": "value", "type": "string"}]}}` )
60
61
if err != nil {
61
62
return nil , err
@@ -64,3 +65,58 @@ func DeserializeTags(data []byte) ([]types.Tag, error) {
64
65
err = avro .Unmarshal (tagsParser , data , & tags )
65
66
return tags , err
66
67
}
68
+
69
+ const avroTagSchema = `{
70
+ "type": "array",
71
+ "items": {
72
+ "type": "record",
73
+ "name": "Tag",
74
+ "fields": [
75
+ { "name": "name", "type": "bytes" },
76
+ { "name": "value", "type": "bytes" }
77
+ ]
78
+ }
79
+ }`
80
+
81
+ func SerializeTags (tags []types.Tag ) ([]byte , error ) {
82
+ if len (tags ) == 0 {
83
+ return make ([]byte , 0 ), nil
84
+ }
85
+
86
+ codec , err := goavro .NewCodec (avroTagSchema )
87
+ if err != nil {
88
+ return nil , err
89
+ }
90
+ avroTags := []map [string ]interface {}{}
91
+ for _ , tag := range tags {
92
+ m := map [string ]interface {}{"name" : []byte (tag .Name ), "value" : []byte (tag .Value )}
93
+ avroTags = append (avroTags , m )
94
+ }
95
+
96
+ data , err := codec .BinaryFromNative (nil , avroTags )
97
+ if err != nil {
98
+ return nil , err
99
+ }
100
+
101
+ return data , err
102
+ }
103
+
104
+ func DeserializeTags (data []byte ) ([]types.Tag , error ) {
105
+ codec , err := goavro .NewCodec (avroTagSchema )
106
+ if err != nil {
107
+ return nil , err
108
+ }
109
+
110
+ avroTags , _ , err := codec .NativeFromBinary (data )
111
+ if err != nil {
112
+ return nil , err
113
+ }
114
+
115
+ tags := []types.Tag {}
116
+
117
+ for _ , v := range avroTags .([]interface {}) {
118
+ tag := v .(map [string ]interface {})
119
+ tags = append (tags , types.Tag {Name : string (tag ["name" ].([]byte )), Value : string (tag ["value" ].([]byte ))})
120
+ }
121
+ return tags , err
122
+ }
0 commit comments