本文整理汇总了Golang中labix/org/v2/mgo/bson.Marshal函数的典型用法代码示例。如果您正苦于以下问题:Golang Marshal函数的具体用法?Golang Marshal怎么用?Golang Marshal使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Marshal函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Put
func (s *Submission) Put() error {
s.Modified = time.Now()
if s.Id == 0 {
s.Created = s.Modified
b, err := bson.Marshal(s)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO submissions(author_id, struct, created, modified) VALUES(?, ?, ?, ?)", s.AuthorId, b, s.Created, s.Modified)
if err != nil {
return err
}
chZerk <- s.AuthorId
s.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(s)
if err != nil {
return err
}
_, err = db.Exec("UPDATE submissions SET author_id=?, struct=?, modified=? WHERE id=?", s.AuthorId, b, s.Modified, s.Id)
chZerk <- s.AuthorId
return err
}
}
示例2: Put
func (s *Standing) Put() error {
s.Modified = time.Now()
if s.Id == 0 {
s.Created = s.Modified
b, err := bson.Marshal(s)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO standings(author_id, score, penalty, struct, created, modified) VALUES(?, ?, ?, ?, ?, ?)", s.AuthorId, s.Score, s.Penalty, b, s.Created, s.Modified)
if err != nil {
return err
}
s.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(s)
if err != nil {
return err
}
_, err = db.Exec("UPDATE standings SET author_id=?, score=?, penalty=?, struct=?, modified=? WHERE id=?", s.AuthorId, s.Score, s.Penalty, b, s.Modified, s.Id)
return err
}
}
示例3: Put
func (c *Clarification) Put() error {
c.Modified = time.Now()
if c.Id == 0 {
c.Created = c.Modified
b, err := bson.Marshal(c)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO clarifications(asker_id, response, struct, created, modified) VALUES(?, ?, ?, ?, ?)", c.AskerId, c.Response, b, c.Created, c.Modified)
if err != nil {
return err
}
c.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(c)
if err != nil {
return err
}
_, err = db.Exec("UPDATE clarifications SET asker_id=?, response=?, struct=?, modified=? WHERE id=?", c.AskerId, c.Response, b, c.Modified, c.Id)
return err
}
}
示例4: Put
func (a *Account) Put() error {
a.Modified = time.Now()
if a.Id == 0 {
a.Created = a.Modified
b, err := bson.Marshal(a)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO accounts(handle_lower, name_lower, level, struct, created, modified) VALUES(?, ?, ?, ?, ?, ?)", strings.ToLower(a.Handle), strings.ToLower(a.Name), a.Level, b, a.Created, a.Modified)
if err != nil {
return err
}
a.Id, err = res.LastInsertId()
if err != nil {
return err
}
chZerk <- a.Id
return nil
} else {
b, err := bson.Marshal(a)
if err != nil {
return err
}
_, err = db.Exec("UPDATE accounts SET handle_lower=?, name_lower=?, level=?, struct=?, modified=? WHERE id=?", strings.ToLower(a.Handle), strings.ToLower(a.Name), a.Level, b, a.Modified, a.Id)
chZerk <- a.Id
return err
}
}
示例5: Put
func (e *Execution) Put() error {
e.Modified = time.Now()
if e.Id == 0 {
e.Created = e.Modified
b, err := bson.Marshal(e)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO executions(struct, created, modified) VALUES(?, ?, ?)", b, e.Created, e.Modified)
if err != nil {
return err
}
e.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(e)
if err != nil {
return err
}
_, err = db.Exec("UPDATE executions SET struct=?, modified=? WHERE id=?", b, e.Modified, e.Id)
return err
}
}
示例6: Put
func (p *Problem) Put() error {
p.Modified = time.Now()
if p.Id == 0 {
p.Created = p.Modified
b, err := bson.Marshal(p)
if err != nil {
return err
}
res, err := db.Exec("INSERT INTO problems(slug, char, struct, created, modified) VALUES(?, ?, ?, ?, ?)", p.Slug, p.Char, b, p.Created, p.Modified)
if err != nil {
return err
}
p.Id, err = res.LastInsertId()
return err
} else {
b, err := bson.Marshal(p)
if err != nil {
return err
}
_, err = db.Exec("UPDATE problems SET slug=?, char=?, struct=?, modified=? WHERE id=?", p.Slug, p.Char, b, p.Modified, p.Id)
return err
}
}
示例7: TestDecodeReadsOnlyOne
func TestDecodeReadsOnlyOne(t *testing.T) {
req := rpc.Request{
ServiceMethod: "Foo.Bar",
Seq: 3,
}
type T struct {
Value string
}
tv := T{"test"}
b, err := bson.Marshal(req)
if err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(b)
b, err = bson.Marshal(tv)
if err != nil {
t.Fatal(err)
}
buf.Write(b)
dec := NewDecoder(buf)
r := new(rpc.Request)
err = dec.Decode(r)
if *r != req {
t.Fatal("Values don't match")
}
if err != nil {
t.Fatal(err)
}
// We should be able to read a second message off this io.Reader
tmp := new(T)
err = dec.Decode(tmp)
if err != nil {
t.Fatal(err)
}
if *tmp != tv {
t.Fatal("Values don't match")
}
}
示例8: TestGetterErrors
func (s *S) TestGetterErrors(c *C) {
e := errors.New("oops")
obj1 := &docWithGetterField{}
obj1.Field = &typeWithGetter{sampleItems[0].obj, e}
data, err := bson.Marshal(obj1)
c.Assert(err, ErrorMatches, "oops")
c.Assert(data, IsNil)
obj2 := &typeWithGetter{sampleItems[0].obj, e}
data, err = bson.Marshal(obj2)
c.Assert(err, ErrorMatches, "oops")
c.Assert(data, IsNil)
}
示例9: fnBsonEncodeFn
func fnBsonEncodeFn(buf *bytes.Buffer, ts *TestStruc) error {
bs, err := bson.Marshal(ts)
if err == nil {
buf.Write(bs)
}
return err
}
示例10: Save
func (node *Node) Save() (err error) {
db, err := DBConnect()
if err != nil {
return
}
defer db.Close()
node.UpdateVersion()
if len(node.Revisions) == 0 || node.Revisions[len(node.Revisions)-1].Version != node.Version {
n := Node{node.Id, node.Version, node.File, node.Attributes, node.Indexes, node.Acl, node.VersionParts, node.Type, nil, node.Relatives}
node.Revisions = append(node.Revisions, n)
}
bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
os.Remove(bsonPath)
nbson, err := bson.Marshal(node)
if err != nil {
return
}
err = ioutil.WriteFile(bsonPath, nbson, 0644)
if err != nil {
return
}
err = db.Upsert(node)
if err != nil {
return
}
return
}
示例11: sendToInstance
// ServiceClient.sendToInstance() tries to make an RPC request on a particular connection to an instance
func (c *ServiceClient) sendToInstance(sr ServiceResource, requestInfo *skynet.RequestInfo, funcName string, in interface{}) (result []byte, serviceErr, err error) {
ts("sendToInstance", requestInfo)
defer te("sendToInstance", requestInfo)
sin := skynet.ServiceRPCIn{
RequestInfo: requestInfo,
Method: funcName,
ClientID: sr.clientID,
}
sin.In, err = bson.Marshal(in)
if err != nil {
return
}
sout := skynet.ServiceRPCOut{}
err = sr.rpcClient.Call(sr.service.Config.Name+".Forward", sin, &sout)
if err != nil {
sr.Close()
dbg("(sr.rpcClient.Call)", err)
c.Log.Item(err)
}
if sout.ErrString != "" {
serviceErr = serviceError{sout.ErrString}
}
result = sout.Out
return
}
示例12: TestMarshalErrorItems
func (s *S) TestMarshalErrorItems(c *C) {
for _, item := range marshalErrorItems {
data, err := bson.Marshal(item.obj)
c.Assert(err, ErrorMatches, item.data)
c.Assert(data, IsNil)
}
}
示例13: Save
func (node *Node) Save() (err error) {
node.UpdateVersion()
if len(node.Revisions) == 0 || node.Revisions[len(node.Revisions)-1].Version != node.Version {
n := Node{node.Id, node.Version, node.File, node.Attributes, node.Public, node.Indexes, node.Acl, node.VersionParts, node.Tags, nil, node.Linkages, node.CreatedOn, node.LastModified}
node.Revisions = append(node.Revisions, n)
}
if node.CreatedOn == "" {
node.CreatedOn = time.Now().Format(time.UnixDate)
} else {
node.LastModified = time.Now().Format(time.UnixDate)
}
bsonPath := fmt.Sprintf("%s/%s.bson", node.Path(), node.Id)
os.Remove(bsonPath)
nbson, err := bson.Marshal(node)
if err != nil {
return
}
err = ioutil.WriteFile(bsonPath, nbson, 0644)
if err != nil {
return
}
err = dbUpsert(node)
if err != nil {
return
}
return
}
示例14: BenchmarkBsonUnmarshal
func BenchmarkBsonUnmarshal(b *testing.B) {
popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano()))
total := 0
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.StopTimer()
p := NewPopulatedPreAccept(popr)
data, err := bson.Marshal(p)
if err != nil {
panic(err)
}
total += len(data)
msg := &PreAccept{}
b.StartTimer()
if err := bson.Unmarshal(data, msg); err != nil {
panic(err)
}
}
b.SetBytes(int64(total / b.N))
}
示例15: MarshalAndPack
// Marshal and pack data into array of bytes for sending.
func (_ *BSONProtocol) MarshalAndPack(name string, structPtr interface{}) ([]byte, error) {
outMsg := &OutgoingBSONMessage{
Event: name,
Data: structPtr,
}
return bson.Marshal(outMsg)
}