-
Notifications
You must be signed in to change notification settings - Fork 301
/
utils.go
73 lines (65 loc) · 2.04 KB
/
utils.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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package identityserver
import (
"context"
"strconv"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/validate"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
var (
getPaths = []string{"ids", "created_at", "updated_at"}
updatePaths = []string{"updated_at"}
)
func cleanFieldMaskPaths(allowedPaths, requestedPaths, addPaths, removePaths []string) []string {
selected := make(map[string]struct{})
for _, path := range addPaths {
selected[path] = struct{}{}
}
for _, path := range requestedPaths {
selected[path] = struct{}{}
}
for _, path := range removePaths {
delete(selected, path)
}
out := make([]string, 0, len(selected))
for _, path := range allowedPaths {
if _, ok := selected[path]; ok {
out = append(out, path)
}
}
return out
}
func cleanContactInfo(info []*ttnpb.ContactInfo) {
for _, info := range info {
info.ValidatedAt = nil
}
}
// TODO: Move this logic to validators in API boundary (https://github.com/TheThingsNetwork/lorawan-stack/issues/69).
func validateContactInfo(info []*ttnpb.ContactInfo) error {
for _, info := range info {
if info.ContactMethod != ttnpb.CONTACT_METHOD_EMAIL {
continue
}
if err := validate.Email(info.Value); err != nil {
return err
}
}
return nil
}
func setTotalHeader(ctx context.Context, total uint64) {
grpc.SetHeader(ctx, metadata.Pairs("x-total-count", strconv.FormatUint(total, 10)))
}