github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/header.go (about)

     1  /**
     2   * Copyright 2018 Confluent Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package kafka
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  )
    23  
    24  /*
    25  #include <string.h>
    26  #include "select_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  }