github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/node/parser/parser.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/point"
     8  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/subscribe"
     9  	"github.com/Asutorufa/yuhaiin/pkg/utils/syncmap"
    10  )
    11  
    12  var store syncmap.SyncMap[subscribe.Type, func(data []byte) (*point.Point, error)]
    13  
    14  func Parse(t subscribe.Type, data []byte) (*point.Point, error) {
    15  	parser, ok := store.Load(t)
    16  	if !ok {
    17  		return nil, fmt.Errorf("no support %s", t)
    18  	}
    19  
    20  	return parser(data)
    21  }
    22  
    23  func trimJSON(b []byte, start, end byte) []byte {
    24  	s := bytes.IndexByte(b, start)
    25  	e := bytes.LastIndexByte(b, end)
    26  	if s == -1 || e == -1 {
    27  		return b
    28  	}
    29  	return b[s : e+1]
    30  }