github.com/wangyougui/gf/v2@v2.6.5/os/gcfg/gcfg_adapter_content.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gcfg
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gvar"
    13  	"github.com/wangyougui/gf/v2/encoding/gjson"
    14  	"github.com/wangyougui/gf/v2/errors/gerror"
    15  )
    16  
    17  // AdapterContent implements interface Adapter using content.
    18  // The configuration content supports the coding types as package `gjson`.
    19  type AdapterContent struct {
    20  	jsonVar *gvar.Var // The pared JSON object for configuration content, type: *gjson.Json.
    21  }
    22  
    23  // NewAdapterContent returns a new configuration management object using custom content.
    24  // The parameter `content` specifies the default configuration content for reading.
    25  func NewAdapterContent(content ...string) (*AdapterContent, error) {
    26  	a := &AdapterContent{
    27  		jsonVar: gvar.New(nil, true),
    28  	}
    29  	if len(content) > 0 {
    30  		if err := a.SetContent(content[0]); err != nil {
    31  			return nil, err
    32  		}
    33  	}
    34  	return a, nil
    35  }
    36  
    37  // SetContent sets customized configuration content for specified `file`.
    38  // The `file` is unnecessary param, default is DefaultConfigFile.
    39  func (a *AdapterContent) SetContent(content string) error {
    40  	j, err := gjson.LoadContent(content, true)
    41  	if err != nil {
    42  		return gerror.Wrap(err, `load configuration content failed`)
    43  	}
    44  	a.jsonVar.Set(j)
    45  	return nil
    46  }
    47  
    48  // Available checks and returns the backend configuration service is available.
    49  // The optional parameter `resource` specifies certain configuration resource.
    50  //
    51  // Note that this function does not return error as it just does simply check for
    52  // backend configuration service.
    53  func (a *AdapterContent) Available(ctx context.Context, resource ...string) (ok bool) {
    54  	if a.jsonVar.IsNil() {
    55  		return false
    56  	}
    57  	return true
    58  }
    59  
    60  // Get retrieves and returns value by specified `pattern` in current resource.
    61  // Pattern like:
    62  // "x.y.z" for map item.
    63  // "x.0.y" for slice item.
    64  func (a *AdapterContent) Get(ctx context.Context, pattern string) (value interface{}, err error) {
    65  	if a.jsonVar.IsNil() {
    66  		return nil, nil
    67  	}
    68  	return a.jsonVar.Val().(*gjson.Json).Get(pattern).Val(), nil
    69  }
    70  
    71  // Data retrieves and returns all configuration data in current resource as map.
    72  // Note that this function may lead lots of memory usage if configuration data is too large,
    73  // you can implement this function if necessary.
    74  func (a *AdapterContent) Data(ctx context.Context) (data map[string]interface{}, err error) {
    75  	if a.jsonVar.IsNil() {
    76  		return nil, nil
    77  	}
    78  	return a.jsonVar.Val().(*gjson.Json).Var().Map(), nil
    79  }