@@ -19,14 +19,15 @@ package helper
19
19
import (
20
20
"bytes"
21
21
"compress/gzip"
22
+ "encoding/base64"
22
23
"encoding/json"
23
24
"io"
24
25
)
25
26
26
- // CompressJSON marshals and compresses JSON data
27
- func CompressJSON ( data interface {}) (string , error ) {
27
+ // CompressWorkflowRequest compresses WorkflowRequest to bytes
28
+ func CompressWorkflowRequest ( workflowRequest interface {}) (string , error ) {
28
29
// Marshal to JSON
29
- jsonData , err := json .Marshal (data )
30
+ jsonData , err := json .Marshal (workflowRequest )
30
31
if err != nil {
31
32
return "" , err
32
33
}
@@ -45,86 +46,32 @@ func CompressJSON(data interface{}) (string, error) {
45
46
return "" , err
46
47
}
47
48
48
- return string (buf .Bytes ()), nil
49
+ // Encode compressed binary data to Base64 to avoid UTF-8 encoding issues
50
+ return base64 .StdEncoding .EncodeToString (buf .Bytes ()), nil
49
51
}
50
52
51
- // DecompressJSON decompresses and unmarshals JSON data
52
- func DecompressJSON (compressedData []byte , target interface {}) error {
53
- // Decompress
54
- reader , err := gzip .NewReader (bytes .NewReader (compressedData ))
53
+ // DecompressWorkflowRequest decompresses bytes to WorkflowRequest
54
+ // This function handles both Base64 encoded and legacy raw binary data for backward compatibility
55
+ func DecompressWorkflowRequest (compressedData string , target interface {}) error {
56
+ // Try Base64 decoding first (new format)
57
+ decodedData , err := base64 .StdEncoding .DecodeString (compressedData )
55
58
if err != nil {
56
59
return err
57
60
}
58
- defer reader .Close ()
59
61
60
- // Read decompressed data
61
- decompressedData , err := io . ReadAll ( reader )
62
+ // Use decoded data for decompression
63
+ reader , err := gzip . NewReader ( bytes . NewReader ( decodedData ) )
62
64
if err != nil {
63
65
return err
64
66
}
65
-
66
- // Unmarshal JSON
67
- return json .Unmarshal (decompressedData , target )
68
- }
69
-
70
- // CompressString compresses string data
71
- func CompressString (data string ) ([]byte , error ) {
72
- var buf bytes.Buffer
73
- gzipWriter := gzip .NewWriter (& buf )
74
-
75
- _ , err := gzipWriter .Write ([]byte (data ))
76
- if err != nil {
77
- return nil , err
78
- }
79
-
80
- err = gzipWriter .Close ()
81
- if err != nil {
82
- return nil , err
83
- }
84
-
85
- return buf .Bytes (), nil
86
- }
87
-
88
- // DecompressString decompresses string data
89
- func DecompressString (compressedData []byte ) (string , error ) {
90
- reader , err := gzip .NewReader (bytes .NewReader (compressedData ))
91
- if err != nil {
92
- return "" , err
93
- }
94
67
defer reader .Close ()
95
68
69
+ // Read decompressed data
96
70
decompressedData , err := io .ReadAll (reader )
97
71
if err != nil {
98
- return "" , err
72
+ return err
99
73
}
100
74
101
- return string (decompressedData ), nil
102
- }
103
-
104
- // Utility functions for backward compatibility
105
-
106
- // CompressWorkflowRequest compresses WorkflowRequest to bytes
107
- func CompressWorkflowRequest (workflowRequest interface {}) (string , error ) {
108
- return CompressJSON (workflowRequest )
109
- }
110
-
111
- // DecompressWorkflowRequest decompresses bytes to WorkflowRequest
112
- func DecompressWorkflowRequest (compressedData string , target interface {}) error {
113
- return DecompressJSON ([]byte (compressedData ), target )
114
- }
115
-
116
- // Example usage:
117
- /*
118
- // Compress before saving
119
- compressedData, err := CompressWorkflowRequest(workflowRequest)
120
- if err != nil {
121
- return err
122
- }
123
- snapshot.WorkflowRequestJson = compressedData
124
- // Decompress when reading
125
- var workflowRequest types.WorkflowRequest
126
- err = DecompressWorkflowRequest(snapshot.WorkflowRequestJson, &workflowRequest)
127
- if err != nil {
128
- return err
75
+ // Unmarshal JSON
76
+ return json .Unmarshal (decompressedData , target )
129
77
}
130
- */
0 commit comments