本文整理匯總了Golang中github.com/nytlabs/streamtools/st/util.ParseString函數的典型用法代碼示例。如果您正苦於以下問題:Golang ParseString函數的具體用法?Golang ParseString怎麽用?Golang ParseString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ParseString函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Run
// connects to an NSQ topic and emits each message into streamtools.
func (b *ToNSQ) Run() {
var writer *nsq.Writer
for {
select {
case ruleI := <-b.inrule:
//rule := ruleI.(map[string]interface{})
topic, err := util.ParseString(ruleI, "Topic")
if err != nil {
b.Error(err)
break
}
nsqdTCPAddrs, err := util.ParseString(ruleI, "NsqdTCPAddrs")
if err != nil {
b.Error(err)
break
}
if writer != nil {
writer.Stop()
}
writer = nsq.NewWriter(nsqdTCPAddrs)
b.topic = topic
b.nsqdTCPAddrs = nsqdTCPAddrs
case msg := <-b.in:
if writer == nil {
continue
}
msgBytes, err := json.Marshal(msg)
if err != nil {
b.Error(err)
}
if len(msgBytes) == 0 {
continue
}
_, _, err = writer.Publish(b.topic, msgBytes)
if err != nil {
b.Error(err)
}
case <-b.quit:
if writer != nil {
writer.Stop()
}
return
case c := <-b.queryrule:
c <- map[string]interface{}{
"Topic": b.topic,
"NsqdTCPAddrs": b.nsqdTCPAddrs,
}
}
}
}
示例2: Run
// connects to an NSQ topic and emits each message into streamtools.
func (b *ToElasticsearch) Run() {
var err error
var esIndex string
var esType string
conn := elastigo.NewConn()
host := "localhost"
port := "9200"
for {
select {
case ruleI := <-b.inrule:
host, err = util.ParseString(ruleI, "Host")
if err != nil {
b.Error(err)
break
}
port, err = util.ParseString(ruleI, "Port")
if err != nil {
b.Error(err)
break
}
esIndex, err = util.ParseString(ruleI, "Index")
if err != nil {
b.Error(err)
break
}
esType, err = util.ParseString(ruleI, "Type")
if err != nil {
b.Error(err)
break
}
conn.Domain = host
conn.Port = port
case msg := <-b.in:
_, err := conn.Index(esIndex, esType, "", nil, msg)
if err != nil {
b.Error(err)
}
case <-b.quit:
return
case c := <-b.queryrule:
c <- map[string]interface{}{
"Host": host,
"Port": port,
"Index": esIndex,
"Type": esType,
}
}
}
}
示例3: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
// This block posts a message to a specified Elasticsearch index with the given type.
func (b *ToElasticsearch) Run() {
var err error
var index string
var indextype string
host := "localhost"
port := "9200"
for {
select {
case msgI := <-b.inrule:
host, err = util.ParseString(msgI, "Host")
if err != nil {
b.Error(err)
continue
}
port, err = util.ParseString(msgI, "Port")
if err != nil {
b.Error(err)
continue
}
index, err = util.ParseString(msgI, "Index")
if err != nil {
b.Error(err)
continue
}
indextype, err = util.ParseString(msgI, "IndexType")
if err != nil {
b.Error(err)
continue
}
// Set the Elasticsearch Host/Port to Connect to
api.Domain = host
api.Port = port
case MsgChan := <-b.queryrule:
// deal with a query request
MsgChan <- map[string]interface{}{
"Host": host,
"Port": port,
"Index": index,
"IndexType": indextype,
}
case <-b.quit:
// quit the block
return
case msg := <-b.in:
var args map[string]interface{}
_, err := core.Index(index, indextype, "", args, msg)
if err != nil {
b.Error(err)
}
}
}
}
示例4: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *ToHTTPGetRequest) Run() {
var respPath, msgPath string
var respTree, msgTree *jee.TokenTree
var err error
for {
select {
case ruleI := <-b.inrule:
respPath, err = util.ParseString(ruleI, "RespPath")
respTree, err = util.BuildTokenTree(respPath)
if err != nil {
b.Error(err)
break
}
msgPath, err = util.ParseString(ruleI, "MsgPath")
msgTree, err = util.BuildTokenTree(msgPath)
if err != nil {
b.Error(err)
break
}
case <-b.quit:
return
case msg := <-b.in:
if respTree == nil {
continue
}
if msgTree == nil {
continue
}
cI, err := jee.Eval(respTree, msg)
if err != nil {
b.Error(err)
break
}
c, ok := cI.(blocks.MsgChan)
if !ok {
b.Error(errors.New("response path must point to a channel"))
continue
}
m, err := jee.Eval(msgTree, msg)
if err != nil {
b.Error(err)
break
}
c <- m
case responseChan := <-b.queryrule:
// deal with a query request
responseChan <- map[string]interface{}{
"RespPath": respPath,
"MsgPath": msgPath,
}
}
}
}
示例5: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *FromSQS) Run() {
var err error
for {
select {
case msgI := <-b.inrule:
for k, _ := range b.auth {
b.auth[k], err = util.ParseString(msgI, k)
if err != nil {
b.Error(err)
break
}
}
b.stopListening()
go b.listener()
case <-b.quit:
b.stopListening()
return
case msg := <-b.fromListener:
var outMsg interface{}
err := json.Unmarshal(msg, &outMsg)
if err != nil {
b.Error(err)
continue
}
b.out <- outMsg
case MsgChan := <-b.queryrule:
// deal with a query request
MsgChan <- b.auth
}
}
}
示例6: Run
func (b *Count) Run() {
waitTimer := time.NewTimer(100 * time.Millisecond)
pq := &PriorityQueue{}
heap.Init(pq)
window := time.Duration(0)
for {
select {
case <-waitTimer.C:
case rule := <-b.inrule:
tmpDurStr, err := util.ParseString(rule, "Window")
if err != nil {
b.Error(err)
continue
}
tmpWindow, err := time.ParseDuration(tmpDurStr)
if err != nil {
b.Error(err)
continue
}
window = tmpWindow
case <-b.quit:
return
case <-b.in:
empty := make([]byte, 0)
queueMessage := &PQMessage{
val: &empty,
t: time.Now(),
}
heap.Push(pq, queueMessage)
case <-b.inpoll:
b.out <- map[string]interface{}{
"Count": len(*pq),
}
case c := <-b.queryrule:
c <- map[string]interface{}{
"Window": window.String(),
}
case c := <-b.querycount:
c <- map[string]interface{}{
"Count": len(*pq),
}
}
for {
pqMsg, diff := pq.PeekAndShift(time.Now(), window)
if pqMsg == nil {
// either the queue is empty, or it"s not time to emit
if diff == 0 {
// then the queue is empty. Pause for 5 seconds before checking again
diff = time.Duration(500) * time.Millisecond
}
waitTimer.Reset(diff)
break
}
}
}
}
示例7: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *Set) Run() {
var path string
set := make(map[interface{}]bool)
var tree *jee.TokenTree
var err error
for {
select {
case ruleI := <-b.inrule:
// set a parameter of the block
path, err = util.ParseString(ruleI, "Path")
tree, err = util.BuildTokenTree(path)
if err != nil {
b.Error(err)
break
}
case <-b.quit:
// quit the block
return
case msg := <-b.add:
if tree == nil {
continue
}
v, err := jee.Eval(tree, msg)
if err != nil {
b.Error(err)
break
}
if _, ok := v.(string); !ok {
b.Error(errors.New("can only build sets of strings"))
continue
}
set[v] = true
// deal with inbound data
case msg := <-b.isMember:
if tree == nil {
continue
}
v, err := jee.Eval(tree, msg)
if err != nil {
b.Error(err)
break
}
_, ok := set[v]
b.out <- map[string]interface{}{
"isMember": ok,
}
case c := <-b.cardinality:
c <- map[string]interface{}{
"cardinality": len(set),
}
case c := <-b.queryrule:
// deal with a query request
c <- map[string]interface{}{
"Path": path,
}
}
}
}
示例8: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *Unpack) Run() {
var path string
var err error
var tree *jee.TokenTree
for {
select {
case ruleI := <-b.inrule:
// set a parameter of the block
rule, ok := ruleI.(map[string]interface{})
if !ok {
b.Error(errors.New("cannot assert rule to map"))
}
path, err = util.ParseString(rule, "Path")
if err != nil {
b.Error(err)
continue
}
token, err := jee.Lexer(path)
if err != nil {
b.Error(err)
continue
}
tree, err = jee.Parser(token)
if err != nil {
b.Error(err)
continue
}
case <-b.quit:
// quit the block
return
case msg := <-b.in:
if tree == nil {
continue
}
arrInterface, err := jee.Eval(tree, msg)
if err != nil {
b.Error(err)
continue
}
arr, ok := arrInterface.([]interface{})
if !ok {
b.Error(errors.New("cannot assert " + path + " to array"))
continue
}
for _, out := range arr {
b.out <- out
}
case c := <-b.queryrule:
// deal with a query request
out := map[string]interface{}{
"Path": path,
}
c <- out
}
}
}
示例9: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *FromWebsocket) Run() {
var ws *websocket.Conn
var url string
to, _ := time.ParseDuration("10s")
var handshakeDialer = &websocket.Dialer{
Subprotocols: []string{"p1", "p2"},
HandshakeTimeout: to,
}
listenWS := make(blocks.MsgChan)
wsHeader := http.Header{"Origin": {"http://localhost/"}}
toOut := make(blocks.MsgChan)
toError := make(chan error)
for {
select {
case msg := <-toOut:
b.out <- msg
case ruleI := <-b.inrule:
var err error
// set a parameter of the block
url, err = util.ParseString(ruleI, "url")
if err != nil {
b.Error(err)
continue
}
if ws != nil {
ws.Close()
}
ws, _, err = handshakeDialer.Dial(url, wsHeader)
if err != nil {
b.Error("could not connect to url")
break
}
ws.SetReadDeadline(time.Time{})
h := recvHandler{toOut, toError}
go h.recv(ws, listenWS)
case err := <-toError:
b.Error(err)
case <-b.quit:
// quit the block
return
case o := <-b.queryrule:
o <- map[string]interface{}{
"url": url,
}
case in := <-listenWS:
b.out <- in
}
}
}
示例10: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *PackByInterval) Run() {
var batch []interface{}
interval := time.Duration(1) * time.Second
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
b.out <- map[string]interface{}{
"Pack": batch,
}
batch = nil
case ruleI := <-b.inrule:
intervalS, err := util.ParseString(ruleI, "Interval")
if err != nil {
b.Error("error parsing batch size")
break
}
dur, err := time.ParseDuration(intervalS)
if err != nil {
b.Error(err)
break
}
if dur <= 0 {
b.Error("interval must be positive")
break
}
interval = dur
ticker.Stop()
ticker = time.NewTicker(interval)
batch = nil
case <-b.quit:
// quit the block
return
case m := <-b.in:
batch = append(batch, m)
case <-b.clear:
batch = nil
case <-b.flush:
b.out <- map[string]interface{}{
"Pack": batch,
}
batch = nil
case r := <-b.queryrule:
r <- map[string]interface{}{
"Interval": interval.String(),
}
}
}
}
示例11: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *FromFile) Run() {
var file *os.File
var filename string
for {
select {
case msgI := <-b.inrule:
// set a parameter of the block
filename, err := util.ParseString(msgI, "Filename")
if err != nil {
b.Error(err)
continue
}
file, err := os.Open(filename)
if err != nil {
b.Error(err)
continue
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var outMsg interface{}
lineBytes := scanner.Bytes()
err := json.Unmarshal(lineBytes, &outMsg)
// if the json parsing fails, store data unparsed as "data"
if err != nil {
outMsg = map[string]interface{}{
"data": lineBytes,
}
}
b.out <- outMsg
}
if err := scanner.Err(); err != nil {
b.Error(err)
continue
}
case <-b.quit:
// quit the block
if file != nil {
file.Close()
}
return
case c := <-b.queryrule:
c <- map[string]interface{}{
"Filename": filename,
}
}
}
}
示例12: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
// This block posts a message to a specified Elasticsearch index with the given type.
func (b *ToElasticsearch) Run() {
for {
select {
case msgI := <-b.inrule:
host, _ := util.ParseString(msgI, "Host")
port, _ := util.ParseString(msgI, "Port")
index, _ := util.ParseString(msgI, "Index")
indextype, _ := util.ParseString(msgI, "IndexType")
// Set the Elasticsearch Host/Port to Connect to
api.Domain = host
api.Port = port
b.host = host
b.port = port
b.index = index
b.indextype = indextype
case respChan := <-b.queryrule:
// deal with a query request
respChan <- map[string]interface{}{
"Host": b.host,
"Port": b.port,
"Index": b.index,
"IndexType": b.indextype,
}
case <-b.quit:
// quit the block
return
case msg := <-b.in:
var args map[string]interface{}
_, err := core.Index(b.index, b.indextype, "", args, msg)
if err != nil {
b.Error(err)
}
}
}
}
示例13: buildEmail
// buildEmail will attempt to pull the email's properties from the expected paths and
// put the email body together.
func (e *ToEmail) buildEmail(msg interface{}) (from, to string, email []byte, err error) {
from, err = util.ParseString(msg, e.fromPath)
if err != nil {
return
}
to, err = util.ParseString(msg, e.toPath)
if err != nil {
return
}
var subject string
subject, err = util.ParseString(msg, e.subjectPath)
if err != nil {
return
}
var body string
body, err = util.ParseString(msg, e.msgPath)
if err != nil {
return
}
email = []byte(fmt.Sprintf(emailTmpl, from, to, subject, body))
return
}
示例14: Run
// Run is the block's main loop. Here we listen on the different channels we set up.
func (b *DeDupe) Run() {
var path string
set := make(map[interface{}]bool)
var tree *jee.TokenTree
var err error
for {
select {
case ruleI := <-b.inrule:
// set a parameter of the block
path, err = util.ParseString(ruleI, "Path")
tree, err = util.BuildTokenTree(path)
if err != nil {
b.Error(err)
break
}
case <-b.quit:
// quit the block
return
// deal with inbound data
case msg := <-b.in:
if tree == nil {
continue
}
v, err := jee.Eval(tree, msg)
if err != nil {
b.Error(err)
break
}
if _, ok := v.(string); !ok {
b.Error(errors.New("can only dedupe sets of strings"))
continue
}
_, ok := set[v]
// emit the incoming message if it isn't found in the set
if !ok {
b.out <- msg
set[v] = true // and add it to the set
}
case c := <-b.queryrule:
// deal with a query request
c <- map[string]interface{}{
"Path": path,
}
}
}
}
示例15: parseEmailRules
// parseEmailInRules will expect a payload from the inrules channel and
// attempt to pull and set the block's to, from and subject paths from it.
func (e *ToEmail) parseEmailRules(msgI interface{}) error {
var err error
e.toPath, err = util.ParseRequiredString(msgI, "ToPath")
if err != nil {
return err
}
e.fromPath, err = util.ParseRequiredString(msgI, "FromPath")
if err != nil {
return err
}
e.subjectPath, err = util.ParseString(msgI, "SubjectPath")
if err != nil {
return err
}
e.msgPath, err = util.ParseString(msgI, "MessagePath")
if err != nil {
return err
}
return nil
}