You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

element.go 1.6 kB

4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. package bsonx
  7. import (
  8. "fmt"
  9. "go.mongodb.org/mongo-driver/bson/bsontype"
  10. )
  11. const validateMaxDepthDefault = 2048
  12. // ElementTypeError specifies that a method to obtain a BSON value an incorrect type was called on a bson.Value.
  13. //
  14. // TODO: rename this ValueTypeError.
  15. type ElementTypeError struct {
  16. Method string
  17. Type bsontype.Type
  18. }
  19. // Error implements the error interface.
  20. func (ete ElementTypeError) Error() string {
  21. return "Call of " + ete.Method + " on " + ete.Type.String() + " type"
  22. }
  23. // Elem represents a BSON element.
  24. //
  25. // NOTE: Element cannot be the value of a map nor a property of a struct without special handling.
  26. // The default encoders and decoders will not process Element correctly. To do so would require
  27. // information loss since an Element contains a key, but the keys used when encoding a struct are
  28. // the struct field names. Instead of using an Element, use a Value as a value in a map or a
  29. // property of a struct.
  30. type Elem struct {
  31. Key string
  32. Value Val
  33. }
  34. // Equal compares e and e2 and returns true if they are equal.
  35. func (e Elem) Equal(e2 Elem) bool {
  36. if e.Key != e2.Key {
  37. return false
  38. }
  39. return e.Value.Equal(e2.Value)
  40. }
  41. func (e Elem) String() string {
  42. // TODO(GODRIVER-612): When bsoncore has appenders for extended JSON use that here.
  43. return fmt.Sprintf(`bson.Element{"%s": %v}`, e.Key, e.Value)
  44. }