gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/modifyx/modify-source.go (about)

     1  package modifyx
     2  
     3  import (
     4  	"gitee.com/zhongguo168a/gocodes/datax"
     5  	"gitee.com/zhongguo168a/gocodes/datax/convertx"
     6  	"gitee.com/zhongguo168a/gocodes/datax/mapx"
     7  	"sync"
     8  )
     9  
    10  type M = map[string]interface{}
    11  type A = []interface{}
    12  
    13  // className = origin["_type"]
    14  func NewSource() *Source {
    15  	return NewSourceWith(datax.M{})
    16  }
    17  
    18  func NewSourceWith(data datax.M) *Source {
    19  	return &Source{
    20  		origin:     mapx.Flat(data, true),
    21  		typ:        mapx.String(data, "_type"),
    22  		modifyList: map[string][]IModify{},
    23  	}
    24  }
    25  
    26  // 数据源
    27  // 通常加载后,数据源不可变,作为配置文件的映射
    28  // 采用数据源的目的是为了避免数据的克隆影响性能,毕竟需要修正的地方有限
    29  type Source struct {
    30  	// 数据
    31  	origin map[string]interface{}
    32  	//
    33  	modifyList map[string][]IModify
    34  	//
    35  	id string
    36  	//
    37  	catalog string
    38  	// 作为结构时的类名
    39  	typ string
    40  	//
    41  	mutex sync.RWMutex
    42  }
    43  
    44  func (s *Source) ClassId() string {
    45  	return s.id
    46  }
    47  func (s *Source) SetClassId(val string) {
    48  	s.id = val
    49  }
    50  func (s *Source) ClassKey() string {
    51  	return s.catalog + "/" + s.id
    52  }
    53  
    54  func (s *Source) ClassCatalog() string {
    55  	return s.catalog
    56  }
    57  func (s *Source) ClassType() string {
    58  	return s.typ
    59  }
    60  
    61  // co.CModify
    62  func (p *Source) AddModify(val IModify) {
    63  	list := p.modifyList[val.Path()]
    64  	list = append(list, val)
    65  	p.modifyList[val.Path()] = list
    66  }
    67  
    68  func (s *Source) GetOrigin(path string) interface{} {
    69  	s.mutex.RLock()
    70  	lastValue := s.origin[path]
    71  	s.mutex.RUnlock()
    72  	return lastValue
    73  }
    74  func (s *Source) IsNil(path string) bool {
    75  	s.mutex.RLock()
    76  	lastValue := s.origin[path]
    77  	s.mutex.RUnlock()
    78  	return lastValue == nil
    79  }
    80  func (s *Source) MapKeys(path string) []string {
    81  	s.mutex.RLock()
    82  	lastValue := s.origin[path+"/keys"]
    83  	s.mutex.RUnlock()
    84  	return lastValue.([]string)
    85  }
    86  
    87  // 获取 map或者array的长度,需要先判断 IsNil
    88  func (s *Source) Length(path string) int {
    89  	s.mutex.RLock()
    90  	lastValue := s.origin[path]
    91  	s.mutex.RUnlock()
    92  	return convertx.AnyToInt(lastValue)
    93  }
    94  
    95  func (s *Source) Get(ctx interface{}, path string) interface{} {
    96  	s.mutex.RLock()
    97  	lastValue := s.origin[path]
    98  	modifys := s.modifyList[path]
    99  	s.mutex.RUnlock()
   100  	if modifys == nil {
   101  		return lastValue
   102  	}
   103  
   104  	for _, imodify := range modifys {
   105  		modify := imodify.(IModify)
   106  		lastValue = modify.Value(ctx, lastValue)
   107  	}
   108  
   109  	return lastValue
   110  }
   111  
   112  func (s *Source) Dispose() {
   113  	s.origin = nil
   114  }
   115  
   116  func (s *Source) Clone() (r *Source) {
   117  	r = NewSourceWith(mapx.CloneMap(s.origin))
   118  	return
   119  }