| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"flag" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/wildducktheories/go-csv" |
| 10 |
"github.com/wildducktheories/go-csv/utils" |
| 11 |
) |
| 12 |
|
| 13 |
func configure(args []string) (*csv.Join, []string, error) { |
| 14 |
flags := flag.NewFlagSet("csv-join", flag.ExitOnError) |
| 15 |
var joinKey string |
| 16 |
var numericKey string |
| 17 |
var joinType string |
| 18 |
|
| 19 |
flags.StringVar(&joinKey, "join-key", "", "The columns of the join key") |
| 20 |
flags.StringVar(&numericKey, "numeric", "", "The specified columns are treated as numeric strings.") |
| 21 |
flags.StringVar(&joinType, "join-type", "outer", "The type of join to perform. One of: outer, left-outer, right-outer, inner") |
| 22 |
if err := flags.Parse(args); err != nil { |
| 23 |
return nil, nil, err |
| 24 |
} |
| 25 |
|
| 26 |
usage := func() { |
| 27 |
fmt.Printf("usage: csv-join {options}\n") |
| 28 |
flags.PrintDefaults() |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
joinKeys, err := csv.Parse(joinKey) |
| 33 |
if err != nil || len(joinKeys) < 1 { |
| 34 |
usage() |
| 35 |
return nil, nil, fmt.Errorf("--join-key must specify one or more columns.") |
| 36 |
} |
| 37 |
leftKeys := make([]string, len(joinKeys)) |
| 38 |
rightKeys := make([]string, len(joinKeys)) |
| 39 |
for i, k := range joinKeys { |
| 40 |
split := strings.Split(k, "=") |
| 41 |
if len(split) == 1 { |
| 42 |
split = append(split, split[0]) |
| 43 |
} |
| 44 |
if len(split) != 2 { |
| 45 |
return nil, nil, fmt.Errorf("each join key must be of the form left=right") |
| 46 |
} |
| 47 |
leftKeys[i] = split[0] |
| 48 |
rightKeys[i] = split[1] |
| 49 |
} |
| 50 |
|
| 51 |
numeric, err := csv.Parse(numericKey) |
| 52 |
if err != nil && len(numericKey) > 0 { |
| 53 |
usage() |
| 54 |
return nil, nil, fmt.Errorf("--numeric must specify the list of numeric keys.") |
| 55 |
} |
| 56 |
|
| 57 |
if i, _, _ := utils.Intersect(joinKeys, numeric); len(i) < len(numeric) { |
| 58 |
return nil, nil, fmt.Errorf("--numeric must be a strict subset of left hand side --join-key") |
| 59 |
} |
| 60 |
|
| 61 |
fn := flags.Args() |
| 62 |
if len(fn) < 2 { |
| 63 |
return nil, nil, fmt.Errorf("expected at least 2 file arguments, found %d", len(fn)) |
| 64 |
} |
| 65 |
|
| 66 |
var leftOuter, rightOuter bool |
| 67 |
switch joinType { |
| 68 |
case "left-outer": |
| 69 |
leftOuter = true |
| 70 |
case "right-outer": |
| 71 |
rightOuter = true |
| 72 |
case "inner": |
| 73 |
default: |
| 74 |
leftOuter = true |
| 75 |
rightOuter = true |
| 76 |
} |
| 77 |
|
| 78 |
return &csv.Join{ |
| 79 |
LeftKeys: leftKeys, |
| 80 |
RightKeys: rightKeys, |
| 81 |
Numeric: numeric, |
| 82 |
LeftOuter: leftOuter, |
| 83 |
RightOuter: rightOuter, |
| 84 |
}, fn, nil |
| 85 |
} |
| 86 |
|
| 87 |
func openReader(n string) (csv.Reader, error) { |
| 88 |
if n == "-" { |
| 89 |
return csv.WithIoReader(os.Stdin), nil |
| 90 |
} else { |
| 91 |
if f, err := os.Open(n); err != nil { |
| 92 |
return nil, err |
| 93 |
} else { |
| 94 |
return csv.WithIoReader(f), nil |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func main() { |
| 100 |
var j *csv.Join |
| 101 |
var err error |
| 102 |
var fn []string |
| 103 |
|
| 104 |
err = func() error { |
| 105 |
if j, fn, err = configure(os.Args[1:]); err == nil { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
leftSortProcess := (&csv.SortKeys{ |
| 110 |
Numeric: j.Numeric, |
| 111 |
Keys: j.LeftKeys, |
| 112 |
}).AsSortProcess() |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
l2r := map[string]string{} |
| 117 |
for i, k := range j.LeftKeys { |
| 118 |
l2r[k] = j.RightKeys[i] |
| 119 |
} |
| 120 |
|
| 121 |
rightNumeric := make([]string, len(j.Numeric)) |
| 122 |
for i, k := range j.Numeric { |
| 123 |
rightNumeric[i] = l2r[k] |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
rightSortProcess := (&csv.SortKeys{ |
| 129 |
Numeric: rightNumeric, |
| 130 |
Keys: j.RightKeys, |
| 131 |
}).AsSortProcess() |
| 132 |
|
| 133 |
|
| 134 |
readers := make([]csv.Reader, len(fn)) |
| 135 |
for i, n := range fn { |
| 136 |
if readers[i], err = openReader(n); err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
procs := make([]csv.Process, len(readers)-1) |
| 143 |
for i, _ := range procs { |
| 144 |
procs[i] = j.WithRight(csv.WithProcess(readers[i+1], rightSortProcess)) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
pipeline := csv.NewPipeLine(procs) |
| 149 |
|
| 150 |
|
| 151 |
var errCh = make(chan error, 1) |
| 152 |
pipeline.Run(csv.WithProcess(readers[0], leftSortProcess), csv.WithIoWriter(os.Stdout), errCh) |
| 153 |
return <-errCh |
| 154 |
} else { |
| 155 |
return err |
| 156 |
} |
| 157 |
}() |
| 158 |
|
| 159 |
if err != nil { |
| 160 |
fmt.Printf("fatal: %v\n", err) |
| 161 |
os.Exit(1) |
| 162 |
} |
| 163 |
} |