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