mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-21 19:39:04 -05:00
Add --lowercase option to map column names to lowercase struct names (#3675)
* Add --lowercase option to map column names to lowercase struct names By default, each column name maps to a struct field preserving the original case. If --lowercase is specified the resulting struct fields will always be lowercase.
This commit is contained in:
@@ -36,6 +36,7 @@ func main() {
|
||||
// https://blog.golang.org/strings
|
||||
delimiter := flag.String("delimiter", ",", "field delimiter for csv file, must be exactly one character long.")
|
||||
header := flag.String("header", "", "header row. If empty, we'll use the first row of the file")
|
||||
lowercase := flag.Bool("lowercase", false, "convert column names to lowercase (otherwise preserve the case in the resulting struct fields)")
|
||||
name := flag.String("name", "Row", "struct name. The user-visible name to give to the struct type that will hold each row of data.")
|
||||
columnTypes := flag.String("column-types", "", "a comma-separated list of types representing the desired type of each column. if absent all types default to be String")
|
||||
pathDescription := "noms path to blob to import"
|
||||
@@ -146,6 +147,11 @@ func main() {
|
||||
} else {
|
||||
headers = strings.Split(*header, ",")
|
||||
}
|
||||
if *lowercase {
|
||||
for i, _ := range headers {
|
||||
headers[i] = strings.ToLower(headers[i])
|
||||
}
|
||||
}
|
||||
|
||||
uniqueHeaders := make(map[string]bool)
|
||||
for _, header := range headers {
|
||||
|
||||
@@ -49,7 +49,11 @@ func (s *testSuite) TearDownTest() {
|
||||
}
|
||||
|
||||
func writeCSV(w io.Writer) {
|
||||
_, err := io.WriteString(w, "year,a,b,c\n")
|
||||
writeCSVWithHeader(w, "year,a,b,c\n")
|
||||
}
|
||||
|
||||
func writeCSVWithHeader(w io.Writer, header string) {
|
||||
_, err := io.WriteString(w, header)
|
||||
d.Chk.NoError(err)
|
||||
for i := 0; i < TEST_DATA_SIZE; i++ {
|
||||
_, err = io.WriteString(w, fmt.Sprintf("%d,a%d,%d,%d\n", TEST_YEAR+i%3, i, i, i*2))
|
||||
@@ -119,6 +123,40 @@ func (s *testSuite) TestCSVImporter() {
|
||||
validateList(s, ds.HeadValue().(types.List))
|
||||
}
|
||||
|
||||
func (s *testSuite) TestCSVImporterLowercase() {
|
||||
input, err := ioutil.TempFile(s.TempDir, "")
|
||||
d.Chk.NoError(err)
|
||||
defer input.Close()
|
||||
writeCSVWithHeader(input, "YeAr,a,B,c\n")
|
||||
defer os.Remove(input.Name())
|
||||
|
||||
setName := "csv"
|
||||
dataspec := spec.CreateValueSpecString("nbs", s.DBDir, setName)
|
||||
stdout, stderr := s.MustRun(main, []string{"--no-progress", "--lowercase", "--column-types", TEST_FIELDS, input.Name(), dataspec})
|
||||
s.Equal("", stdout)
|
||||
s.Equal("", stderr)
|
||||
|
||||
db := datas.NewDatabase(nbs.NewLocalStore(s.DBDir, clienttest.DefaultMemTableSize))
|
||||
defer os.RemoveAll(s.DBDir)
|
||||
defer db.Close()
|
||||
ds := db.GetDataset(setName)
|
||||
|
||||
validateList(s, ds.HeadValue().(types.List))
|
||||
}
|
||||
|
||||
func (s *testSuite) TestCSVImporterLowercaseDuplicate() {
|
||||
input, err := ioutil.TempFile(s.TempDir, "")
|
||||
d.Chk.NoError(err)
|
||||
defer input.Close()
|
||||
writeCSVWithHeader(input, "YeAr,a,B,year\n")
|
||||
defer os.Remove(input.Name())
|
||||
|
||||
setName := "csv"
|
||||
dataspec := spec.CreateValueSpecString("nbs", s.DBDir, setName)
|
||||
_, stderr, _ := s.Run(main, []string{"--no-progress", "--lowercase", "--column-types", TEST_FIELDS, input.Name(), dataspec})
|
||||
s.Contains(stderr, "must be unique")
|
||||
}
|
||||
|
||||
func (s *testSuite) TestCSVImporterFromBlob() {
|
||||
test := func(pathFlag string) {
|
||||
defer os.RemoveAll(s.DBDir)
|
||||
|
||||
Reference in New Issue
Block a user