本文整理匯總了Golang中github.com/mongodb/mongo-tools/common/json.Unmarshal函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unmarshal函數的具體用法?Golang Unmarshal怎麽用?Golang Unmarshal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unmarshal函數的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ParseReadPreference
func ParseReadPreference(rp string) (mgo.Mode, bson.D, error) {
var mode string
var tags bson.D
if rp == "" {
return mgo.Nearest, nil, nil
}
if rp[0] != '{' {
mode = rp
} else {
var doc readPrefDoc
err := json.Unmarshal([]byte(rp), &doc)
if err != nil {
return 0, nil, fmt.Errorf("invalid --ReadPreferences json object: %v", err)
}
tags = doc.Tags
mode = doc.Mode
}
switch mode {
case "primary":
return mgo.Primary, tags, nil
case "primaryPreferred":
return mgo.PrimaryPreferred, tags, nil
case "secondary":
return mgo.Secondary, tags, nil
case "secondaryPreferred":
return mgo.SecondaryPreferred, tags, nil
case "nearest":
return mgo.Nearest, tags, nil
}
return 0, nil, fmt.Errorf("invalid readPreference mode '%v'", mode)
}
示例2: TestKerberos
func TestKerberos(t *testing.T) {
testutil.VerifyTestType(t, testutil.KerberosTestType)
Convey("Should be able to run mongoexport with Kerberos auth", t, func() {
opts, err := testutil.GetKerberosOptions()
So(err, ShouldBeNil)
sessionProvider, err := db.NewSessionProvider(*opts)
So(err, ShouldBeNil)
export := MongoExport{
ToolOptions: *opts,
OutputOpts: &OutputFormatOptions{},
InputOpts: &InputOptions{},
SessionProvider: sessionProvider,
}
var out bytes.Buffer
num, err := export.exportInternal(&out)
So(err, ShouldBeNil)
So(num, ShouldEqual, 1)
outputLines := strings.Split(strings.TrimSpace(out.String()), "\n")
So(len(outputLines), ShouldEqual, 1)
outMap := map[string]interface{}{}
So(json.Unmarshal([]byte(outputLines[0]), &outMap), ShouldBeNil)
So(outMap["kerberos"], ShouldEqual, true)
So(outMap["authenticated"], ShouldEqual, "yeah")
So(outMap["_id"].(map[string]interface{})["$oid"], ShouldEqual, "528fb35afb3a8030e2f643c3")
})
}
示例3: TestJSONArray
func TestJSONArray(t *testing.T) {
testutil.VerifyTestType(t, testutil.UnitTestType)
Convey("With a JSON export output in array mode", t, func() {
out := &bytes.Buffer{}
Convey("exporting a bunch of documents should produce valid json", func() {
jsonExporter := NewJSONExportOutput(true, false, out)
err := jsonExporter.WriteHeader()
So(err, ShouldBeNil)
// Export a few docs of various types
testObjs := []interface{}{bson.NewObjectId(), "asd", 12345, 3.14159, bson.M{"A": 1}}
for _, obj := range testObjs {
err = jsonExporter.ExportDocument(bson.M{"_id": obj})
So(err, ShouldBeNil)
}
err = jsonExporter.WriteFooter()
So(err, ShouldBeNil)
// Unmarshal the whole thing, it should be valid json
fromJSON := []map[string]interface{}{}
err = json.Unmarshal(out.Bytes(), &fromJSON)
So(err, ShouldBeNil)
So(len(fromJSON), ShouldEqual, len(testObjs))
})
Reset(func() {
out.Reset()
})
})
}
示例4: MetadataFromJSON
// MetadataFromJSON takes a slice of JSON bytes and unmarshals them into usable
// collection options and indexes for restoring collections.
func (restore *MongoRestore) MetadataFromJSON(jsonBytes []byte) (bson.D, []IndexDocument, error) {
if len(jsonBytes) == 0 {
// skip metadata parsing if the file is empty
return nil, nil, nil
}
meta := &Metadata{}
err := json.Unmarshal(jsonBytes, meta)
if err != nil {
return nil, nil, err
}
// first get the ordered key information for each index,
// then merge it with a set of options stored as a map
metaAsMap := metaDataMapIndex{}
err = json.Unmarshal(jsonBytes, &metaAsMap)
if err != nil {
return nil, nil, fmt.Errorf("error unmarshalling metadata as map: %v", err)
}
for i := range meta.Indexes {
// remove "key" from the map so we can decode it properly later
delete(metaAsMap.Indexes[i], "key")
// parse extra index fields
meta.Indexes[i].Options = metaAsMap.Indexes[i]
if err := bsonutil.ConvertJSONDocumentToBSON(meta.Indexes[i].Options); err != nil {
return nil, nil, fmt.Errorf("extended json error: %v", err)
}
// parse the values of the index keys, so we can support extended json
for pos, field := range meta.Indexes[i].Key {
meta.Indexes[i].Key[pos].Value, err = bsonutil.ParseJSONValue(field.Value)
if err != nil {
return nil, nil, fmt.Errorf("extended json in '%v' field: %v", field.Name, err)
}
}
}
// parse the values of options fields, to support extended json
meta.Options, err = bsonutil.GetExtendedBsonD(meta.Options)
if err != nil {
return nil, nil, fmt.Errorf("extended json in 'options': %v", err)
}
return meta.Options, meta.Indexes, nil
}
示例5: Dump
// Dump handles some final options checking and executes MongoDump
func (dump *MongoDump) Dump() error {
err := dump.ValidateOptions()
if err != nil {
return fmt.Errorf("Bad Option: %v", err)
}
if dump.InputOptions.Query != "" {
// TODO, check for extended json support...
// gonna need to do some exploring later on, since im 95% sure
// this is undefined in the current tools
err = json.Unmarshal([]byte(dump.InputOptions.Query), &dump.query)
if err != nil {
return fmt.Errorf("error parsing query: %v", err)
}
}
if dump.OutputOptions.Out == "-" {
dump.useStdout = true
}
if dump.OutputOptions.DumpDBUsersAndRoles {
//first make sure this is possible with the connected database
dump.authVersion, err = auth.GetAuthVersion(dump.SessionProvider.GetSession())
if err != nil {
return fmt.Errorf("error getting auth schema version for dumpDbUsersAndRoles: %v", err)
}
log.Logf(2, "using auth schema version %v", dump.authVersion)
if dump.authVersion != 3 {
return fmt.Errorf("backing up users and roles is only supported for "+
"deployments with auth schema versions 3, found: %v", dump.authVersion)
}
}
//switch on what kind of execution to do
switch {
case dump.ToolOptions.DB == "" && dump.ToolOptions.Collection == "":
err = dump.DumpEverything()
case dump.ToolOptions.DB != "" && dump.ToolOptions.Collection == "":
err = dump.DumpDatabase(dump.ToolOptions.DB)
case dump.ToolOptions.DB != "" && dump.ToolOptions.Collection != "":
err = dump.DumpCollection(dump.ToolOptions.DB, dump.ToolOptions.Collection)
}
if dump.OutputOptions.DumpDBUsersAndRoles {
log.Logf(0, "dumping users and roles for %v", dump.ToolOptions.DB)
if dump.ToolOptions.DB == "admin" {
log.Logf(0, "skipping users/roles dump, already dumped admin database")
} else {
err = dump.DumpUsersAndRolesForDB(dump.ToolOptions.DB)
if err != nil {
return fmt.Errorf("error dumping users and roles: %v", err)
}
}
}
log.Logf(1, "done")
return err
}
示例6: getSortFromArg
//getSortFromArg takes a sort specification in JSON and returns it as a bson.D
//object which preserves the ordering of the keys as they appear in the input.
func getSortFromArg(queryRaw string) (bson.D, error) {
parsedJSON := bson.D{}
err := sloppyjson.Unmarshal([]byte(queryRaw), &parsedJSON)
if err != nil {
return nil, fmt.Errorf("Query is not valid JSON: %v", err)
}
return parsedJSON, nil
}
示例7: getSortFromArg
// getSortFromArg takes a sort specification in JSON and returns it as a bson.D
// object which preserves the ordering of the keys as they appear in the input.
func getSortFromArg(queryRaw string) (bson.D, error) {
parsedJSON := bson.D{}
err := json.Unmarshal([]byte(queryRaw), &parsedJSON)
if err != nil {
return nil, fmt.Errorf("query '%v' is not valid JSON: %v", queryRaw, err)
}
// TODO: verify sort specification before returning a nil error
return parsedJSON, nil
}
示例8: parseJson
// Convert a json string to a raw document
func parseJson(jsonText string) (Document, error) {
rawObj := Document{}
err := json.Unmarshal([]byte(jsonText), &rawObj)
if err != nil {
return rawObj, err
}
err = normalizeObj(rawObj)
return rawObj, err
}
示例9: getObjectFromByteArg
// getObjectFromByteArg takes an object in extended JSON, and converts it to an object that
// can be passed straight to db.collection.find(...) as a query or sort critera.
// Returns an error if the string is not valid JSON, or extended JSON.
func getObjectFromByteArg(queryRaw []byte) (map[string]interface{}, error) {
parsedJSON := map[string]interface{}{}
err := json.Unmarshal(queryRaw, &parsedJSON)
if err != nil {
return nil, fmt.Errorf("query '%v' is not valid JSON: %v", queryRaw, err)
}
err = bsonutil.ConvertJSONDocumentToBSON(parsedJSON)
if err != nil {
return nil, err
}
return parsedJSON, nil
}
示例10: parseID
// parse and convert extended JSON
func (mf *MongoFiles) parseID() (interface{}, error) {
// parse the id using extended json
var asJSON interface{}
err := json.Unmarshal([]byte(mf.FileName), &asJSON)
if err != nil {
return nil, fmt.Errorf(
"error parsing _id as json: %v; make sure you are properly escaping input", err)
}
id, err := bsonutil.ConvertJSONValueToBSON(asJSON)
if err != nil {
return nil, fmt.Errorf("error converting _id to bson: %v", err)
}
return id, nil
}
示例11: getObjectFromArg
//getObjectFromArg takes an object in extended JSON, and converts it to an object that
//can be passed straight to db.collection.find(...) as a query or sort critera.
//Returns an error if the string is not valid JSON, or extended JSON.
func getObjectFromArg(queryRaw string) (map[string]interface{}, error) {
parsedJSON := map[string]interface{}{}
err := sloppyjson.Unmarshal([]byte(queryRaw), &parsedJSON)
if err != nil {
return nil, fmt.Errorf("Query is not valid JSON: %v", err)
}
for key, val := range parsedJSON {
if valSubDoc, ok := val.(map[string]interface{}); ok {
newVal, err := bsonutil.ParseSpecialKeys(valSubDoc)
if err != nil {
return nil, fmt.Errorf("Error in query: %v", err)
}
parsedJSON[key] = newVal
}
}
return parsedJSON, nil
}
示例12: TestMongoDumpMetaData
func TestMongoDumpMetaData(t *testing.T) {
testutil.VerifyTestType(t, testutil.IntegrationTestType)
log.SetWriter(ioutil.Discard)
Convey("With a MongoDump instance", t, func() {
err := setUpMongoDumpTestData()
So(err, ShouldBeNil)
Convey("testing that the dumped directory contains information about indexes", func() {
md := simpleMongoDumpInstance()
md.OutputOptions.Out = "dump"
err = md.Init()
So(err, ShouldBeNil)
err = md.Dump()
So(err, ShouldBeNil)
path, err := os.Getwd()
So(err, ShouldBeNil)
dumpDir := util.ToUniversalPath(filepath.Join(path, "dump"))
dumpDBDir := util.ToUniversalPath(filepath.Join(dumpDir, testDB))
So(fileDirExists(dumpDir), ShouldBeTrue)
So(fileDirExists(dumpDBDir), ShouldBeTrue)
Convey("having one metadata file per collection", func() {
c1, err := countNonIndexBSONFiles(dumpDBDir)
So(err, ShouldBeNil)
c2, err := countMetaDataFiles(dumpDBDir)
So(err, ShouldBeNil)
So(c1, ShouldEqual, c2)
Convey("and that the JSON in a metadata file is valid", func() {
metaFiles, err := getMatchingFiles(dumpDBDir, ".*\\.metadata\\.json")
So(err, ShouldBeNil)
So(len(metaFiles), ShouldBeGreaterThan, 0)
oneMetaFile, err := os.Open(util.ToUniversalPath(filepath.Join(dumpDBDir, metaFiles[0])))
So(err, ShouldBeNil)
contents, err := ioutil.ReadAll(oneMetaFile)
var jsonResult map[string]interface{}
err = json.Unmarshal(contents, &jsonResult)
So(err, ShouldBeNil)
Convey("and contains an 'indexes' key", func() {
_, ok := jsonResult["indexes"]
So(ok, ShouldBeTrue)
So(oneMetaFile.Close(), ShouldBeNil)
})
})
})
Reset(func() {
So(os.RemoveAll(dumpDir), ShouldBeNil)
})
})
Reset(func() {
So(tearDownMongoDumpTestData(), ShouldBeNil)
})
})
}
示例13: constructWCObject
// constructWCObject takes in a write concern and attempts to construct an
// mgo.Safe object from it. It returns an error if it is unable to parse the
// string or if a parsed write concern field value is invalid.
func constructWCObject(writeConcern string) (sessionSafety *mgo.Safe, err error) {
sessionSafety = &mgo.Safe{}
defer func() {
// If the user passes a w value of 0, we set the session to use the
// unacknowledged write concern but only if journal commit acknowledgment,
// is not required. If commit acknowledgment is required, it prevails,
// and the server will require that mongod acknowledge the write operation
if sessionSafety.WMode == "" && sessionSafety.W == 0 && !sessionSafety.J {
sessionSafety = nil
}
}()
jsonWriteConcern := map[string]interface{}{}
if err = json.Unmarshal([]byte(writeConcern), &jsonWriteConcern); err != nil {
// if the writeConcern string can not be unmarshaled into JSON, this
// allows a default to the old behavior wherein the entire argument
// passed in is assigned to the 'w' field - thus allowing users pass
// a write concern that looks like: "majority", 0, "4", etc.
wValue, err := strconv.Atoi(writeConcern)
if err != nil {
sessionSafety.WMode = writeConcern
} else {
sessionSafety.W = wValue
if wValue < 0 {
return sessionSafety, fmt.Errorf("invalid '%v' argument: %v", w, wValue)
}
}
return sessionSafety, nil
}
if jVal, ok := jsonWriteConcern[j]; ok && util.IsTruthy(jVal) {
sessionSafety.J = true
}
if fsyncVal, ok := jsonWriteConcern[fSync]; ok && util.IsTruthy(fsyncVal) {
sessionSafety.FSync = true
}
if wtimeout, ok := jsonWriteConcern[wTimeout]; ok {
wtimeoutValue, err := util.ToInt(wtimeout)
if err != nil {
return sessionSafety, fmt.Errorf("invalid '%v' argument: %v", wTimeout, wtimeout)
}
sessionSafety.WTimeout = wtimeoutValue
}
if wInterface, ok := jsonWriteConcern[w]; ok {
wValue, err := util.ToInt(wInterface)
if err != nil {
// if the argument is neither a string nor int, error out
wStrVal, ok := wInterface.(string)
if !ok {
return sessionSafety, fmt.Errorf("invalid '%v' argument: %v", w, wInterface)
}
sessionSafety.WMode = wStrVal
} else {
sessionSafety.W = wValue
if wValue < 0 {
return sessionSafety, fmt.Errorf("invalid '%v' argument: %v", w, wValue)
}
}
}
return sessionSafety, nil
}