summaryrefslogtreecommitdiff
path: root/lib/go/thrift/header_context.go
blob: ac9bd4882befb55b8c76132c5fda10089cac1893 (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 thrift

import (
	"context"
)

// See https://godoc.org/context#WithValue on why do we need the unexported typedefs.
type (
	headerKey     string
	headerKeyList int
)

// Values for headerKeyList.
const (
	headerKeyListRead headerKeyList = iota
	headerKeyListWrite
)

// SetHeader sets a header in the context.
func SetHeader(ctx context.Context, key, value string) context.Context {
	return context.WithValue(
		ctx,
		headerKey(key),
		value,
	)
}

// UnsetHeader unsets a previously set header in the context.
func UnsetHeader(ctx context.Context, key string) context.Context {
	return context.WithValue(
		ctx,
		headerKey(key),
		nil,
	)
}

// GetHeader returns a value of the given header from the context.
func GetHeader(ctx context.Context, key string) (value string, ok bool) {
	if v := ctx.Value(headerKey(key)); v != nil {
		value, ok = v.(string)
	}
	return
}

// SetReadHeaderList sets the key list of read THeaders in the context.
func SetReadHeaderList(ctx context.Context, keys []string) context.Context {
	return context.WithValue(
		ctx,
		headerKeyListRead,
		keys,
	)
}

// GetReadHeaderList returns the key list of read THeaders from the context.
func GetReadHeaderList(ctx context.Context) []string {
	if v := ctx.Value(headerKeyListRead); v != nil {
		if value, ok := v.([]string); ok {
			return value
		}
	}
	return nil
}

// SetWriteHeaderList sets the key list of THeaders to write in the context.
func SetWriteHeaderList(ctx context.Context, keys []string) context.Context {
	return context.WithValue(
		ctx,
		headerKeyListWrite,
		keys,
	)
}

// GetWriteHeaderList returns the key list of THeaders to write from the context.
func GetWriteHeaderList(ctx context.Context) []string {
	if v := ctx.Value(headerKeyListWrite); v != nil {
		if value, ok := v.([]string); ok {
			return value
		}
	}
	return nil
}

// AddReadTHeaderToContext adds the whole THeader headers into context.
func AddReadTHeaderToContext(ctx context.Context, headers THeaderMap) context.Context {
	keys := make([]string, 0, len(headers))
	for key, value := range headers {
		ctx = SetHeader(ctx, key, value)
		keys = append(keys, key)
	}
	return SetReadHeaderList(ctx, keys)
}