This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
generated from yandex-praktikum/go-musthave-devops-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
95 lines (73 loc) · 2.38 KB
/
marshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package httpbackend
import (
"context"
"fmt"
"github.com/alkurbatov/metrics-collector/internal/entity"
"github.com/alkurbatov/metrics-collector/internal/security"
"github.com/alkurbatov/metrics-collector/internal/storage"
"github.com/alkurbatov/metrics-collector/internal/validators"
"github.com/alkurbatov/metrics-collector/pkg/metrics"
"github.com/rs/zerolog/log"
)
func toRecord(ctx context.Context, req *metrics.MetricReq, signer *security.Signer) (storage.Record, error) {
var record storage.Record
if err := validators.ValidateMetricName(req.ID, req.MType); err != nil {
return record, err
}
switch req.MType {
case metrics.KindCounter:
if req.Delta == nil {
return record, entity.ErrIncompleteRequest
}
record = storage.Record{Name: req.ID, Value: *req.Delta}
case metrics.KindGauge:
if req.Value == nil {
return record, entity.ErrIncompleteRequest
}
record = storage.Record{Name: req.ID, Value: *req.Value}
default:
return record, entity.MetricNotImplementedError(req.MType)
}
if signer != nil {
valid, err := signer.VerifyRecordSignature(record, req.Hash)
if err != nil {
// NB (alkurbatov): We don't want to give any hints to potential attacker,
// but still want to debug implementation errors. Thus, the error is only logged.
log.Ctx(ctx).Error().Err(err).Msg("")
}
if err != nil || !valid {
return record, entity.ErrInvalidSignature
}
}
return record, nil
}
func toMetricReq(record storage.Record, signer *security.Signer) (*metrics.MetricReq, error) {
req := &metrics.MetricReq{ID: record.Name, MType: record.Value.Kind()}
if signer != nil {
hash, err := signer.CalculateRecordSignature(record)
if err != nil {
return nil, fmt.Errorf("httpbackend - toMetricReq - signer.CalculateRecordSignature: %w", err)
}
req.Hash = hash
}
switch record.Value.Kind() {
case metrics.KindCounter:
delta, _ := record.Value.(metrics.Counter)
req.Delta = &delta
case metrics.KindGauge:
value, _ := record.Value.(metrics.Gauge)
req.Value = &value
}
return req, nil
}
func toMetricReqList(records []storage.Record, signer *security.Signer) ([]*metrics.MetricReq, error) {
rv := make([]*metrics.MetricReq, len(records))
for i, record := range records {
req, err := toMetricReq(record, signer)
if err != nil {
return nil, fmt.Errorf("httpbackend - toMetricReqList - toMetricReq: %w", err)
}
rv[i] = req
}
return rv, nil
}