@@ -13,6 +13,7 @@ import (
13
13
"strings"
14
14
15
15
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
16
+ extcommon "github.com/goto/optimus-any2any/ext/common"
16
17
"github.com/goto/optimus-any2any/internal/component/option"
17
18
"github.com/goto/optimus-any2any/internal/component/source"
18
19
"github.com/goto/optimus-any2any/pkg/flow"
@@ -29,13 +30,15 @@ type OSSSource struct {
29
30
pathPrefix string
30
31
fileFormat string
31
32
csvDelimiter rune
33
+ columnMap map [string ]string
32
34
}
33
35
34
36
var _ flow.Source = (* OSSSource )(nil )
35
37
36
38
// NewSource creates a new OSSSource.
37
39
func NewSource (ctx context.Context , l * slog.Logger , svcAcc string ,
38
- sourceBucketPath , fileFormat string , csvDelimiter rune , opts ... option.Option ) (* OSSSource , error ) {
40
+ sourceBucketPath , fileFormat string , csvDelimiter rune ,
41
+ columnMappingFilePath string , opts ... option.Option ) (* OSSSource , error ) {
39
42
// create commonSource source
40
43
commonSource := source .NewCommonSource (l , opts ... )
41
44
@@ -50,6 +53,11 @@ func NewSource(ctx context.Context, l *slog.Logger, svcAcc string,
50
53
if err != nil {
51
54
return nil , errors .WithStack (err )
52
55
}
56
+ // read column map
57
+ columnMap , err := extcommon .GetColumnMap (columnMappingFilePath )
58
+ if err != nil {
59
+ return nil , errors .WithStack (err )
60
+ }
53
61
54
62
ossSource := & OSSSource {
55
63
CommonSource : commonSource ,
@@ -59,6 +67,7 @@ func NewSource(ctx context.Context, l *slog.Logger, svcAcc string,
59
67
pathPrefix : strings .TrimPrefix (parsedURL .Path , "/" ),
60
68
fileFormat : fileFormat ,
61
69
csvDelimiter : csvDelimiter ,
70
+ columnMap : columnMap ,
62
71
}
63
72
64
73
// add clean function
@@ -110,12 +119,19 @@ func (o *OSSSource) process() {
110
119
111
120
// send records
112
121
for _ , record := range records {
113
- o .Send (record )
122
+ mappedRecord := extcommon .KeyMapping (o .columnMap , record )
123
+ raw , err := json .Marshal (mappedRecord )
124
+ if err != nil {
125
+ o .Logger .Error (fmt .Sprintf ("source(oss): failed to marshal record: %s" , err .Error ()))
126
+ o .SetError (errors .WithStack (err ))
127
+ continue
128
+ }
129
+ o .Send (raw )
114
130
}
115
131
}
116
132
}
117
133
118
- func (o * OSSSource ) unpackRecords (object * oss.GetObjectResult ) ([][] byte , error ) {
134
+ func (o * OSSSource ) unpackRecords (object * oss.GetObjectResult ) ([]map [ string ] interface {} , error ) {
119
135
// unmarshal object based on file format
120
136
var (
121
137
records []map [string ]interface {}
@@ -132,17 +148,7 @@ func (o *OSSSource) unpackRecords(object *oss.GetObjectResult) ([][]byte, error)
132
148
if err != nil {
133
149
return nil , errors .WithStack (err )
134
150
}
135
-
136
- // marshal records
137
- raws := make ([][]byte , 0 , len (records ))
138
- for _ , record := range records {
139
- raw , err := json .Marshal (record )
140
- if err != nil {
141
- return nil , errors .WithStack (err )
142
- }
143
- raws = append (raws , raw )
144
- }
145
- return raws , nil
151
+ return records , nil
146
152
}
147
153
148
154
func (o * OSSSource ) unmarshalCSV (object * oss.GetObjectResult ) ([]map [string ]interface {}, error ) {
0 commit comments