From c70bc7c96ac36873ba14d959b06c10e2bfa893bf Mon Sep 17 00:00:00 2001 From: John Date: Sun, 23 Feb 2020 20:25:55 +0800 Subject: [PATCH] improve pakage g/gins --- frame/g/g_func.go | 12 +- frame/g/g_setting.go | 1 + frame/gins/gins.go | 264 +----------------- frame/gins/gins_config.go | 17 ++ frame/gins/gins_database.go | 122 ++++++++ frame/gins/gins_i18n.go | 17 ++ frame/gins/gins_log.go | 46 +++ frame/gins/gins_redis.go | 53 ++++ frame/gins/gins_resource.go | 17 ++ frame/gins/gins_server.go | 40 +++ frame/gins/gins_view.go | 46 +++ ...asic_test.go => gins_z_unit_basic_test.go} | 0 ...fig_test.go => gins_z_unit_config_test.go} | 0 ...e_test.go => gins_z_unit_database_test.go} | 0 ...edis_test.go => gins_z_unit_redis_test.go} | 0 ..._view_test.go => gins_z_unit_view_test.go} | 0 internal/empty/empty.go | 2 +- internal/intlog/intlog.go | 8 +- 18 files changed, 374 insertions(+), 271 deletions(-) create mode 100644 frame/gins/gins_config.go create mode 100644 frame/gins/gins_database.go create mode 100644 frame/gins/gins_i18n.go create mode 100644 frame/gins/gins_log.go create mode 100644 frame/gins/gins_redis.go create mode 100644 frame/gins/gins_resource.go create mode 100644 frame/gins/gins_server.go create mode 100644 frame/gins/gins_view.go rename frame/gins/{gins_basic_test.go => gins_z_unit_basic_test.go} (100%) rename frame/gins/{gins_config_test.go => gins_z_unit_config_test.go} (100%) rename frame/gins/{gins_database_test.go => gins_z_unit_database_test.go} (100%) rename frame/gins/{gins_redis_test.go => gins_z_unit_redis_test.go} (100%) rename frame/gins/{gins_view_test.go => gins_z_unit_view_test.go} (100%) diff --git a/frame/g/g_func.go b/frame/g/g_func.go index 38334f003..728f0dd25 100644 --- a/frame/g/g_func.go +++ b/frame/g/g_func.go @@ -49,9 +49,15 @@ func TryCatch(try func(), catch ...func(exception interface{})) { gutil.TryCatch(try, catch...) } -// IsEmpty checks given value empty or not. -// It returns false if value is: integer(0), bool(false), slice/map(len=0), nil; -// or else true. +// IsNil checks whether given is nil. +// Note that it might use reflect feature which affects performance a little bit. +func IsNil(value interface{}) bool { + return empty.IsNil(value) +} + +// IsEmpty checks whether given empty. +// It returns true if is in: 0, nil, false, "", len(slice/map/chan) == 0. +// Or else it returns true. func IsEmpty(value interface{}) bool { return empty.IsEmpty(value) } diff --git a/frame/g/g_setting.go b/frame/g/g_setting.go index 64104f8d9..20b5b4573 100644 --- a/frame/g/g_setting.go +++ b/frame/g/g_setting.go @@ -10,6 +10,7 @@ import "github.com/gogf/gf/net/ghttp" // SetServerGraceful enables/disables graceful reload feature of http Web Server. // This feature is disabled in default. +// Deprecated, use configuration of ghttp.Server for controlling this feature. func SetServerGraceful(enabled bool) { ghttp.SetGraceful(enabled) } diff --git a/frame/gins/gins.go b/frame/gins/gins.go index f29044776..d08ba08d7 100644 --- a/frame/gins/gins.go +++ b/frame/gins/gins.go @@ -5,41 +5,15 @@ // You can obtain one at https://github.com/gogf/gf. // Package gins provides instances and core components management. -// -// Note that it should not used glog.Panic* functions for panics if you do not want -// to log all the panics. package gins import ( - "fmt" "github.com/gogf/gf/internal/intlog" - "github.com/gogf/gf/net/ghttp" - "github.com/gogf/gf/text/gstr" - "github.com/gogf/gf/util/gutil" - "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/container/gmap" - "github.com/gogf/gf/database/gdb" - "github.com/gogf/gf/database/gredis" - "github.com/gogf/gf/i18n/gi18n" "github.com/gogf/gf/os/gcfg" "github.com/gogf/gf/os/gfsnotify" - "github.com/gogf/gf/os/glog" - "github.com/gogf/gf/os/gres" - "github.com/gogf/gf/os/gview" - "github.com/gogf/gf/text/gregex" - "github.com/gogf/gf/util/gconv" -) - -const ( - gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis" - gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger" - gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server" - gFRAME_CORE_COMPONENT_NAME_VIEWER = "gf.core.component.viewer" - gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database" - gLOGGER_NODE_NAME = "logger" - gVIEWER_NODE_NAME = "viewer" ) var ( @@ -86,243 +60,7 @@ func SetIfNotExist(name string, instance interface{}) bool { return instances.SetIfNotExist(name, instance) } -// View returns an instance of View with default settings. -// The parameter is the name for the instance. -func View(name ...string) *gview.View { - instanceName := gview.DEFAULT_NAME - if len(name) > 0 && name[0] != "" { - instanceName = name[0] - } - instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName) - return instances.GetOrSetFuncLock(instanceKey, func() interface{} { - view := gview.Instance(instanceName) - // To avoid file no found error while it's not necessary. - if Config().Available() { - var m map[string]interface{} - // It firstly searches the configuration of the instance name. - if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName)); m == nil { - // If the configuration for the instance does not exist, - // it uses the default view configuration. - m = Config().GetMap(gVIEWER_NODE_NAME) - } - if m != nil { - if err := view.SetConfigWithMap(m); err != nil { - panic(err) - } - } - } - return view - }).(*gview.View) -} - -// Config returns an instance of View with default settings. -// The parameter is the name for the instance. -func Config(name ...string) *gcfg.Config { - return gcfg.Instance(name...) -} - -// Resource returns an instance of Resource. -// The parameter is the name for the instance. -func Resource(name ...string) *gres.Resource { - return gres.Instance(name...) -} - -// I18n returns an instance of gi18n.Manager. -// The parameter is the name for the instance. -func I18n(name ...string) *gi18n.Manager { - return gi18n.Instance(name...) -} - -// Log returns an instance of glog.Logger. -// The parameter is the name for the instance. -func Log(name ...string) *glog.Logger { - instanceName := glog.DEFAULT_NAME - if len(name) > 0 && name[0] != "" { - instanceName = name[0] - } - instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName) - return instances.GetOrSetFuncLock(instanceKey, func() interface{} { - logger := glog.Instance(instanceName) - // To avoid file no found error while it's not necessary. - if Config().Available() { - var m map[string]interface{} - // It firstly searches the configuration of the instance name. - if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gLOGGER_NODE_NAME, instanceName)); m == nil { - // If the configuration for the instance does not exist, - // it uses the default logging configuration. - m = Config().GetMap(gLOGGER_NODE_NAME) - } - if m != nil { - if err := logger.SetConfigWithMap(m); err != nil { - panic(err) - } - } - } - return logger - }).(*glog.Logger) -} - -// Database returns an instance of database ORM object -// with specified configuration group name. -func Database(name ...string) gdb.DB { - config := Config() - group := gdb.DEFAULT_GROUP_NAME - if len(name) > 0 && name[0] != "" { - group = name[0] - } - instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group) - db := instances.GetOrSetFuncLock(instanceKey, func() interface{} { - // Configuration already exists. - if gdb.GetConfig(group) != nil { - db, err := gdb.Instance(group) - if err != nil { - panic(err) - } - return db - } - m := config.GetMap("database") - if m == nil { - panic(`database init failed: "database" node not found, is config file or configuration missing?`) - } - // Parse as map-slice and adds it to gdb's global configurations. - for group, groupConfig := range m { - cg := gdb.ConfigGroup{} - switch value := groupConfig.(type) { - case []interface{}: - for _, v := range value { - if node := parseDBConfigNode(v); node != nil { - cg = append(cg, *node) - } - } - case map[string]interface{}: - if node := parseDBConfigNode(value); node != nil { - cg = append(cg, *node) - } - } - if len(cg) > 0 { - gdb.SetConfigGroup(group, cg) - } - } - // Parse as a single node configuration, - // which is the default group configuration. - if node := parseDBConfigNode(m); node != nil { - cg := gdb.ConfigGroup{} - if node.LinkInfo != "" || node.Host != "" { - cg = append(cg, *node) - } - if len(cg) > 0 { - gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg) - } - } - addConfigMonitor(instanceKey, config) - - if db, err := gdb.New(name...); err == nil { - // Initialize logger for ORM. - m := config.GetMap(fmt.Sprintf("database.%s", gLOGGER_NODE_NAME)) - if m == nil { - m = config.GetMap(gLOGGER_NODE_NAME) - } - if m != nil { - if err := db.GetLogger().SetConfigWithMap(m); err != nil { - panic(err) - } - } - return db - } else { - panic(err) - } - return nil - }) - if db != nil { - return db.(gdb.DB) - } - return nil -} - -func parseDBConfigNode(value interface{}) *gdb.ConfigNode { - nodeMap, ok := value.(map[string]interface{}) - if !ok { - return nil - } - node := &gdb.ConfigNode{} - err := gconv.Struct(nodeMap, node) - if err != nil { - panic(err) - } - if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil { - node.LinkInfo = gconv.String(v) - } - // Parse link syntax. - if node.LinkInfo != "" && node.Type == "" { - match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo) - if len(match) == 3 { - node.Type = gstr.Trim(match[1]) - node.LinkInfo = gstr.Trim(match[2]) - } - } - return node -} - -// Redis returns an instance of redis client with specified configuration group name. -func Redis(name ...string) *gredis.Redis { - config := Config() - group := gredis.DEFAULT_GROUP_NAME - if len(name) > 0 && name[0] != "" { - group = name[0] - } - instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group) - result := instances.GetOrSetFuncLock(instanceKey, func() interface{} { - // If already configured, it returns the redis instance. - if _, ok := gredis.GetConfig(group); ok { - return gredis.Instance(group) - } - // Or else, it parses the default configuration file and returns a new redis instance. - if m := config.GetMap("redis"); m != nil { - if v, ok := m[group]; ok { - redisConfig, err := gredis.ConfigFromStr(gconv.String(v)) - if err != nil { - panic(err) - } - addConfigMonitor(instanceKey, config) - return gredis.New(redisConfig) - } else { - panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group)) - } - } else { - panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath())) - } - return nil - }) - if result != nil { - return result.(*gredis.Redis) - } - return nil -} - -// Server returns an instance of http server with specified name. -func Server(name ...interface{}) *ghttp.Server { - instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name) - return instances.GetOrSetFuncLock(instanceKey, func() interface{} { - s := ghttp.GetServer(name...) - // To avoid file no found error while it's not necessary. - if Config().Available() { - var m map[string]interface{} - // It firstly searches the configuration of the instance name. - if m = Config().GetMap(fmt.Sprintf(`server.%s`, s.GetName())); m == nil { - // If the configuration for the instance does not exist, - // it uses the default server configuration. - m = Config().GetMap("server") - } - if m != nil { - if err := s.SetConfigWithMap(m); err != nil { - panic(err) - } - } - } - return s - }).(*ghttp.Server) -} - +// addConfigMonitor adds fsnotify monitor for configuration file if it exists. func addConfigMonitor(key string, config *gcfg.Config) { if path := config.FilePath(); path != "" && gfile.Exists(path) { _, err := gfsnotify.Add(path, func(event *gfsnotify.Event) { diff --git a/frame/gins/gins_config.go b/frame/gins/gins_config.go new file mode 100644 index 000000000..b68d74d0a --- /dev/null +++ b/frame/gins/gins_config.go @@ -0,0 +1,17 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "github.com/gogf/gf/os/gcfg" +) + +// Config returns an instance of View with default settings. +// The parameter is the name for the instance. +func Config(name ...string) *gcfg.Config { + return gcfg.Instance(name...) +} diff --git a/frame/gins/gins_database.go b/frame/gins/gins_database.go new file mode 100644 index 000000000..f39a7c16f --- /dev/null +++ b/frame/gins/gins_database.go @@ -0,0 +1,122 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "fmt" + "github.com/gogf/gf/text/gstr" + "github.com/gogf/gf/util/gutil" + + "github.com/gogf/gf/database/gdb" + "github.com/gogf/gf/text/gregex" + "github.com/gogf/gf/util/gconv" +) + +const ( + gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database" +) + +// Database returns an instance of database ORM object +// with specified configuration group name. +func Database(name ...string) gdb.DB { + config := Config() + group := gdb.DEFAULT_GROUP_NAME + if len(name) > 0 && name[0] != "" { + group = name[0] + } + instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_DATABASE, group) + db := instances.GetOrSetFuncLock(instanceKey, func() interface{} { + // Configuration already exists. + if gdb.GetConfig(group) != nil { + db, err := gdb.Instance(group) + if err != nil { + panic(err) + } + return db + } + m := config.GetMap("database") + if m == nil { + panic(`database init failed: "database" node not found, is config file or configuration missing?`) + } + // Parse as map-slice and adds it to gdb's global configurations. + for group, groupConfig := range m { + cg := gdb.ConfigGroup{} + switch value := groupConfig.(type) { + case []interface{}: + for _, v := range value { + if node := parseDBConfigNode(v); node != nil { + cg = append(cg, *node) + } + } + case map[string]interface{}: + if node := parseDBConfigNode(value); node != nil { + cg = append(cg, *node) + } + } + if len(cg) > 0 { + gdb.SetConfigGroup(group, cg) + } + } + // Parse as a single node configuration, + // which is the default group configuration. + if node := parseDBConfigNode(m); node != nil { + cg := gdb.ConfigGroup{} + if node.LinkInfo != "" || node.Host != "" { + cg = append(cg, *node) + } + if len(cg) > 0 { + gdb.SetConfigGroup(gdb.DEFAULT_GROUP_NAME, cg) + } + } + addConfigMonitor(instanceKey, config) + + if db, err := gdb.New(name...); err == nil { + // Initialize logger for ORM. + m := config.GetMap(fmt.Sprintf("database.%s", gLOGGER_NODE_NAME)) + if m == nil { + m = config.GetMap(gLOGGER_NODE_NAME) + } + if m != nil { + if err := db.GetLogger().SetConfigWithMap(m); err != nil { + panic(err) + } + } + return db + } else { + panic(err) + } + return nil + }) + if db != nil { + return db.(gdb.DB) + } + return nil +} + +func parseDBConfigNode(value interface{}) *gdb.ConfigNode { + nodeMap, ok := value.(map[string]interface{}) + if !ok { + return nil + } + node := &gdb.ConfigNode{} + err := gconv.Struct(nodeMap, node) + if err != nil { + panic(err) + } + if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil { + node.LinkInfo = gconv.String(v) + } + // Parse link syntax. + if node.LinkInfo != "" && node.Type == "" { + match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo) + if len(match) == 3 { + node.Type = gstr.Trim(match[1]) + node.LinkInfo = gstr.Trim(match[2]) + } + } + return node +} diff --git a/frame/gins/gins_i18n.go b/frame/gins/gins_i18n.go new file mode 100644 index 000000000..cc4cb8c40 --- /dev/null +++ b/frame/gins/gins_i18n.go @@ -0,0 +1,17 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "github.com/gogf/gf/i18n/gi18n" +) + +// I18n returns an instance of gi18n.Manager. +// The parameter is the name for the instance. +func I18n(name ...string) *gi18n.Manager { + return gi18n.Instance(name...) +} diff --git a/frame/gins/gins_log.go b/frame/gins/gins_log.go new file mode 100644 index 000000000..0b8f21b64 --- /dev/null +++ b/frame/gins/gins_log.go @@ -0,0 +1,46 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "fmt" + "github.com/gogf/gf/os/glog" +) + +const ( + gFRAME_CORE_COMPONENT_NAME_LOGGER = "gf.core.component.logger" + gLOGGER_NODE_NAME = "logger" +) + +// Log returns an instance of glog.Logger. +// The parameter is the name for the instance. +func Log(name ...string) *glog.Logger { + instanceName := glog.DEFAULT_NAME + if len(name) > 0 && name[0] != "" { + instanceName = name[0] + } + instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_LOGGER, instanceName) + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { + logger := glog.Instance(instanceName) + // To avoid file no found error while it's not necessary. + if Config().Available() { + var m map[string]interface{} + // It firstly searches the configuration of the instance name. + if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gLOGGER_NODE_NAME, instanceName)); m == nil { + // If the configuration for the instance does not exist, + // it uses the default logging configuration. + m = Config().GetMap(gLOGGER_NODE_NAME) + } + if m != nil { + if err := logger.SetConfigWithMap(m); err != nil { + panic(err) + } + } + } + return logger + }).(*glog.Logger) +} diff --git a/frame/gins/gins_redis.go b/frame/gins/gins_redis.go new file mode 100644 index 000000000..195308ab2 --- /dev/null +++ b/frame/gins/gins_redis.go @@ -0,0 +1,53 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "fmt" + "github.com/gogf/gf/database/gredis" + "github.com/gogf/gf/util/gconv" +) + +const ( + gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis" +) + +// Redis returns an instance of redis client with specified configuration group name. +func Redis(name ...string) *gredis.Redis { + config := Config() + group := gredis.DEFAULT_GROUP_NAME + if len(name) > 0 && name[0] != "" { + group = name[0] + } + instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_REDIS, group) + result := instances.GetOrSetFuncLock(instanceKey, func() interface{} { + // If already configured, it returns the redis instance. + if _, ok := gredis.GetConfig(group); ok { + return gredis.Instance(group) + } + // Or else, it parses the default configuration file and returns a new redis instance. + if m := config.GetMap("redis"); m != nil { + if v, ok := m[group]; ok { + redisConfig, err := gredis.ConfigFromStr(gconv.String(v)) + if err != nil { + panic(err) + } + addConfigMonitor(instanceKey, config) + return gredis.New(redisConfig) + } else { + panic(fmt.Sprintf(`configuration for redis not found for group "%s"`, group)) + } + } else { + panic(fmt.Sprintf(`incomplete configuration for redis: "redis" node not found in config file "%s"`, config.FilePath())) + } + return nil + }) + if result != nil { + return result.(*gredis.Redis) + } + return nil +} diff --git a/frame/gins/gins_resource.go b/frame/gins/gins_resource.go new file mode 100644 index 000000000..20f235a63 --- /dev/null +++ b/frame/gins/gins_resource.go @@ -0,0 +1,17 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "github.com/gogf/gf/os/gres" +) + +// Resource returns an instance of Resource. +// The parameter is the name for the instance. +func Resource(name ...string) *gres.Resource { + return gres.Instance(name...) +} diff --git a/frame/gins/gins_server.go b/frame/gins/gins_server.go new file mode 100644 index 000000000..421f157d9 --- /dev/null +++ b/frame/gins/gins_server.go @@ -0,0 +1,40 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "fmt" + "github.com/gogf/gf/net/ghttp" +) + +const ( + gFRAME_CORE_COMPONENT_NAME_SERVER = "gf.core.component.server" +) + +// Server returns an instance of http server with specified name. +func Server(name ...interface{}) *ghttp.Server { + instanceKey := fmt.Sprintf("%s.%v", gFRAME_CORE_COMPONENT_NAME_SERVER, name) + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { + s := ghttp.GetServer(name...) + // To avoid file no found error while it's not necessary. + if Config().Available() { + var m map[string]interface{} + // It firstly searches the configuration of the instance name. + if m = Config().GetMap(fmt.Sprintf(`server.%s`, s.GetName())); m == nil { + // If the configuration for the instance does not exist, + // it uses the default server configuration. + m = Config().GetMap("server") + } + if m != nil { + if err := s.SetConfigWithMap(m); err != nil { + panic(err) + } + } + } + return s + }).(*ghttp.Server) +} diff --git a/frame/gins/gins_view.go b/frame/gins/gins_view.go new file mode 100644 index 000000000..b7a525cc4 --- /dev/null +++ b/frame/gins/gins_view.go @@ -0,0 +1,46 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gins + +import ( + "fmt" + "github.com/gogf/gf/os/gview" +) + +const ( + gFRAME_CORE_COMPONENT_NAME_VIEWER = "gf.core.component.viewer" + gVIEWER_NODE_NAME = "viewer" +) + +// View returns an instance of View with default settings. +// The parameter is the name for the instance. +func View(name ...string) *gview.View { + instanceName := gview.DEFAULT_NAME + if len(name) > 0 && name[0] != "" { + instanceName = name[0] + } + instanceKey := fmt.Sprintf("%s.%s", gFRAME_CORE_COMPONENT_NAME_VIEWER, instanceName) + return instances.GetOrSetFuncLock(instanceKey, func() interface{} { + view := gview.Instance(instanceName) + // To avoid file no found error while it's not necessary. + if Config().Available() { + var m map[string]interface{} + // It firstly searches the configuration of the instance name. + if m = Config().GetMap(fmt.Sprintf(`%s.%s`, gVIEWER_NODE_NAME, instanceName)); m == nil { + // If the configuration for the instance does not exist, + // it uses the default view configuration. + m = Config().GetMap(gVIEWER_NODE_NAME) + } + if m != nil { + if err := view.SetConfigWithMap(m); err != nil { + panic(err) + } + } + } + return view + }).(*gview.View) +} diff --git a/frame/gins/gins_basic_test.go b/frame/gins/gins_z_unit_basic_test.go similarity index 100% rename from frame/gins/gins_basic_test.go rename to frame/gins/gins_z_unit_basic_test.go diff --git a/frame/gins/gins_config_test.go b/frame/gins/gins_z_unit_config_test.go similarity index 100% rename from frame/gins/gins_config_test.go rename to frame/gins/gins_z_unit_config_test.go diff --git a/frame/gins/gins_database_test.go b/frame/gins/gins_z_unit_database_test.go similarity index 100% rename from frame/gins/gins_database_test.go rename to frame/gins/gins_z_unit_database_test.go diff --git a/frame/gins/gins_redis_test.go b/frame/gins/gins_z_unit_redis_test.go similarity index 100% rename from frame/gins/gins_redis_test.go rename to frame/gins/gins_z_unit_redis_test.go diff --git a/frame/gins/gins_view_test.go b/frame/gins/gins_z_unit_view_test.go similarity index 100% rename from frame/gins/gins_view_test.go rename to frame/gins/gins_z_unit_view_test.go diff --git a/internal/empty/empty.go b/internal/empty/empty.go index 5927baedc..aa859c266 100644 --- a/internal/empty/empty.go +++ b/internal/empty/empty.go @@ -72,7 +72,7 @@ func IsEmpty(value interface{}) bool { } // IsNil checks whether given is nil. -// Note that it's using reflect feature which affects performance a little bit. +// Note that it might use reflect feature which affects performance a little bit. func IsNil(value interface{}) bool { if value == nil { return true diff --git a/internal/intlog/intlog.go b/internal/intlog/intlog.go index 221084dd9..52b39b0a6 100644 --- a/internal/intlog/intlog.go +++ b/internal/intlog/intlog.go @@ -42,7 +42,7 @@ func Print(v ...interface{}) { if !isGFDebug { return } - fmt.Println(append([]interface{}{now(), "[INTE]", file()}, v...)...) + fmt.Println(append([]interface{}{now(), "[GF]", file()}, v...)...) } // Printf prints with format using fmt.Printf. @@ -51,7 +51,7 @@ func Printf(format string, v ...interface{}) { if !isGFDebug { return } - fmt.Printf(now()+" [INTE] "+file()+" "+format+"\n", v...) + fmt.Printf(now()+" [GF] "+file()+" "+format+"\n", v...) } // Error prints with newline using fmt.Println. @@ -60,7 +60,7 @@ func Error(v ...interface{}) { if !isGFDebug { return } - array := append([]interface{}{now(), "[INTE]", file()}, v...) + array := append([]interface{}{now(), "[GF]", file()}, v...) array = append(array, "\n"+gdebug.StackWithFilter(gFILTER_KEY)) fmt.Println(array...) } @@ -71,7 +71,7 @@ func Errorf(format string, v ...interface{}) { return } fmt.Printf( - now()+" [INTE] "+file()+" "+format+"\n%s\n", + now()+" [GF] "+file()+" "+format+"\n%s\n", append(v, gdebug.StackWithFilter(gFILTER_KEY))..., ) }