github.com/aacfactory/fns-contrib/databases/sql@v1.2.84/dac/specifications/dict.go (about)

     1  package specifications
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sync"
     7  )
     8  
     9  func NewDict() *Dict {
    10  	return &Dict{
    11  		values: sync.Map{},
    12  	}
    13  }
    14  
    15  type Dict struct {
    16  	values sync.Map
    17  }
    18  
    19  func (dict *Dict) Get(key ...any) (value []string, has bool) {
    20  	keyLen := len(key)
    21  	if keyLen == 0 || keyLen > 2 {
    22  		return
    23  	}
    24  	rv := reflect.Indirect(reflect.ValueOf(key[0]))
    25  	rt := rv.Type()
    26  	if rt.Kind() != reflect.Struct {
    27  		return
    28  	}
    29  	st := fmt.Sprintf("%s.%s", rt.PkgPath(), rt.Name())
    30  	if keyLen == 1 {
    31  		v, exist := dict.values.Load(st)
    32  		if exist {
    33  			value, has = v.([]string)
    34  		}
    35  		return
    36  	}
    37  	v, exist := dict.values.Load(fmt.Sprintf("%s:%s", st, key[1]))
    38  	if exist {
    39  		value, has = v.([]string)
    40  	}
    41  	return
    42  }
    43  
    44  // Set
    45  // table: key is {path}.{name}, value is table name
    46  // column: key is {path}.{name}:{field}, value is column name
    47  func (dict *Dict) Set(key string, value ...string) {
    48  	dict.values.Store(key, value)
    49  	return
    50  }
    51  
    52  func DictSet(key string, value ...string) {
    53  	dict.Set(key, value...)
    54  }