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

mendersoftware / useradm / 1565757771

29 Nov 2024 07:56AM UTC coverage: 87.019%. Remained the same
1565757771

push

gitlab-ci

web-flow
Merge pull request #434 from alfrunes/1.22.x

chore(deps): Upgrade golang to latest

2869 of 3297 relevant lines covered (87.02%)

131.0 hits per line

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

95.16
/model/settings.go
1
// Copyright 2022 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 model
16

17
import (
18
        "encoding/json"
19

20
        validation "github.com/go-ozzo/ozzo-validation/v4"
21
        "go.mongodb.org/mongo-driver/bson"
22
)
23

24
const (
25
        settingsID       = "_id"
26
        settingsETag     = "etag"
27
        settingsUserID   = "user_id"
28
        settingsTenantID = "tenant_id"
29

30
        maxSettings = 1024
31
)
32

33
type SettingsValues map[string]interface{}
34

35
type Settings struct {
36
        ID     string         `json:"id"`
37
        ETag   string         `json:"etag"`
38
        UserID string         `json:"-"`
39
        Values SettingsValues `json:"-"`
40
}
41

42
func (s Settings) MarshalJSON() ([]byte, error) {
8✔
43
        return json.Marshal(s.Values)
8✔
44
}
8✔
45

46
func (s *Settings) UnmarshalJSON(b []byte) error {
10✔
47
        return json.Unmarshal(b, &s.Values)
10✔
48
}
10✔
49

50
func (s Settings) MarshalBSON() ([]byte, error) {
7✔
51
        value := map[string]interface{}{}
7✔
52
        for k, v := range s.Values {
15✔
53
                value[k] = v
8✔
54
        }
8✔
55
        if s.ID != "" {
8✔
56
                value[settingsID] = s.ID
1✔
57
        }
1✔
58
        value[settingsETag] = s.ETag
7✔
59
        if s.UserID != "" {
7✔
60
                value[settingsUserID] = s.UserID
×
61
        }
×
62
        return bson.Marshal(value)
7✔
63
}
64

65
func (s *Settings) UnmarshalBSON(b []byte) error {
8✔
66
        value := map[string]interface{}{}
8✔
67
        err := bson.Unmarshal(b, &value)
8✔
68
        if val, ok := value[settingsID]; ok {
16✔
69
                if valString, ok := val.(string); ok {
9✔
70
                        s.ID = valString
1✔
71
                }
1✔
72
        }
73
        if val, ok := value[settingsETag]; ok {
16✔
74
                if valString, ok := val.(string); ok {
16✔
75
                        s.ETag = valString
8✔
76
                }
8✔
77
        }
78
        if err == nil {
16✔
79
                delete(value, settingsID)
8✔
80
                delete(value, settingsETag)
8✔
81
                delete(value, settingsUserID)
8✔
82
                delete(value, settingsTenantID)
8✔
83
                s.Values = value
8✔
84
        }
8✔
85
        return err
8✔
86
}
87

88
func ValidateKeys(value interface{}) error {
9✔
89
        s, _ := value.(SettingsValues)
9✔
90
        for k := range s {
18✔
91
                err := validation.Validate(k, lessThan128)
9✔
92
                if err != nil {
10✔
93
                        return err
1✔
94
                }
1✔
95
        }
96
        return nil
8✔
97
}
98

99
func lessThan4096Strings(value interface{}) error {
8✔
100
        if _, ok := value.(string); ok {
16✔
101
                return validation.Validate(value, lessThan4096)
8✔
102
        }
8✔
103
        return nil
×
104
}
105

106
func (s Settings) Validate() error {
10✔
107
        return validation.ValidateStruct(&s,
10✔
108
                validation.Field(&s.Values,
10✔
109
                        validation.Length(0, maxSettings),
10✔
110
                        validation.By(ValidateKeys),
10✔
111
                        validation.Each(
10✔
112
                                validation.By(lessThan4096Strings),
10✔
113
                        ),
10✔
114
                ),
10✔
115
        )
10✔
116
}
10✔
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