khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame^] | 1 | package kafka |
| 2 | |
| 3 | /** |
| 4 | * Copyright 2018 Confluent Inc. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "strconv" |
| 22 | ) |
| 23 | |
| 24 | /* |
| 25 | #include <string.h> |
| 26 | #include <librdkafka/rdkafka.h> |
| 27 | #include "glue_rdkafka.h" |
| 28 | */ |
| 29 | import "C" |
| 30 | |
| 31 | // Header represents a single Kafka message header. |
| 32 | // |
| 33 | // Message headers are made up of a list of Header elements, retaining their original insert |
| 34 | // order and allowing for duplicate Keys. |
| 35 | // |
| 36 | // Key is a human readable string identifying the header. |
| 37 | // Value is the key's binary value, Kafka does not put any restrictions on the format of |
| 38 | // of the Value but it should be made relatively compact. |
| 39 | // The value may be a byte array, empty, or nil. |
| 40 | // |
| 41 | // NOTE: Message headers are not available on producer delivery report messages. |
| 42 | type Header struct { |
| 43 | Key string // Header name (utf-8 string) |
| 44 | Value []byte // Header value (nil, empty, or binary) |
| 45 | } |
| 46 | |
| 47 | // String returns the Header Key and data in a human representable possibly truncated form |
| 48 | // suitable for displaying to the user. |
| 49 | func (h Header) String() string { |
| 50 | if h.Value == nil { |
| 51 | return fmt.Sprintf("%s=nil", h.Key) |
| 52 | } |
| 53 | |
| 54 | valueLen := len(h.Value) |
| 55 | if valueLen == 0 { |
| 56 | return fmt.Sprintf("%s=<empty>", h.Key) |
| 57 | } |
| 58 | |
| 59 | truncSize := valueLen |
| 60 | trunc := "" |
| 61 | if valueLen > 50+15 { |
| 62 | truncSize = 50 |
| 63 | trunc = fmt.Sprintf("(%d more bytes)", valueLen-truncSize) |
| 64 | } |
| 65 | |
| 66 | return fmt.Sprintf("%s=%s%s", h.Key, strconv.Quote(string(h.Value[:truncSize])), trunc) |
| 67 | } |