Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | // not use this file except in compliance with the License. You may obtain |
| 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | |
| 7 | package bsonx |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson/bsontype" |
| 13 | ) |
| 14 | |
| 15 | const validateMaxDepthDefault = 2048 |
| 16 | |
| 17 | // ElementTypeError specifies that a method to obtain a BSON value an incorrect type was called on a bson.Value. |
| 18 | // |
| 19 | // TODO: rename this ValueTypeError. |
| 20 | type ElementTypeError struct { |
| 21 | Method string |
| 22 | Type bsontype.Type |
| 23 | } |
| 24 | |
| 25 | // Error implements the error interface. |
| 26 | func (ete ElementTypeError) Error() string { |
| 27 | return "Call of " + ete.Method + " on " + ete.Type.String() + " type" |
| 28 | } |
| 29 | |
| 30 | // Elem represents a BSON element. |
| 31 | // |
| 32 | // NOTE: Element cannot be the value of a map nor a property of a struct without special handling. |
| 33 | // The default encoders and decoders will not process Element correctly. To do so would require |
| 34 | // information loss since an Element contains a key, but the keys used when encoding a struct are |
| 35 | // the struct field names. Instead of using an Element, use a Value as a value in a map or a |
| 36 | // property of a struct. |
| 37 | type Elem struct { |
| 38 | Key string |
| 39 | Value Val |
| 40 | } |
| 41 | |
| 42 | // Equal compares e and e2 and returns true if they are equal. |
| 43 | func (e Elem) Equal(e2 Elem) bool { |
| 44 | if e.Key != e2.Key { |
| 45 | return false |
| 46 | } |
| 47 | return e.Value.Equal(e2.Value) |
| 48 | } |
| 49 | |
| 50 | func (e Elem) String() string { |
| 51 | // TODO(GODRIVER-612): When bsoncore has appenders for extended JSON use that here. |
| 52 | return fmt.Sprintf(`bson.Element{"%s": %v}`, e.Key, e.Value) |
| 53 | } |