From d6552ccbaec89b616560541a2896ee8344c40aff Mon Sep 17 00:00:00 2001 From: Tim Gallant Date: Sun, 18 Jul 2021 21:52:59 -0700 Subject: [PATCH] fixes linting errors --- cmd/db2jsonschema/main.go | 7 +++++- database/sqlite3/sqlite3.go | 43 +++++++++++++++++++----------------- test/setup.go | 7 +++--- test/sqlite3/sqlite3_test.go | 6 +++++ 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/cmd/db2jsonschema/main.go b/cmd/db2jsonschema/main.go index 323159d..457e459 100644 --- a/cmd/db2jsonschema/main.go +++ b/cmd/db2jsonschema/main.go @@ -46,7 +46,12 @@ var ( func HandleGenerate(cmd *cobra.Command, args []string) { if len(driver) == 0 || len(dburl) == 0 { - cmd.Help() + err := cmd.Help() + if err != nil { + log.Error(err) + os.Exit(1) + return + } return } req := &db2jsonschema.Request{ diff --git a/database/sqlite3/sqlite3.go b/database/sqlite3/sqlite3.go index 3d67c4c..792a728 100644 --- a/database/sqlite3/sqlite3.go +++ b/database/sqlite3/sqlite3.go @@ -21,24 +21,24 @@ type SQLiteTable struct { } type SQLiteCreateTable struct { - TableName string `"CREATE" "TABLE" @Ident` - FieldExpressions []*SQLiteFieldExpression `( "(" @@ ( "," @@ )* ( "," )?` - PrimaryKeys []string `( "PRIMARY" "KEY" "(" @Ident ( "," @Ident )* ")" )* ( "," )? )?` - Constraints []*SQLiteConstraint `( "CONSTRAINT" @@ ( "," "CONSTRAINT" @@ )* )? ")"` + TableName string `parser:"'CREATE' 'TABLE' @Ident"` + FieldExpressions []*SQLiteFieldExpression `parser:"( '(' @@ ( ',' @@ )* ( ',' )?"` + PrimaryKeys []string `parser:"( 'PRIMARY' 'KEY' '(' @Ident ( ',' @Ident )* ')' )* ( ',' )? )?"` + Constraints []*SQLiteConstraint `parser:"( 'CONSTRAINT' @@ ( ',' 'CONSTRAINT' @@ )* )? ')'"` } type SQLiteFieldExpression struct { - Name string `@Ident` - Type string `@Ident ( "(" Number ")" )*` - NotNull bool `( @"NOT_NULL"` - AutoIncrement bool `| @"AUTO_INCREMENT" )*` + Name string `parser:"@Ident"` + Type string `parser:"@Ident ( '(' Number ')' )*"` + NotNull bool `parser:"( @'NOT_NULL'"` + AutoIncrement bool `parser:"| @'AUTO_INCREMENT' )*"` } type SQLiteConstraint struct { - Name string `@Ident` - ForeignKey string `"FOREIGN" "KEY" "(" @Ident ")"` - ReferencedTable string `"REFERENCES" @Ident` - ReferencedField string `"(" @Ident ")"` + Name string `parser:"@Ident"` + ForeignKey string `parser:"'FOREIGN' 'KEY' '(' @Ident ')'"` + ReferencedTable string `parser:"'REFERENCES' @Ident"` + ReferencedField string `parser:"'(' @Ident ')'"` } var ( @@ -66,13 +66,13 @@ var ( } sqlLexer = lexer.Must(stateful.NewSimple([]stateful.Rule{ - {`Keyword`, `(?i)\b(CREATE|TABLE|PRIMARY|FOREIGN|KEY|CONSTRAINT|REFERENCE)\b`, nil}, - {`Ident`, `[a-zA-Z_][a-zA-Z0-9_]*`, nil}, - {`Number`, `[-+]?\d*\.?\d+([eE][-+]?\d+)?`, nil}, - {`String`, `'[^']*'|"[^"]*"`, nil}, - {`Operators`, `<>|!=|<=|>=|[-+*/%,.()=<>]`, nil}, - {"whitespace", `\s+`, nil}, - {"backtick", "`", nil}, + {Name: `Keyword`, Pattern: `(?i)\b(CREATE|TABLE|PRIMARY|FOREIGN|KEY|CONSTRAINT|REFERENCE)\b`, Action: nil}, + {Name: `Ident`, Pattern: `[a-zA-Z_][a-zA-Z0-9_]*`, Action: nil}, + {Name: `Number`, Pattern: `[-+]?\d*\.?\d+([eE][-+]?\d+)?`, Action: nil}, + {Name: `String`, Pattern: `'[^']*'|"[^"]*"`, Action: nil}, + {Name: `Operators`, Pattern: `<>|!=|<=|>=|[-+*/%,.()=<>]`, Action: nil}, + {Name: "whitespace", Pattern: `\s+`, Action: nil}, + {Name: "backtick", Pattern: "`", Action: nil}, })) parser = participle.MustBuild( @@ -100,7 +100,10 @@ func SelectTables(conn *sql.DB) ([]*SQLiteTable, error) { for row.Next() { var name string var sql string - row.Scan(&name, &sql) + err = row.Scan(&name, &sql) + if err != nil { + return nil, err + } table := SQLiteTable{name, sql} tables = append(tables, &table) } diff --git a/test/setup.go b/test/setup.go index 894e16a..efc1d49 100644 --- a/test/setup.go +++ b/test/setup.go @@ -35,8 +35,8 @@ type Track struct { Genre Genre } -func MigrateTables(db *gorm.DB) { - db.AutoMigrate( +func MigrateTables(db *gorm.DB) error { + return db.AutoMigrate( &Genre{}, &Artist{}, &Album{}, @@ -71,6 +71,5 @@ func (t *TestDB) Setup() error { if err != nil { return err } - MigrateTables(db) - return nil + return MigrateTables(db) } diff --git a/test/sqlite3/sqlite3_test.go b/test/sqlite3/sqlite3_test.go index a265ffd..8b78276 100644 --- a/test/sqlite3/sqlite3_test.go +++ b/test/sqlite3/sqlite3_test.go @@ -35,25 +35,31 @@ func TestMain(m *testing.M) { fmt.Println(tempDir) if err != nil { fmt.Println(err) + os.Exit(1) return } tempDir = newTestDir tempFile, err := os.CreateTemp(tempDir, "test.*.db") if err != nil { fmt.Println(err) + os.Exit(1) return } testDB.DataSource = tempFile.Name() err = testDB.Setup() if err != nil { fmt.Println(err) + os.Exit(1) return } m.Run() err = os.RemoveAll(tempDir) if err != nil { fmt.Println(err) + os.Exit(1) + return } + os.Exit(0) } func TestYAMLOutput(t *testing.T) {