本文整理汇总了Golang中gopkg/in/mgo/v2/bson.Raw.Data方法的典型用法代码示例。如果您正苦于以下问题:Golang Raw.Data方法的具体用法?Golang Raw.Data怎么用?Golang Raw.Data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gopkg/in/mgo/v2/bson.Raw
的用法示例。
在下文中一共展示了Raw.Data方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Debug
func (bd *BSONDump) Debug() error {
stream, err := bd.init()
if err != nil {
return err
}
defer stream.Close()
reusableBuf := make([]byte, db.MaxBSONSize)
var result bson.Raw
for {
hasDoc, docSize := stream.LoadNextInto(reusableBuf)
if !hasDoc {
break
}
result.Kind = reusableBuf[0]
result.Data = reusableBuf[0:docSize]
err = DebugBSON(result, 0, os.Stdout)
if err != nil {
return err
}
}
if err := stream.Err(); err != nil {
return err
}
return nil
}
示例2: Debug
// Debug iterates through the BSON file and for each document it finds,
// recursively descends into objects and arrays and prints a human readable
// BSON representation containing the type and size of each field.
// It returns the number of documents processed and a non-nil error if one is
// encountered before the end of the file is reached.
func (bd *BSONDump) Debug() (int, error) {
numFound := 0
if bd.bsonSource == nil {
panic("Tried to call Debug() before opening file")
}
defer bd.bsonSource.Close()
reusableBuf := make([]byte, db.MaxBSONSize)
var result bson.Raw
for {
hasDoc, docSize := bd.bsonSource.LoadNextInto(reusableBuf)
if !hasDoc {
break
}
result.Data = reusableBuf[0:docSize]
if bd.BSONDumpOptions.ObjCheck {
validated := bson.M{}
err := bson.Unmarshal(result.Data, &validated)
if err != nil {
// ObjCheck is turned on and we hit an error, so short-circuit now.
return numFound, fmt.Errorf("failed to validate bson during objcheck: %v", err)
}
}
err := printBSON(result, 0, bd.Out)
if err != nil {
log.Logf(log.Always, "encountered error debugging BSON data: %v", err)
}
numFound++
}
if err := bd.bsonSource.Err(); err != nil {
// This error indicates the BSON document header is corrupted;
// either the 4-byte header couldn't be read in full, or
// the size in the header would require reading more bytes
// than the file has left
return numFound, err
}
return numFound, nil
}