当前位置: 首页>>代码示例>>Golang>>正文


Golang Msg.Map方法代码示例

本文整理汇总了Golang中github.com/garyguo110/transporter/pkg/message.Msg.Map方法的典型用法代码示例。如果您正苦于以下问题:Golang Msg.Map方法的具体用法?Golang Msg.Map怎么用?Golang Msg.Map使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/garyguo110/transporter/pkg/message.Msg的用法示例。


在下文中一共展示了Msg.Map方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: dumpMessage

/*
 * dump each message to the file
 */
func (d *File) dumpMessage(msg *message.Msg) (*message.Msg, error) {
	var line string

	if msg.IsMap() {
		ba, err := json.Marshal(msg.Map())
		if err != nil {
			d.pipe.Err <- NewError(ERROR, d.path, fmt.Sprintf("Can't unmarshal document (%s)", err.Error()), msg.Data)
			return msg, nil
		}
		line = string(ba)
	} else {
		line = fmt.Sprintf("%v", msg.Data)
	}

	if strings.HasPrefix(d.uri, "stdout://") {
		fmt.Println(line)
	} else {
		_, err := fmt.Fprintln(d.filehandle, line)
		if err != nil {
			d.pipe.Err <- NewError(ERROR, d.path, fmt.Sprintf("Error writing to file (%s)", err.Error()), msg.Data)
			return msg, nil
		}
	}

	return msg, nil
}
开发者ID:Garyguo110,项目名称:transporter,代码行数:29,代码来源:file.go

示例2: runCommand

func (e *Elasticsearch) runCommand(msg *message.Msg) error {
	if !msg.IsMap() {
		return nil
	}

	if _, hasKey := msg.Map()["flush"]; hasKey {
		e.indexer.Flush()
	}
	return nil
}
开发者ID:Garyguo110,项目名称:transporter,代码行数:10,代码来源:elasticsearch.go

示例3: applyOp

// applyOp applies one operation to the database
func (r *Rethinkdb) applyOp(msg *message.Msg) (*message.Msg, error) {
	var (
		resp gorethink.WriteResponse
		err  error
	)

	_, msgTable, err := msg.SplitNamespace()
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, fmt.Sprintf("rethinkdb error (msg namespace improperly formatted, must be database.table, got %s)", msg.Namespace), msg.Data)
		return msg, nil
	}
	if !msg.IsMap() {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (document must be a json document)", msg.Data)
		return msg, nil
	}
	doc := msg.Map()

	switch msg.Op {
	case message.Delete:
		id, err := msg.IDString("id")
		if err != nil {
			r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (cannot delete an object with a nil id)", msg.Data)
			return msg, nil
		}
		resp, err = gorethink.Table(msgTable).Get(id).Delete().RunWrite(r.client)
	case message.Insert:
		resp, err = gorethink.Table(msgTable).Insert(doc).RunWrite(r.client)
	case message.Update:
		resp, err = gorethink.Table(msgTable).Insert(doc, gorethink.InsertOpts{Conflict: "replace"}).RunWrite(r.client)
	}
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (%s)", err)
		return msg, nil
	}

	err = r.handleResponse(&resp)
	if err != nil {
		r.pipe.Err <- NewError(ERROR, r.path, "rethinkdb error (%s)", err)
	}

	return msg, nil
}
开发者ID:Garyguo110,项目名称:transporter,代码行数:43,代码来源:rethinkdb.go

示例4: writeMessage

// writeMessage writes one message to the destination mongo, or sends an error down the pipe
// TODO this can be cleaned up.  I'm not sure whether this should pipe the error, or whether the
//   caller should pipe the error
func (m *Mongodb) writeMessage(msg *message.Msg) (*message.Msg, error) {
	_, msgColl, err := msg.SplitNamespace()
	if err != nil {
		m.pipe.Err <- NewError(ERROR, m.path, fmt.Sprintf("mongodb error (msg namespace improperly formatted, must be database.collection, got %s)", msg.Namespace), msg.Data)
		return msg, nil
	}

	collection := m.mongoSession.DB(m.database).C(msgColl)

	if !msg.IsMap() {
		m.pipe.Err <- NewError(ERROR, m.path, fmt.Sprintf("mongodb error (document must be a bson document, got %T instead)", msg.Data), msg.Data)
		return msg, nil
	}

	doc := &SyncDoc{
		Doc:        msg.Map(),
		Collection: msgColl,
	}

	if m.bulk {
		m.bulkWriteChannel <- doc
	} else if msg.Op == message.Delete {
		err := collection.Remove(doc.Doc)
		if err != nil {
			m.pipe.Err <- NewError(ERROR, m.path, fmt.Sprintf("mongodb error removing (%s)", err.Error()), msg.Data)
		}
	} else {
		err := collection.Insert(doc.Doc)
		if mgo.IsDup(err) {
			err = collection.Update(bson.M{"_id": doc.Doc["_id"]}, doc.Doc)
		}
		if err != nil {
			m.pipe.Err <- NewError(ERROR, m.path, fmt.Sprintf("mongodb error (%s)", err.Error()), msg.Data)
		}
	}

	return msg, nil
}
开发者ID:Garyguo110,项目名称:transporter,代码行数:41,代码来源:mongodb.go


注:本文中的github.com/garyguo110/transporter/pkg/message.Msg.Map方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。