-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
181 lines (144 loc) · 4.52 KB
/
properties.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package servicectx facilitates exchanging arbitrary properties across microservices via HTTP headers, query strings,
// OpenTelemetry/OpenTracing baggage, and/or Go Context.
package servicectx
import (
"net/http"
"strconv"
"strings"
"time"
)
// Values key => value properties
type Values map[string]string
// Properties grouped by a service name
type Properties map[string]Values
// NamePrefix a prefix at the beginning of a property name indicating it belongs to this package
const NamePrefix = "x-service"
const Separator = "-"
// ParsePropertyName parses a string like "x-service-api-branch" into service name ("api"),
// property name ("branch"), and a boolean success flag
func ParsePropertyName(name string) (serviceName, option string, ok bool) {
name = strings.ToLower(name)
if !strings.HasPrefix(name, NamePrefix) {
return "", "", false
}
name = strings.TrimPrefix(name, NamePrefix+Separator)
parts := strings.SplitN(name, Separator, 2)
if len(parts) < 2 {
return "", "", false
}
return parts[0], parts[1], true
}
// GetPropertyName builds a string from service name and property name
func GetPropertyName(serviceName, option string) string {
return NamePrefix + Separator + serviceName + Separator + option
}
// New constructs a new properties instance
func New() Properties {
return Properties{}
}
// HasService checks if there are options for a given service
func (p Properties) HasService(serviceName string) bool {
serviceName = sanitizeServiceName(serviceName)
_, ok := p[serviceName]
return ok
}
// HasProperty checks if a given property exists for a service
func (p Properties) HasProperty(serviceName, option string) bool {
serviceName = sanitizeServiceName(serviceName)
if service, ok := p[serviceName]; ok {
if _, ok := service[option]; ok {
return true
}
}
return false
}
// GetByService returns all options for a given service
func (p Properties) GetByService(serviceName string) Values {
serviceName = sanitizeServiceName(serviceName)
return p[serviceName]
}
// Get returns an property value for a given service
func (p Properties) Get(serviceName, prop, defaultValue string) string {
serviceName = sanitizeServiceName(serviceName)
if service, ok := p[serviceName]; ok {
if value, ok := service[prop]; ok {
return value
}
}
return defaultValue
}
// GetInt returns a property value for a given service as an integer
func (p Properties) GetInt(serviceName, prop string, defaultValue int) int {
valueStr := p.Get(serviceName, prop, "")
if len(valueStr) == 0 {
return defaultValue
}
value, err := strconv.Atoi(valueStr)
if err != nil {
return defaultValue
}
return value
}
// GetDuration returns a property value for a given service as time.Duration
func (p Properties) GetDuration(serviceName, prop string, defaultValue time.Duration) time.Duration {
valueStr := p.Get(serviceName, prop, "")
if len(valueStr) == 0 {
return defaultValue
}
value, err := time.ParseDuration(valueStr)
if err != nil {
return defaultValue
}
return value
}
// GetBool returns a property value for a given service as boolean
func (p Properties) GetBool(serviceName, prop string, defaultValue bool) bool {
valueStr := p.Get(serviceName, prop, "")
if len(valueStr) == 0 {
return defaultValue
}
value, err := strconv.ParseBool(valueStr)
if err != nil {
return defaultValue
}
return value
}
// Set sets a property value for a given service
func (p Properties) Set(serviceName, prop, value string) Properties {
serviceName = sanitizeServiceName(serviceName)
if _, ok := p[serviceName]; !ok {
p[serviceName] = map[string]string{}
}
p[serviceName][prop] = value
return p
}
// HeaderMap returns options as a map of HTTP headers
func (p Properties) HeaderMap() map[string]string {
result := map[string]string{}
for service, props := range p {
for key, value := range props {
result[GetPropertyName(service, key)] = value
}
}
return result
}
// InjectIntoHeaders adds property headers to http.Header
func (p Properties) InjectIntoHeaders(headers http.Header) {
for name, value := range p.HeaderMap() {
headers.Set(name, value)
}
}
// Merge merges two sets of properties. The receiver is modified and returned for chaining.
func (p Properties) Merge(other Properties) Properties {
for serviceName, values := range other {
for key, value := range values {
p.Set(serviceName, key, value)
}
}
return p
}
// removes dashes from service names,
// so that "my-service" and "myservice" are treated equally.
func sanitizeServiceName(name string) string {
return strings.ReplaceAll(name, "-", "")
}