-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
100 lines (95 loc) · 2.46 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
pgs "github.com/lyft/protoc-gen-star"
)
// must...
func (m *Module) must(ok bool, err error) bool {
if err != nil {
m.Fail(err)
}
return ok
}
func (m *Module) failWithInvalidType(field pgs.Field) {
typ := field.Type()
want := ToCustomRule(typ.ProtoType(), typ.ProtoLabel())
m.Failf("Option provided to %s is invalid, use %s",
field.FullyQualifiedName(), want)
}
// RedactionDefaults returns the default value that can be used for the input
// pgs.Field for redaction. Predefined reduction defaults are:
// * `0` for any number type
// * `"REDACTED"` for string type
// * `nil` for byte type
// * `0th value` for enum type
// * `nil` map for map type
// * `nil` for repeated field type
// * for message type, redaction is applied inside the message type
func RedactionDefaults(typ pgs.ProtoType, isRepeated bool) string {
// isRepeated fields is for map or slice type fields
if isRepeated {
return "nil"
}
switch typ {
case pgs.Int32T, pgs.Int64T,
pgs.SInt32, pgs.SInt64,
pgs.UInt32T, pgs.UInt64T,
pgs.SFixed32, pgs.SFixed64,
pgs.Fixed32T, pgs.Fixed64T,
pgs.FloatT, pgs.DoubleT, pgs.EnumT:
return "0"
case pgs.BoolT:
return "false"
case pgs.StringT:
return `"REDACTED"`
case pgs.BytesT, pgs.GroupT:
return "nil"
case pgs.MessageT:
return `-`
default: // repeated and map
return "nil"
}
}
// ToCustomRule return redact proto' field rules based on their type
func ToCustomRule(typ pgs.ProtoType, lab pgs.ProtoLabel) string {
if lab == pgs.Repeated {
return "(redact.custom).element.*"
}
switch typ {
case pgs.FloatT:
return "(redact.custom).float"
case pgs.DoubleT:
return "(redact.custom).double"
case pgs.Int32T:
return "(redact.custom).int32"
case pgs.Int64T:
return "(redact.custom).int64"
case pgs.UInt32T:
return "(redact.custom).uint32"
case pgs.UInt64T:
return "(redact.custom).uint64"
case pgs.SInt32:
return "(redact.custom).sint32"
case pgs.SInt64:
return "(redact.custom).sint64"
case pgs.Fixed32T:
return "(redact.custom).fixed32"
case pgs.Fixed64T:
return "(redact.custom).fixed64"
case pgs.SFixed32:
return "(redact.custom).sfixed32"
case pgs.SFixed64:
return "(redact.custom).sfixed64"
case pgs.BoolT:
return "(redact.custom).bool"
case pgs.StringT:
return "(redact.custom).string"
case pgs.BytesT:
return "(redact.custom).bytes"
case pgs.EnumT:
return "(redact.custom).enum"
case pgs.MessageT:
return "(redact.custom).message.*"
default:
return "(redact.redact)"
}
}