Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support sqlx tags #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion db2struct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var packageName = goopt.String([]string{"--package"}, "", "name to set for packa
var structName = goopt.String([]string{"--struct"}, "", "name to set for struct")

var jsonAnnotation = goopt.Flag([]string{"--json"}, []string{"--no-json"}, "Add json annotations (default)", "Disable json annotations")
var sqlxAnnotation = goopt.Flag([]string{"--sqlx"}, []string{}, "Add sqlx annotations (tags)", "")
var gormAnnotation = goopt.Flag([]string{"--gorm"}, []string{}, "Add gorm annotations (tags)", "")
var gureguTypes = goopt.Flag([]string{"--guregu"}, []string{}, "Add guregu null types", "")

Expand Down Expand Up @@ -96,7 +97,7 @@ func main() {
*packageName = "newpackage"
}
// Generate struct string based on columnDataTypes
struc, err := db2struct.Generate(*columnDataTypes, *mariadbTable, *structName, *packageName, *jsonAnnotation, *gormAnnotation, *gureguTypes)
struc, err := db2struct.Generate(*columnDataTypes, *mariadbTable, *structName, *packageName, *jsonAnnotation, *gormAnnotation, *sqlxAnnotation, *gureguTypes)

if err != nil {
fmt.Println("Error in creating struct from json: " + err.Error())
Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ var Debug = false

// Generate Given a Column map with datatypes and a name structName,
// attempts to generate a struct definition
func Generate(columnTypes map[string]map[string]string, tableName string, structName string, pkgName string, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool) ([]byte, error) {
func Generate(columnTypes map[string]map[string]string, tableName string, structName string, pkgName string, jsonAnnotation bool, gormAnnotation bool, sqlxAnnotation bool, gureguTypes bool) ([]byte, error) {
var dbTypes string
dbTypes = generateMysqlTypes(columnTypes, 0, jsonAnnotation, gormAnnotation, gureguTypes)
dbTypes = generateMysqlTypes(columnTypes, 0, jsonAnnotation, gormAnnotation, sqlxAnnotation, gureguTypes)
src := fmt.Sprintf("package %s\ntype %s %s}",
pkgName,
structName,
Expand Down
6 changes: 5 additions & 1 deletion utils_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetColumnsFromMysqlTable(mariadbUser string, mariadbPassword string, mariad
}

// Generate go struct entries for a map[string]interface{} structure
func generateMysqlTypes(obj map[string]map[string]string, depth int, jsonAnnotation bool, gormAnnotation bool, gureguTypes bool) string {
func generateMysqlTypes(obj map[string]map[string]string, depth int, jsonAnnotation bool, gormAnnotation bool, sqlxAnnotation bool, gureguTypes bool) string {
structure := "struct {"

keys := make([]string, 0, len(obj))
Expand Down Expand Up @@ -91,6 +91,10 @@ func generateMysqlTypes(obj map[string]map[string]string, depth int, jsonAnnotat
if jsonAnnotation == true {
annotations = append(annotations, fmt.Sprintf("json:\"%s\"", key))
}

if sqlxAnnotation == true {
annotations = append(annotations, fmt.Sprintf("db:\"%s\"", key))
}
if len(annotations) > 0 {
structure += fmt.Sprintf("\n%s %s `%s`",
fieldName,
Expand Down