improve package gcfg

This commit is contained in:
John Guo
2022-05-18 15:11:49 +08:00
parent 3430cf1f17
commit c871bb3a1e
3 changed files with 19 additions and 17 deletions

View File

@ -85,7 +85,11 @@ func (c *Config) GetAdapter() Adapter {
// It returns true if configuration file is present in default AdapterFile, or else false.
// Note that this function does not return error as it just does simply check for backend configuration service.
func (c *Config) Available(ctx context.Context, resource ...string) (ok bool) {
return c.adapter.Available(ctx, resource...)
var usedResource string
if len(resource) > 0 {
usedResource = resource[0]
}
return c.adapter.Available(ctx, usedResource)
}
// Get retrieves and returns value by specified `pattern`.

View File

@ -10,17 +10,20 @@ import "context"
// Adapter is the interface for configuration retrieving.
type Adapter interface {
// Available checks and returns the configuration service is available.
// Available checks and returns the backend configuration service is available.
// The optional parameter `resource` specifies certain configuration resource.
//
// It returns true if configuration file is present in default AdapterFile, or else false.
// Note that this function does not return error as it just does simply check for backend configuration service.
Available(ctx context.Context, resource ...string) (ok bool)
// Note that this function does not return error as it just does simply check for
// backend configuration service.
Available(ctx context.Context, resource string) (ok bool)
// Get retrieves and returns value by specified `pattern`.
// Get retrieves and returns value by specified `pattern` in current resource.
// Pattern like:
// "x.y.z" for map item.
// "x.0.y" for slice item.
Get(ctx context.Context, pattern string) (value interface{}, err error)
// Data retrieves and returns all configuration data as map type.
// Data retrieves and returns all configuration data in current resource as map.
// Note that this function may lead lots of memory usage if configuration data is too large,
// you can implement this function if necessary.
Data(ctx context.Context) (data map[string]interface{}, err error)

View File

@ -197,21 +197,16 @@ func (c *AdapterFile) Dump() {
}
// Available checks and returns whether configuration of given `file` is available.
func (c *AdapterFile) Available(ctx context.Context, fileName ...string) bool {
var (
usedFileName string
)
if len(fileName) > 0 && fileName[0] != "" {
usedFileName = fileName[0]
} else {
usedFileName = c.defaultName
func (c *AdapterFile) Available(ctx context.Context, fileName string) bool {
if fileName == "" {
fileName = c.defaultName
}
// Custom configuration content exists.
if c.GetContent(usedFileName) != "" {
if c.GetContent(fileName) != "" {
return true
}
// Configuration file exists in system path.
if path, _ := c.GetFilePath(usedFileName); path != "" {
if path, _ := c.GetFilePath(fileName); path != "" {
return true
}
return false