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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 bsoncore
  7. import (
  8. "bytes"
  9. "fmt"
  10. "go.mongodb.org/mongo-driver/bson/bsontype"
  11. )
  12. // MalformedElementError represents a class of errors that RawElement methods return.
  13. type MalformedElementError string
  14. func (mee MalformedElementError) Error() string { return string(mee) }
  15. // ErrElementMissingKey is returned when a RawElement is missing a key.
  16. const ErrElementMissingKey MalformedElementError = "element is missing key"
  17. // ErrElementMissingType is returned when a RawElement is missing a type.
  18. const ErrElementMissingType MalformedElementError = "element is missing type"
  19. // Element is a raw bytes representation of a BSON element.
  20. type Element []byte
  21. // Key returns the key for this element. If the element is not valid, this method returns an empty
  22. // string. If knowing if the element is valid is important, use KeyErr.
  23. func (e Element) Key() string {
  24. key, _ := e.KeyErr()
  25. return key
  26. }
  27. // KeyBytes returns the key for this element as a []byte. If the element is not valid, this method
  28. // returns an empty string. If knowing if the element is valid is important, use KeyErr. This method
  29. // will not include the null byte at the end of the key in the slice of bytes.
  30. func (e Element) KeyBytes() []byte {
  31. key, _ := e.KeyBytesErr()
  32. return key
  33. }
  34. // KeyErr returns the key for this element, returning an error if the element is not valid.
  35. func (e Element) KeyErr() (string, error) {
  36. key, err := e.KeyBytesErr()
  37. return string(key), err
  38. }
  39. // KeyBytesErr returns the key for this element as a []byte, returning an error if the element is
  40. // not valid.
  41. func (e Element) KeyBytesErr() ([]byte, error) {
  42. if len(e) <= 0 {
  43. return nil, ErrElementMissingType
  44. }
  45. idx := bytes.IndexByte(e[1:], 0x00)
  46. if idx == -1 {
  47. return nil, ErrElementMissingKey
  48. }
  49. return e[1 : idx+1], nil
  50. }
  51. // Validate ensures the element is a valid BSON element.
  52. func (e Element) Validate() error {
  53. if len(e) < 1 {
  54. return ErrElementMissingType
  55. }
  56. idx := bytes.IndexByte(e[1:], 0x00)
  57. if idx == -1 {
  58. return ErrElementMissingKey
  59. }
  60. return Value{Type: bsontype.Type(e[0]), Data: e[idx+2:]}.Validate()
  61. }
  62. // CompareKey will compare this element's key to key. This method makes it easy to compare keys
  63. // without needing to allocate a string. The key may be null terminated. If a valid key cannot be
  64. // read this method will return false.
  65. func (e Element) CompareKey(key []byte) bool {
  66. if len(e) < 2 {
  67. return false
  68. }
  69. idx := bytes.IndexByte(e[1:], 0x00)
  70. if idx == -1 {
  71. return false
  72. }
  73. if index := bytes.IndexByte(key, 0x00); index > -1 {
  74. key = key[:index]
  75. }
  76. return bytes.Equal(e[1:idx+1], key)
  77. }
  78. // Value returns the value of this element. If the element is not valid, this method returns an
  79. // empty Value. If knowing if the element is valid is important, use ValueErr.
  80. func (e Element) Value() Value {
  81. val, _ := e.ValueErr()
  82. return val
  83. }
  84. // ValueErr returns the value for this element, returning an error if the element is not valid.
  85. func (e Element) ValueErr() (Value, error) {
  86. if len(e) <= 0 {
  87. return Value{}, ErrElementMissingType
  88. }
  89. idx := bytes.IndexByte(e[1:], 0x00)
  90. if idx == -1 {
  91. return Value{}, ErrElementMissingKey
  92. }
  93. val, rem, exists := ReadValue(e[idx+2:], bsontype.Type(e[0]))
  94. if !exists {
  95. return Value{}, NewInsufficientBytesError(e, rem)
  96. }
  97. return val, nil
  98. }
  99. // String implements the fmt.String interface. The output will be in extended JSON format.
  100. func (e Element) String() string {
  101. if len(e) <= 0 {
  102. return ""
  103. }
  104. t := bsontype.Type(e[0])
  105. idx := bytes.IndexByte(e[1:], 0x00)
  106. if idx == -1 {
  107. return ""
  108. }
  109. key, valBytes := []byte(e[1:idx+1]), []byte(e[idx+2:])
  110. val, _, valid := ReadValue(valBytes, t)
  111. if !valid {
  112. return ""
  113. }
  114. return fmt.Sprintf(`"%s": %v`, key, val)
  115. }
  116. // DebugString outputs a human readable version of RawElement. It will attempt to stringify the
  117. // valid components of the element even if the entire element is not valid.
  118. func (e Element) DebugString() string {
  119. if len(e) <= 0 {
  120. return "<malformed>"
  121. }
  122. t := bsontype.Type(e[0])
  123. idx := bytes.IndexByte(e[1:], 0x00)
  124. if idx == -1 {
  125. return fmt.Sprintf(`bson.Element{[%s]<malformed>}`, t)
  126. }
  127. key, valBytes := []byte(e[1:idx+1]), []byte(e[idx+2:])
  128. val, _, valid := ReadValue(valBytes, t)
  129. if !valid {
  130. return fmt.Sprintf(`bson.Element{[%s]"%s": <malformed>}`, t, key)
  131. }
  132. return fmt.Sprintf(`bson.Element{[%s]"%s": %v}`, t, key, val)
  133. }