github.com/zooyer/miskit@v1.0.71/sdk/jcon/main.go (about)

     1  package jcon
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  
     8  	"github.com/zooyer/jsons"
     9  )
    10  
    11  func Init(path string) error {
    12  	files, err := ioutil.ReadDir(path)
    13  	if err != nil {
    14  		return err
    15  	}
    16  	var c = make(map[string]map[string]*cacheInfo)
    17  	for _, file := range files {
    18  		if !file.IsDir() {
    19  			continue
    20  		}
    21  		ns, err := readNamespace(filepath.Join(path, file.Name()))
    22  		if err != nil {
    23  			return err
    24  		}
    25  		c[file.Name()] = ns
    26  	}
    27  	cache = c
    28  	dir = path
    29  	return nil
    30  }
    31  
    32  func Notify(namespace, name string, fn func()) (err error) {
    33  	return notify(namespace, name, fn)
    34  }
    35  
    36  func GetData(namespace, name string) (data []byte, err error) {
    37  	return getData(namespace, name)
    38  }
    39  
    40  func GetConfig(namespace, name string, v interface{}) (err error) {
    41  	data, err := getData(namespace, name)
    42  	if err != nil {
    43  		return
    44  	}
    45  
    46  	var raw []jsons.Raw
    47  	if err = json.Unmarshal(data, &raw); err != nil {
    48  		return
    49  	}
    50  
    51  	for i, r := range raw {
    52  		raw[i] = r.Get("config")
    53  	}
    54  
    55  	if data, err = json.Marshal(raw); err != nil {
    56  		return
    57  	}
    58  
    59  	return json.Unmarshal(data, v)
    60  }