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

mendersoftware / mender-server / 1897784243

30 Jun 2025 11:50AM UTC coverage: 65.64% (-0.09%) from 65.731%
1897784243

Pull #769

gitlab-ci

alfrunes
chore(deviceauth): Add missing config env key replacer

The replacer did not exist because there were no prior configuration
settings with a period or dash in the key name.

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #769: MEN-7744: Rate limits for authenticated requests

181 of 332 new or added lines in 9 files covered. (54.52%)

1 existing line in 1 file now uncovered.

32539 of 49572 relevant lines covered (65.64%)

1.39 hits per line

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

72.09
/backend/pkg/config/unmarshal.go
1
// Copyright 2025 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 config
16

17
import (
18
        "encoding/json"
19
        "fmt"
20
        "io"
21
        "strings"
22
        "time"
23

24
        "github.com/go-viper/mapstructure/v2"
25
        "github.com/pkg/errors"
26
)
27

28
type Duration time.Duration
29

NEW
30
func (duration *Duration) UnmarshalText(b []byte) error {
×
NEW
31
        d, err := time.ParseDuration(string(b))
×
NEW
32
        if err != nil {
×
NEW
33
                return err
×
NEW
34
        }
×
NEW
35
        *duration = Duration(d)
×
NEW
36
        return nil
×
37
}
38

39
// UnmarshalSliceSetting will unmarshal an array of objects into the result T
40
// using either newline separated json objects for strings (from env) or
41
// an array of objects (using json struct tag) for any configuration source
42
// that is able to express a slice of objects.
43
func UnmarshalSliceSetting[T any](c Reader, path string, result *[]T) error {
1✔
44
        value := c.Get(path)
1✔
45
        var err error
1✔
46
        switch cfg := value.(type) {
1✔
47
        case string:
1✔
48
                decoder := json.NewDecoder(strings.NewReader(cfg))
1✔
49
                for {
2✔
50
                        var elem T
1✔
51
                        err = decoder.Decode(&elem)
1✔
52
                        if err != nil {
2✔
53
                                break
1✔
54
                        }
55
                        *result = append(*result, elem)
1✔
56
                }
57
                if errors.Is(err, io.EOF) {
2✔
58
                        err = nil
1✔
59
                }
1✔
60
        case []any:
1✔
61
                var decoder *mapstructure.Decoder
1✔
62
                decoder, err = mapstructure.NewDecoder(
1✔
63
                        &mapstructure.DecoderConfig{
1✔
64
                                TagName: "json",
1✔
65
                                Result:  &result,
1✔
66
                                Squash:  true,
1✔
67
                                DecodeHook: mapstructure.ComposeDecodeHookFunc(
1✔
68
                                        mapstructure.TextUnmarshallerHookFunc(),
1✔
69
                                        mapstructure.StringToTimeDurationHookFunc(),
1✔
70
                                ),
1✔
71
                        },
1✔
72
                )
1✔
73
                if err != nil {
1✔
NEW
74
                        return err
×
NEW
75
                }
×
76
                err = decoder.Decode(value)
1✔
NEW
77
        case nil:
×
78
                // pass (empty config)
NEW
79
        default:
×
NEW
80
                err = fmt.Errorf("invalid config type %T", cfg)
×
81
        }
82
        return err
1✔
83
}
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