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

mendersoftware / deviceconnect / 1376315854

12 Jul 2024 04:21PM UTC coverage: 76.156% (-0.2%) from 76.366%
1376315854

push

gitlab-ci

web-flow
Merge pull request #381 from mendersoftware/dependabot/go_modules/golang-dependencies-f8f4aaf149

chore: bump the golang-dependencies group with 4 updates

0 of 6 new or added lines in 2 files covered. (0.0%)

2 existing lines in 1 file now uncovered.

2453 of 3221 relevant lines covered (76.16%)

22.14 hits per line

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

88.89
/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
)
30

NEW
31
func newRegistry() *bsoncodec.Registry {
×
NEW
32
        reg := bson.NewRegistry()
×
UNCOV
33
        // Add UUID encoder/decoder for github.com/google/uuid.UUID
×
NEW
34
        reg.RegisterTypeEncoder(tUUID, bsoncodec.ValueEncoderFunc(uuidEncodeValue))
×
NEW
35
        reg.RegisterTypeDecoder(tUUID, bsoncodec.ValueDecoderFunc(uuidDecodeValue))
×
NEW
36
        return reg
×
UNCOV
37
}
×
38

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

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

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

79
                        fallthrough
1✔
80
                case bson.TypeBinaryUUID, bson.TypeBinaryUUIDOld:
3✔
81
                        copy(uid[:], data)
3✔
82

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

91
        case bson.TypeUndefined:
1✔
92
                err = r.ReadUndefined()
1✔
93

94
        case bson.TypeNull:
1✔
95
                err = r.ReadNull()
1✔
96

97
        default:
1✔
98
                err = errors.Errorf("cannot decode %v as a UUID", rType)
1✔
99
        }
100

101
        if err != nil {
9✔
102
                return err
2✔
103
        }
2✔
104
        val.Set(reflect.ValueOf(uid))
5✔
105
        return nil
5✔
106
}
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