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 } }
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 } }
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 } }
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 } }
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 } }
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 } }
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") } }
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) }
func fnBsonEncodeFn(buf *bytes.Buffer, ts *TestStruc) error { bs, err := bson.Marshal(ts) if err == nil { buf.Write(bs) } return err }
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 }
// 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 }
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) } }
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 }
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)) }
// 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) }
// Bson to Json,InterfaceP{} by BsonBytes // failed func _Bson2Json(m *bson.M, i interface{}) error { b, err := bson.Marshal(m) if isError(err) { return err } return bson.Unmarshal(b, &i) }
// 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, err error) { if requestInfo == nil { requestInfo = &skynet.RequestInfo{ RequestID: skynet.UUID(), } } sin := service.ServiceRPCIn{ RequestInfo: requestInfo, Method: funcName, } sin.In, err = bson.Marshal(in) if err != nil { return } sout := service.ServiceRPCOut{} err = sr.rpcClient.Call(sr.service.Config.Name+".Forward", sin, &sout) if err != nil { sr.Close() c.Log.Item(err) } if sout.ErrString != "" { err = serviceError{sout.ErrString} } result = sout.Out return }
// 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) { sin := skynet.ServiceRPCIn{ RequestInfo: requestInfo, Method: funcName, ClientID: sr.clientID, } sin.In, err = bson.Marshal(in) if err != nil { err = fmt.Errorf("Error calling bson.Marshal: %v", err) return } sout := skynet.ServiceRPCOut{} err = sr.rpcClient.Call(sr.service.Name+".Forward", sin, &sout) if err != nil { sr.Close() // Log failure log.Printf(log.ERROR, "Error calling sr.rpcClient.Call: "+err.Error()) } if sout.ErrString != "" { serviceErr = serviceError{sout.ErrString} } result = sout.Out return }
func (s *URLSuite) TestBSON(c *C) { type doc struct { URL *charm.URL } url := charm.MustParseURL("cs:series/name") data, err := bson.Marshal(doc{url}) c.Assert(err, IsNil) var v doc err = bson.Unmarshal(data, &v) c.Assert(v.URL, DeepEquals, url) data, err = bson.Marshal(doc{}) c.Assert(err, IsNil) err = bson.Unmarshal(data, &v) c.Assert(v.URL, IsNil) }
func Encode(obj interface{}) (p *WirePkt, err error) { p = new(WirePkt) switch obj.(type) { case *IdentifyClient: p.Type = TypeIdentifyClient case *TaskRequest: p.Type = TypeTaskRequest case *TaskResponse: p.Type = TypeTaskResponse case *Acknowledgement: p.Type = TypeAcknowledgement default: Warn("Encoding unknown type!") return nil, ErrUnknownType } p.Payload, err = bson.Marshal(obj) if err != nil { return nil, err } if len(p.Payload) >= MaximumPayloadSize { return nil, ErrObjectTooLarge } p.Length = uint16(len(p.Payload)) return p, nil }
func (s *S) TestMarshalAllItems(c *C) { for i, item := range allItems { data, err := bson.Marshal(item.obj) c.Assert(err, IsNil) c.Assert(string(data), Equals, wrapInDoc(item.data), Commentf("Failed on item %d: %#v", i, item)) } }
func init() { log.SetFlags(log.Ltime | log.Lshortfile) thtw = *th.NewThriftTweet() // create structs, maps by using json data to do initial population json.Unmarshal([]byte(jsons), &tw) json.Unmarshal([]byte(jsons), &pbtw) json.Unmarshal([]byte(jsons), &twl) json.Unmarshal([]byte(jsons), &thtw) //log.Println("pbtw\n", pbtw) // create by values per serialization type bsonTweet, _ = bson.Marshal(&tw) jsonTweet = []byte(jsons) protoTw, _ = proto.Marshal(&pbtw) //ptw2, err := proto.Marshal(&pbtw) //log.Println(err) //log.Println(ptw2) msgpackTw, _ = msgpack.Marshal(tw) enc := gob.NewEncoder(&gobTw) err := enc.Encode(tw) if err != nil { panic("error") } //log.Println(string(gobTw.Bytes())) buf := thrift.NewTMemoryBuffer() thbp := thrift.NewTBinaryProtocol(buf, false, true) thtw.Write(thbp) thriftTw = buf.Bytes() tw2 := th.NewThriftTweet() tw2.Read(thbp) }
func (s *S) TestMarshalOneWayItems(c *C) { for _, item := range marshalItems { data, err := bson.Marshal(item.obj) c.Assert(err, IsNil) c.Assert(string(data), Equals, wrapInDoc(item.data)) } }
func TestIntegrationList_Pagination(t *testing.T) { storage, err := goejdb.Open("TestIntegrationList", goejdb.JBOWRITER|goejdb.JBOCREAT) if err != nil { t.Error(err) } defer storage.Del() defer os.Remove("TestIntegrationList") chatColl, err := storage.CreateColl("12345", nil) if err != nil { t.Error(err) } defer os.Remove("TestIntegrationList_12345") for i := 25; i > 0; i-- { bsonBytes, _ := bson.Marshal(quotes.Quote{When: i}) chatColl.SaveBson(bsonBytes) } manager := manageStorage{storage} res, err2 := manager.List("12345", 10) if err2 != nil { t.Error(err2) } assert.Equal(t, 10, len(*res)) }
func TestSend(t *testing.T) { client, server := net.Pipe() go doServiceHandshake(server, true, t) cn, err := NewConnectionFromNetConn("TestRPCService", client) c := cn.(*Conn) s := rpc.NewServer() var ts TestRPCService s.Register(&ts) go s.ServeCodec(bsonrpc.NewServerCodec(server)) var tp TestParam tp.Val1 = "Hello World" tp.Val2 = 10 ri := &skynet.RequestInfo{} ts.TestMethod = func(in skynet.ServiceRPCIn, out *skynet.ServiceRPCOut) (err error) { out.Out, err = bson.Marshal(&tp) var t TestParam if err != nil { return } if in.ClientID != c.clientID { return errors.New("Failed to set ClientID on request") } if in.Method != "Foo" { return errors.New("Failed to set Method on request") } if *in.RequestInfo != *ri { return errors.New("Failed to set RequestInfo on request") } err = bson.Unmarshal(in.In, &t) if err != nil { return } if t.Val1 != tp.Val1 || tp.Val2 != tp.Val2 { return errors.New("Request failed to send proper data") } return } err = c.Send(ri, "Foo", tp, &tp) if err != nil { t.Error(err) return } c.Close() server.Close() }
func TestDecode(t *testing.T) { req := rpc.Request{ ServiceMethod: "Foo.Bar", Seq: 3, } b, err := bson.Marshal(req) if err != nil { t.Fatal(err) } buf := bytes.NewBuffer(b) dec := NewDecoder(buf) r := new(rpc.Request) err = dec.Decode(r) if err != nil { t.Fatal(err) } if *r != req { t.Fatal("Values don't match") } }
func WriteFile(path string, obj interface{}) (err error) { out, err := bson.Marshal(obj) if err != nil { return err } return ioutil.WriteFile(path, out, os.FileMode(0777)) }
func (s *S) TestMarshalSampleItems(c *C) { for i, item := range sampleItems { data, err := bson.Marshal(item.obj) c.Assert(err, IsNil) c.Assert(string(data), Equals, item.data, Commentf("Failed on item %d", i)) } }
func (s *S) TestMarshalShortWithGetter(c *C) { obj := typeWithIntGetter{42} data, err := bson.Marshal(obj) c.Assert(err, IsNil) m := bson.M{} err = bson.Unmarshal(data, m) c.Assert(m["v"], Equals, 42) }
func (s *S) TestUnmarshalZeroesMap(c *C) { data, err := bson.Marshal(bson.M{"b": 2}) c.Assert(err, IsNil) m := bson.M{"a": 1} err = bson.Unmarshal(data, &m) c.Assert(err, IsNil) c.Assert(m, DeepEquals, bson.M{"b": 2}) }