@@ -13,6 +13,7 @@ import (
1313 "strings"
1414
1515 "github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
16+ extcommon "github.com/goto/optimus-any2any/ext/common"
1617 "github.com/goto/optimus-any2any/internal/component/option"
1718 "github.com/goto/optimus-any2any/internal/component/source"
1819 "github.com/goto/optimus-any2any/pkg/flow"
@@ -29,13 +30,15 @@ type OSSSource struct {
2930 pathPrefix string
3031 fileFormat string
3132 csvDelimiter rune
33+ columnMap map [string ]string
3234}
3335
3436var _ flow.Source = (* OSSSource )(nil )
3537
3638// NewSource creates a new OSSSource.
3739func 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 ) {
3942 // create commonSource source
4043 commonSource := source .NewCommonSource (l , opts ... )
4144
@@ -50,6 +53,11 @@ func NewSource(ctx context.Context, l *slog.Logger, svcAcc string,
5053 if err != nil {
5154 return nil , errors .WithStack (err )
5255 }
56+ // read column map
57+ columnMap , err := extcommon .GetColumnMap (columnMappingFilePath )
58+ if err != nil {
59+ return nil , errors .WithStack (err )
60+ }
5361
5462 ossSource := & OSSSource {
5563 CommonSource : commonSource ,
@@ -59,6 +67,7 @@ func NewSource(ctx context.Context, l *slog.Logger, svcAcc string,
5967 pathPrefix : strings .TrimPrefix (parsedURL .Path , "/" ),
6068 fileFormat : fileFormat ,
6169 csvDelimiter : csvDelimiter ,
70+ columnMap : columnMap ,
6271 }
6372
6473 // add clean function
@@ -110,12 +119,19 @@ func (o *OSSSource) process() {
110119
111120 // send records
112121 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 )
114130 }
115131 }
116132}
117133
118- func (o * OSSSource ) unpackRecords (object * oss.GetObjectResult ) ([][] byte , error ) {
134+ func (o * OSSSource ) unpackRecords (object * oss.GetObjectResult ) ([]map [ string ] interface {} , error ) {
119135 // unmarshal object based on file format
120136 var (
121137 records []map [string ]interface {}
@@ -132,17 +148,7 @@ func (o *OSSSource) unpackRecords(object *oss.GetObjectResult) ([][]byte, error)
132148 if err != nil {
133149 return nil , errors .WithStack (err )
134150 }
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
146152}
147153
148154func (o * OSSSource ) unmarshalCSV (object * oss.GetObjectResult ) ([]map [string ]interface {}, error ) {
0 commit comments