-
Notifications
You must be signed in to change notification settings - Fork 14
/
format.go
65 lines (51 loc) · 1.26 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package transform
import (
"bytes"
"compress/gzip"
"fmt"
"io"
iconfig "github.com/brexhq/substation/internal/config"
"github.com/brexhq/substation/internal/errors"
)
type formatBase64Config struct {
Object iconfig.Object `json:"object"`
}
func (c *formatBase64Config) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func (c *formatBase64Config) Validate() error {
if c.Object.SourceKey == "" && c.Object.TargetKey != "" {
return fmt.Errorf("object_source_key: %v", errors.ErrMissingRequiredOption)
}
if c.Object.SourceKey != "" && c.Object.TargetKey == "" {
return fmt.Errorf("object_target_key: %v", errors.ErrMissingRequiredOption)
}
return nil
}
type formatGzipConfig struct{}
func (c *formatGzipConfig) Decode(in interface{}) error {
return iconfig.Decode(in, c)
}
func fmtToGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write(data); err != nil {
return nil, err
}
if err := gz.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func fmtFromGzip(data []byte) ([]byte, error) {
r := bytes.NewReader(data)
gz, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
output, err := io.ReadAll(gz)
if err != nil {
return nil, err
}
return output, nil
}