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.

bson_1_8.go 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // +build !go1.9
  7. package bson // import "go.mongodb.org/mongo-driver/bson"
  8. import (
  9. "math"
  10. "strconv"
  11. "strings"
  12. )
  13. // Zeroer allows custom struct types to implement a report of zero
  14. // state. All struct types that don't implement Zeroer or where IsZero
  15. // returns false are considered to be not zero.
  16. type Zeroer interface {
  17. IsZero() bool
  18. }
  19. // D represents a BSON Document. This type can be used to represent BSON in a concise and readable
  20. // manner. It should generally be used when serializing to BSON. For deserializing, the Raw or
  21. // Document types should be used.
  22. //
  23. // Example usage:
  24. //
  25. // primitive.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  26. //
  27. // This type should be used in situations where order matters, such as MongoDB commands. If the
  28. // order is not important, a map is more comfortable and concise.
  29. type D []E
  30. // Map creates a map from the elements of the D.
  31. func (d D) Map() M {
  32. m := make(M, len(d))
  33. for _, e := range d {
  34. m[e.Key] = e.Value
  35. }
  36. return m
  37. }
  38. // E represents a BSON element for a D. It is usually used inside a D.
  39. type E struct {
  40. Key string
  41. Value interface{}
  42. }
  43. // M is an unordered, concise representation of a BSON Document. It should generally be used to
  44. // serialize BSON when the order of the elements of a BSON document do not matter. If the element
  45. // order matters, use a D instead.
  46. //
  47. // Example usage:
  48. //
  49. // primitive.M{"foo": "bar", "hello": "world", "pi": 3.14159}
  50. //
  51. // This type is handled in the encoders as a regular map[string]interface{}. The elements will be
  52. // serialized in an undefined, random order, and the order will be different each time.
  53. type M map[string]interface{}
  54. // An A represents a BSON array. This type can be used to represent a BSON array in a concise and
  55. // readable manner. It should generally be used when serializing to BSON. For deserializing, the
  56. // RawArray or Array types should be used.
  57. //
  58. // Example usage:
  59. //
  60. // primitive.A{"bar", "world", 3.14159, primitive.D{{"qux", 12345}}}
  61. //
  62. type A []interface{}
  63. func formatDouble(f float64) string {
  64. var s string
  65. if math.IsInf(f, 1) {
  66. s = "Infinity"
  67. } else if math.IsInf(f, -1) {
  68. s = "-Infinity"
  69. } else if math.IsNaN(f) {
  70. s = "NaN"
  71. } else {
  72. // Print exactly one decimalType place for integers; otherwise, print as many are necessary to
  73. // perfectly represent it.
  74. s = strconv.FormatFloat(f, 'G', -1, 64)
  75. if !strings.ContainsRune(s, '.') {
  76. s += ".0"
  77. }
  78. }
  79. return s
  80. }