blob: 21e880d66c25cc87df26e1ea9e8db449047c0be5 [file] [log] [blame]
Yuxuan 'fishy' Wangb1002a72019-08-05 13:03:02 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package thrift
21
22import (
23 "context"
24)
25
26// See https://godoc.org/context#WithValue on why do we need the unexported typedefs.
27type (
28 headerKey string
29 headerKeyList int
30)
31
32// Values for headerKeyList.
33const (
34 headerKeyListRead headerKeyList = iota
Yuxuan 'fishy' Wang26ef9042019-08-19 00:18:22 -070035 headerKeyListWrite
Yuxuan 'fishy' Wangb1002a72019-08-05 13:03:02 -070036)
37
38// SetHeader sets a header in the context.
39func SetHeader(ctx context.Context, key, value string) context.Context {
40 return context.WithValue(
41 ctx,
42 headerKey(key),
43 value,
44 )
45}
46
47// GetHeader returns a value of the given header from the context.
48func GetHeader(ctx context.Context, key string) (value string, ok bool) {
49 if v := ctx.Value(headerKey(key)); v != nil {
50 value, ok = v.(string)
51 }
52 return
53}
54
55// SetReadHeaderList sets the key list of read THeaders in the context.
56func SetReadHeaderList(ctx context.Context, keys []string) context.Context {
57 return context.WithValue(
58 ctx,
59 headerKeyListRead,
60 keys,
61 )
62}
63
64// GetReadHeaderList returns the key list of read THeaders from the context.
65func GetReadHeaderList(ctx context.Context) []string {
66 if v := ctx.Value(headerKeyListRead); v != nil {
67 if value, ok := v.([]string); ok {
68 return value
69 }
70 }
71 return nil
72}
73
Yuxuan 'fishy' Wang26ef9042019-08-19 00:18:22 -070074// SetWriteHeaderList sets the key list of THeaders to write in the context.
75func SetWriteHeaderList(ctx context.Context, keys []string) context.Context {
76 return context.WithValue(
77 ctx,
78 headerKeyListWrite,
79 keys,
80 )
81}
82
83// GetWriteHeaderList returns the key list of THeaders to write from the context.
84func GetWriteHeaderList(ctx context.Context) []string {
85 if v := ctx.Value(headerKeyListWrite); v != nil {
86 if value, ok := v.([]string); ok {
87 return value
88 }
89 }
90 return nil
91}
92
Yuxuan 'fishy' Wangb1002a72019-08-05 13:03:02 -070093// AddReadTHeaderToContext adds the whole THeader headers into context.
94func AddReadTHeaderToContext(ctx context.Context, headers THeaderMap) context.Context {
95 keys := make([]string, 0, len(headers))
96 for key, value := range headers {
97 ctx = SetHeader(ctx, key, value)
98 keys = append(keys, key)
99 }
100 return SetReadHeaderList(ctx, keys)
101}