From e8fb6d457615dec846e19206b1521408b73a5bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9F=A9=E5=B1=B1=E6=9D=B0?= Date: Sun, 17 May 2020 16:56:40 +0800 Subject: [PATCH] fix: 1. issue: https://github.com/hantmac/fuckdb/issues/13; 2. add xml annotation --- cmd/fuckdb/bases/base.go | 6 ++++-- cmd/fuckdb/bases/base_mysql.go | 7 +++++-- cmd/fuckdb/cmd/generate.go | 4 ++++ cmd/fuckdb/fuckdbGo/fuckdb_go.go | 4 +++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cmd/fuckdb/bases/base.go b/cmd/fuckdb/bases/base.go index 8100a2c..cbc81f5 100644 --- a/cmd/fuckdb/bases/base.go +++ b/cmd/fuckdb/bases/base.go @@ -81,8 +81,10 @@ 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, xmlAnnotation bool, xormAnnotation bool, fakerAnnotation bool, gureguTypes bool) ([]byte, error) { - dbTypes := generateMysqlTypes(columnTypes, 0, jsonAnnotation, gormAnnotation, xmlAnnotation, xormAnnotation, fakerAnnotation, gureguTypes) + jsonAnnotation bool, gormAnnotation bool, xmlAnnotation bool, xormAnnotation bool, fakerAnnotation bool, + gureguTypes bool, structSorted bool) ([]byte, error) { + dbTypes := generateMysqlTypes(columnTypes, 0, jsonAnnotation, gormAnnotation, xmlAnnotation, + xormAnnotation, fakerAnnotation, gureguTypes, structSorted) src := fmt.Sprintf("package %s\ntype %s %s}", pkgName, diff --git a/cmd/fuckdb/bases/base_mysql.go b/cmd/fuckdb/bases/base_mysql.go index 12c56ae..a1bcca4 100644 --- a/cmd/fuckdb/bases/base_mysql.go +++ b/cmd/fuckdb/bases/base_mysql.go @@ -63,14 +63,17 @@ 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, xmlAnnotation bool, xormAnnotation bool, fakerAnnotation bool, gureguTypes bool) string { +func generateMysqlTypes(obj map[string]map[string]string, depth int, jsonAnnotation bool, gormAnnotation bool, + xmlAnnotation bool, xormAnnotation bool, fakerAnnotation bool, gureguTypes bool, structSorted bool) string { structure := "struct {" keys := make([]string, 0, len(obj)) for key := range obj { keys = append(keys, key) } - sort.Strings(keys) + if structSorted { + sort.Strings(keys) + } for _, key := range keys { mysqlType := obj[key] diff --git a/cmd/fuckdb/cmd/generate.go b/cmd/fuckdb/cmd/generate.go index d64951f..ac331cf 100644 --- a/cmd/fuckdb/cmd/generate.go +++ b/cmd/fuckdb/cmd/generate.go @@ -32,7 +32,9 @@ var mysqlInfo = `{ "database": "db_example", "packageName": "packageName", "structName": "structName", + "structSorted": false, "jsonAnnotation": true, + "xmlAnnotation": false, "gormAnnotation": true } }` @@ -53,7 +55,9 @@ var generateCmd = &cobra.Command{ "database": "db_example", "packageName": "packageName", "structName": "structName", + "structSorted": false, "jsonAnnotation": true, + "xmlAnnotation": false, "gormAnnotation": true } }`, diff --git a/cmd/fuckdb/fuckdbGo/fuckdb_go.go b/cmd/fuckdb/fuckdbGo/fuckdb_go.go index 256ace5..3db67eb 100644 --- a/cmd/fuckdb/fuckdbGo/fuckdb_go.go +++ b/cmd/fuckdb/fuckdbGo/fuckdb_go.go @@ -19,7 +19,9 @@ type DBConf struct { Database string `json:"database"` PackageName string `json:"packageName"` StructName string `json:"structName"` + StructSorted bool `json:"struct_sorted"` JSONAnnotation bool `json:"jsonAnnotation"` + XMLAnnotation bool `json:"xmlAnnotation"` GormAnnotation bool `json:"gormAnnotation"` } `json:"db"` } @@ -70,7 +72,7 @@ func FuckdbGo() string { structInfo, err := bases.Generate(*columnDataTypes, mysqlInfo.Db.Table, mysqlInfo.Db.StructName, mysqlInfo.Db.PackageName, mysqlInfo.Db.JSONAnnotation, mysqlInfo.Db.GormAnnotation, - false, false, false, true) + mysqlInfo.Db.XMLAnnotation, false, false, true, mysqlInfo.Db.StructSorted) return string(structInfo) }