This commit is contained in:
kesonan 2024-06-04 18:26:34 +08:00 committed by GitHub
parent 7954ad3759
commit 97c7835d9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 13 deletions

View File

@ -3,7 +3,6 @@ package config
import ( import (
_ "embed" _ "embed"
"errors" "errors"
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -19,12 +18,8 @@ const (
configFile = "goctl.yaml" configFile = "goctl.yaml"
) )
var ( //go:embed default.yaml
//go:embed default.yaml var defaultConfig []byte
defaultConfig []byte
ExternalConfig *External
)
// Config defines the file naming style // Config defines the file naming style
type ( type (
@ -77,14 +72,13 @@ func NewConfig(format string) (*Config, error) {
return cfg, err return cfg, err
} }
func init() { func GetExternalConfig() (*External, error) {
var cfg External var cfg External
err := loadConfig(&cfg) err := loadConfig(&cfg)
if err != nil { if err != nil {
fmt.Println(err.Error()) return nil, err
} else {
ExternalConfig = &cfg
} }
return &cfg, nil
} }
func loadConfig(cfg *External) error { func loadConfig(cfg *External) error {

View File

@ -258,10 +258,12 @@ func ConvertStringDataType(dataBaseType string, isDefaultNull, unsigned, strict
} }
func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool) (string, string) { func convertDatatypeWithConfig(dataBaseType string, isDefaultNull, unsigned bool) (string, string) {
if config.ExternalConfig == nil { externalConfig, err := config.GetExternalConfig()
if err != nil {
return "", "" 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) { if !ok || (len(opt.Type) == 0 && len(opt.UnsignedType) == 0 && len(opt.NullType) == 0) {
return "", "" return "", ""
} }