• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / iot-manager / 1401702106

05 Aug 2024 08:32PM UTC coverage: 87.577%. Remained the same
1401702106

push

gitlab-ci

web-flow
Merge pull request #295 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3264 of 3727 relevant lines covered (87.58%)

11.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/store/mongo/bson.go
1
// Copyright 2024 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package mongo
16

17
import (
18
        "reflect"
19

20
        "github.com/google/uuid"
21
        "github.com/pkg/errors"
22
        "go.mongodb.org/mongo-driver/bson"
23
        "go.mongodb.org/mongo-driver/bson/bsoncodec"
24
        "go.mongodb.org/mongo-driver/bson/bsonrw"
25
)
26

27
var (
28
        tUUID = reflect.TypeOf(uuid.UUID{})
29
        tM    = reflect.TypeOf(bson.M{})
30
)
31

32
func newRegistry() *bsoncodec.Registry {
6✔
33
        reg := bson.NewRegistry()
6✔
34
        // Add UUID encoder/decoder for github.com/google/uuid.UUID
6✔
35
        reg.RegisterTypeEncoder(tUUID, bsoncodec.ValueEncoderFunc(uuidEncodeValue))
6✔
36
        reg.RegisterTypeDecoder(tUUID, bsoncodec.ValueDecoderFunc(uuidDecodeValue))
6✔
37
        reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, tM)
6✔
38
        return reg
6✔
39
}
6✔
40

41
func uuidEncodeValue(ec bsoncodec.EncodeContext, w bsonrw.ValueWriter, val reflect.Value) error {
130✔
42
        if !val.IsValid() || val.Type() != tUUID {
131✔
43
                return bsoncodec.ValueEncoderError{
1✔
44
                        Name:     "UUIDEncodeValue",
1✔
45
                        Types:    []reflect.Type{tUUID},
1✔
46
                        Received: val,
1✔
47
                }
1✔
48
        }
1✔
49
        uid := val.Interface().(uuid.UUID)
129✔
50
        return w.WriteBinaryWithSubtype(uid[:], byte(bson.TypeBinaryUUID))
129✔
51
}
52

53
func uuidDecodeValue(ec bsoncodec.DecodeContext, r bsonrw.ValueReader, val reflect.Value) error {
117✔
54
        if !val.CanSet() || val.Type() != tUUID {
118✔
55
                return bsoncodec.ValueDecoderError{
1✔
56
                        Name:     "UUIDDecodeValue",
1✔
57
                        Types:    []reflect.Type{tUUID},
1✔
58
                        Received: val,
1✔
59
                }
1✔
60
        }
1✔
61

62
        var (
116✔
63
                data    []byte
116✔
64
                err     error
116✔
65
                subtype byte
116✔
66
                uid     uuid.UUID = uuid.Nil
116✔
67
        )
116✔
68
        switch rType := r.Type(); rType {
116✔
69
        case bson.TypeBinary:
112✔
70
                data, subtype, err = r.ReadBinary()
112✔
71
                switch subtype {
112✔
72
                case bson.TypeBinaryGeneric:
2✔
73
                        if len(data) != 16 {
3✔
74
                                return errors.Errorf(
1✔
75
                                        "cannot decode %v as a UUID: "+
1✔
76
                                                "incorrect length: %d",
1✔
77
                                        data, len(data),
1✔
78
                                )
1✔
79
                        }
1✔
80

81
                        fallthrough
1✔
82
                case bson.TypeBinaryUUID, bson.TypeBinaryUUIDOld:
110✔
83
                        copy(uid[:], data)
110✔
84

85
                default:
1✔
86
                        err = errors.Errorf(
1✔
87
                                "cannot decode %v as a UUID: "+
1✔
88
                                        "incorrect subtype 0x%02x",
1✔
89
                                data, subtype,
1✔
90
                        )
1✔
91
                }
92

93
        case bson.TypeUndefined:
1✔
94
                err = r.ReadUndefined()
1✔
95

96
        case bson.TypeNull:
1✔
97
                err = r.ReadNull()
1✔
98

99
        default:
2✔
100
                err = errors.Errorf("cannot decode %v as a UUID", rType)
2✔
101
        }
102

103
        if err != nil {
118✔
104
                return err
3✔
105
        }
3✔
106
        val.Set(reflect.ValueOf(uid))
112✔
107
        return nil
112✔
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc