Skip to content

Commit

Permalink
-r -b/-a; unit-test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Jan 21, 2024
1 parent b49bf70 commit b2e62c8
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 34 deletions.
55 changes: 21 additions & 34 deletions pkg/transformers/reorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,45 +370,32 @@ func (tr *TransformerReorder) reorderBeforeOrAfterWithRegex(
return
}

outrec := mlrval.NewMlrmapAsRecord()
pe := inrec.Head

// We use outrec.PutReference not output.PutCopy since inrec will be GC'ed

for ; pe != nil; pe = pe.Next {
if pe.Key == tr.centerFieldName {
break
}
if !tr.fieldNamesSet[pe.Key] {
outrec.PutReference(pe.Key, pe.Value)
}
}

if !tr.putAfter {
for _, fieldName := range tr.fieldNames {
value := inrec.Get(fieldName)
if value != nil {
outrec.PutReference(fieldName, value)
matchingFieldNamesSet := lib.NewOrderedMap()
for pe := inrec.Head; pe != nil; pe = pe.Next {
for _, regex := range tr.regexes {
if regex.MatchString(pe.Key) {
if pe.Key != tr.centerFieldName {
matchingFieldNamesSet.Put(pe.Key, pe.Value)
break
}
}
}
}

value := inrec.Get(tr.centerFieldName)
if value != nil {
outrec.PutReference(tr.centerFieldName, value)
}

if tr.putAfter {
for _, fieldName := range tr.fieldNames {
value := inrec.Get(fieldName)
if value != nil {
outrec.PutReference(fieldName, value)
// We use outrec.PutReference not output.PutCopy since inrec will be GC'ed
outrec := mlrval.NewMlrmapAsRecord()
for pe := inrec.Head; pe != nil; pe = pe.Next {
if pe.Key == tr.centerFieldName {
if tr.putAfter {
outrec.PutReference(pe.Key, pe.Value)
}
}
}

for ; pe != nil; pe = pe.Next {
if pe.Key != tr.centerFieldName && !tr.fieldNamesSet[pe.Key] {
for pf := matchingFieldNamesSet.Head; pf != nil; pf = pf.Next {
outrec.PutReference(pf.Key, pf.Value.(*mlrval.Mlrval))
}
if !tr.putAfter {
outrec.PutReference(pe.Key, pe.Value)
}
} else if !matchingFieldNamesSet.Has(pe.Key) {
outrec.PutReference(pe.Key, pe.Value)
}
}
Expand Down
1 change: 1 addition & 0 deletions test/cases/verb-reorder/regex-after/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr --n2x reorder -r 3,9,8 -a 6 test/input/reorder-regex.nidx
Empty file.
10 changes: 10 additions & 0 deletions test/cases/verb-reorder/regex-after/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 a
2 b
4 d
5 e
6 f
3 c
8 h
9 i
7 g
10 j
1 change: 1 addition & 0 deletions test/cases/verb-reorder/regex-before/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr --n2x reorder -r 3,9,8 -b 6 test/input/reorder-regex.nidx
Empty file.
10 changes: 10 additions & 0 deletions test/cases/verb-reorder/regex-before/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 a
2 b
4 d
5 e
3 c
8 h
9 i
6 f
7 g
10 j
1 change: 1 addition & 0 deletions test/cases/verb-reorder/regex-end/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr --n2x reorder -r 3,9,8 -e test/input/reorder-regex.nidx
Empty file.
10 changes: 10 additions & 0 deletions test/cases/verb-reorder/regex-end/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 a
2 b
4 d
5 e
6 f
7 g
10 j
3 c
8 h
9 i
1 change: 1 addition & 0 deletions test/cases/verb-reorder/regex-start/cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mlr --n2x reorder -r 3,9,8 test/input/reorder-regex.nidx
Empty file.
10 changes: 10 additions & 0 deletions test/cases/verb-reorder/regex-start/expout
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
3 c
8 h
9 i
1 a
2 b
4 d
5 e
6 f
7 g
10 j
1 change: 1 addition & 0 deletions test/input/reorder-regex.nidx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a b c d e f g h i j

0 comments on commit b2e62c8

Please sign in to comment.