gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/config.v2/bytes.go (about)

     1  package config
     2  
     3  import (
     4  	"bytes"
     5  	"gitee.com/sy_183/go-common/lock"
     6  	"gitee.com/sy_183/go-common/yaml"
     7  	"sync"
     8  )
     9  
    10  type BytesLoader struct {
    11  	bytes []byte
    12  	typ   Type
    13  	mu    sync.Mutex
    14  }
    15  
    16  func (l *BytesLoader) SetBytes(bs []byte, typ Type) {
    17  	if typ.Id == TypeUnknown.Id {
    18  		return
    19  	}
    20  	lock.LockDo(&l.mu, func() {
    21  		l.bytes = bs
    22  		l.typ = typ
    23  	})
    24  }
    25  
    26  func (l *BytesLoader) Load() (*yaml.Node, error) {
    27  	bs, typ := lock.LockGetDouble(&l.mu, func() ([]byte, Type) {
    28  		return l.bytes, l.typ
    29  	})
    30  	if typ.Id == TypeUnknown.Id {
    31  		return nil, nil
    32  	}
    33  	return typ.Parser.Parse(bytes.NewReader(bs))
    34  }