go.uber.org/cadence@v1.2.9/internal/headers.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package internal
    22  
    23  import (
    24  	"context"
    25  
    26  	"go.uber.org/cadence/.gen/go/shared"
    27  )
    28  
    29  // HeaderWriter is an interface to write information to cadence headers
    30  type HeaderWriter interface {
    31  	Set(string, []byte)
    32  }
    33  
    34  // HeaderReader is an interface to read information from cadence headers
    35  type HeaderReader interface {
    36  	ForEachKey(handler func(string, []byte) error) error
    37  }
    38  
    39  // ContextPropagator is an interface that determines what information from
    40  // context to pass along
    41  type ContextPropagator interface {
    42  	// Inject injects information from a Go Context into headers
    43  	Inject(context.Context, HeaderWriter) error
    44  
    45  	// Extract extracts context information from headers and returns a context
    46  	// object
    47  	Extract(context.Context, HeaderReader) (context.Context, error)
    48  
    49  	// InjectFromWorkflow injects information from workflow context into headers
    50  	InjectFromWorkflow(Context, HeaderWriter) error
    51  
    52  	// ExtractToWorkflow extracts context information from headers and returns
    53  	// a workflow context
    54  	ExtractToWorkflow(Context, HeaderReader) (Context, error)
    55  }
    56  
    57  type headerReader struct {
    58  	header *shared.Header
    59  }
    60  
    61  func (hr *headerReader) ForEachKey(handler func(string, []byte) error) error {
    62  	if hr.header == nil {
    63  		return nil
    64  	}
    65  	for key, value := range hr.header.Fields {
    66  		if err := handler(key, value); err != nil {
    67  			return err
    68  		}
    69  	}
    70  	return nil
    71  }
    72  
    73  // NewHeaderReader returns a header reader interface
    74  func NewHeaderReader(header *shared.Header) HeaderReader {
    75  	return &headerReader{header}
    76  }
    77  
    78  type headerWriter struct {
    79  	header *shared.Header
    80  }
    81  
    82  func (hw *headerWriter) Set(key string, value []byte) {
    83  	if hw.header == nil {
    84  		return
    85  	}
    86  	hw.header.Fields[key] = value
    87  }
    88  
    89  // NewHeaderWriter returns a header writer interface
    90  func NewHeaderWriter(header *shared.Header) HeaderWriter {
    91  	if header != nil && header.Fields == nil {
    92  		header.Fields = make(map[string][]byte)
    93  	}
    94  	return &headerWriter{header}
    95  }