From 97c7835d9eb2d97e5db2959ced5cee6d0f97585a Mon Sep 17 00:00:00 2001 From: kesonan Date: Tue, 4 Jun 2024 18:26:34 +0800 Subject: [PATCH] fix #4161 (#4176) --- tools/goctl/config/config.go | 16 +++++----------- tools/goctl/model/sql/converter/types.go | 6 ++++-- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/tools/goctl/config/config.go b/tools/goctl/config/config.go index f14caa16..1792f5a5 100644 --- a/tools/goctl/config/config.go +++ b/tools/goctl/config/config.go @@ -3,7 +3,6 @@ package config import ( _ "embed" "errors" - "fmt" "os" "path/filepath" "strings" @@ -19,12 +18,8 @@ const ( configFile = "goctl.yaml" ) -var ( - //go:embed default.yaml - defaultConfig []byte - - ExternalConfig *External -) +//go:embed default.yaml +var defaultConfig []byte // Config defines the file naming style type ( @@ -77,14 +72,13 @@ func NewConfig(format string) (*Config, error) { return cfg, err } -func init() { +func GetExternalConfig() (*External, error) { var cfg External err := loadConfig(&cfg) if err != nil { - fmt.Println(err.Error()) - } else { - ExternalConfig = &cfg + return nil, err } + return &cfg, nil } func loadConfig(cfg *External) error { diff --git a/tools/goctl/model/sql/converter/types.go b/tools/goctl/model/sql/converter/types.go index 2cd803df..6ddfcd58 100644 --- a/tools/goctl/model/sql/converter/types.go +++ b/tools/goctl/model/sql/converter/types.go @@ -258,10 +258,12 @@ func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict } func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool) (string, string) { - if config.ExternalConfig == nil { + externalConfig, err := config.GetExternalConfig() + if err != nil { return "", "" } - opt, ok := config.ExternalConfig.Model.TypesMap[strings.ToLower(dataBaseType)] + + opt, ok := externalConfig.Model.TypesMap[strings.ToLower(dataBaseType)] if !ok || (len(opt.Type) == 0 && len(opt.UnsignedType) == 0 && len(opt.NullType) == 0) { return "", "" }