-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
281 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
id,full_name,street,city,state,postcode,country | ||
1,John Lmith,123 Something Street,Anytown,Queensland,4209,Australia | ||
2,Jane Doe,456 Something Street,Anytown,Queensland,4209,Australia | ||
3,John Doe,789 Something Street,Anytown,Queensland,4209,Australia |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Input Columns id, first_name, last_name, address | ||
|
||
Column last_name | ||
-> Trim | ||
-> Replace "S" with "K" | ||
|
||
Columns first_name, last_name | ||
-> Replace "K" with "L" | ||
-> Join with " " as full_name | ||
|
||
Column address | ||
-> Split on "," as street, city, state | ||
|
||
Columns street, city, state | ||
-> Trim | ||
|
||
Column state | ||
-> Split on " " as state, postcode, country | ||
|
||
Output Columns id, full_name, street, city, state, postcode, country |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
id,first_name,last_name,address | ||
1,John,Smith ,"123 Something Street, Anytown, Queensland 4209 Australia" | ||
2,Jane,Doe,"456 Something Street, Anytown, Queensland 4209 Australia" | ||
3,John,Doe,"789 Something Street, Anytown, Queensland 4209 Australia" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,62 @@ | ||
package transformers | ||
|
||
import "strings" | ||
import ( | ||
parser "csvshift/gen" | ||
"strings" | ||
) | ||
|
||
type ReplaceTransformer struct { | ||
type multipleColumnReplaceTransformer struct { | ||
Columns []string | ||
From string | ||
To string | ||
} | ||
|
||
func (t *ReplaceTransformer) Apply(row map[string]interface{}) { | ||
type MultipleColumnReplaceTransformerFactory struct{} | ||
|
||
type singleColumnReplaceTransformer struct { | ||
Column string | ||
From string | ||
To string | ||
} | ||
|
||
type SingleColumnReplaceTransformerFactory struct{} | ||
|
||
func (t *multipleColumnReplaceTransformer) Apply(row map[string]interface{}) { | ||
for _, column := range t.Columns { | ||
val, ok := row[column].(string) | ||
if ok { | ||
row[column] = strings.ReplaceAll(val, t.From, t.To) | ||
} | ||
} | ||
} | ||
|
||
func (t *MultipleColumnReplaceTransformerFactory) IsMatch(modifier parser.IMultipleColumnTransformationContext) bool { | ||
return modifier.REPLACE() != nil | ||
} | ||
|
||
func (t *MultipleColumnReplaceTransformerFactory) Create(columns []string, modifier parser.IMultipleColumnTransformationContext) Transformer { | ||
return &multipleColumnReplaceTransformer{ | ||
Columns: columns, | ||
From: ExtractStringContent(modifier.STRING(0).GetText()), | ||
To: ExtractStringContent(modifier.STRING(1).GetText()), | ||
} | ||
} | ||
|
||
func (t *singleColumnReplaceTransformer) Apply(row map[string]interface{}) { | ||
val, ok := row[t.Column].(string) | ||
if ok { | ||
row[t.Column] = strings.ReplaceAll(val, t.From, t.To) | ||
} | ||
} | ||
|
||
func (t *SingleColumnReplaceTransformerFactory) IsMatch(modifier parser.ISingleColumnTransformationContext) bool { | ||
return modifier.REPLACE() != nil | ||
} | ||
|
||
func (t *SingleColumnReplaceTransformerFactory) Create(column string, modifier parser.ISingleColumnTransformationContext) Transformer { | ||
return &singleColumnReplaceTransformer{ | ||
Column: column, | ||
From: ExtractStringContent(modifier.STRING(0).GetText()), | ||
To: ExtractStringContent(modifier.STRING(1).GetText()), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.