Copyright The OpenTelemetry Authors Licensed 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 correlation

import (
	

	
)

type correlationsType struct{}
SetHookFunc describes a type of a callback that is called when storing baggage in the context.
GetHookFunc describes a type of a callback that is called when getting baggage from the context.
value under this key is either of type Map or correlationsData
var correlationsKey = &correlationsType{}

type correlationsData struct {
	m       Map
	setHook SetHookFunc
	getHook GetHookFunc
}

func ( correlationsData) () bool {
	return .setHook == nil && .getHook == nil
}

type hookKind int

const (
	hookKindSet hookKind = iota
	hookKindGet
)

func ( *correlationsData) ( hookKind,  SetHookFunc,  GetHookFunc) {
	switch  {
	case hookKindSet:
		.setHook = 
	case hookKindGet:
		.getHook = 
	}
}
ContextWithSetHook installs a hook function that will be invoked every time ContextWithMap is called. To avoid unnecessary callback invocations (recursive or not), the callback can temporarily clear the hooks from the context with the ContextWithNoHooks function. Note that NewContext also calls ContextWithMap, so the hook will be invoked. Passing nil SetHookFunc creates a context with no set hook to call. This function should not be used by applications or libraries. It is mostly for interoperation with other observability APIs.
ContextWithGetHook installs a hook function that will be invoked every time MapFromContext is called. To avoid unnecessary callback invocations (recursive or not), the callback can temporarily clear the hooks from the context with the ContextWithNoHooks function. Note that NewContext also calls MapFromContext, so the hook will be invoked. Passing nil GetHookFunc creates a context with no get hook to call. This function should not be used by applications or libraries. It is mostly for interoperation with other observability APIs.
func ( context.Context,  GetHookFunc) context.Context {
	return contextWithHook(, hookKindGet, nil, )
}

func ( context.Context,  hookKind,  SetHookFunc,  GetHookFunc) context.Context {
	switch v := .Value(correlationsKey).(type) {
	case correlationsData:
		.overrideHook(, , )
		if .isHookless() {
			return context.WithValue(, correlationsKey, .m)
		}
		return context.WithValue(, correlationsKey, )
	case Map:
		return contextWithOneHookAndMap(, , , , )
	default:
		 := NewEmptyMap()
		return contextWithOneHookAndMap(, , , , )
	}
}

func ( context.Context,  hookKind,  SetHookFunc,  GetHookFunc,  Map) context.Context {
	 := correlationsData{m: }
	.overrideHook(, , )
	if .isHookless() {
		return 
	}
	return context.WithValue(, correlationsKey, )
}
ContextWithNoHooks creates a context with all the hooks disabled. Also returns old set and get hooks. This function can be used to temporarily clear the context from hooks and then reinstate them by calling ContextWithSetHook and ContextWithGetHook functions passing the hooks returned by this function. This function should not be used by applications or libraries. It is mostly for interoperation with other observability APIs.
func ( context.Context) (context.Context, SetHookFunc, GetHookFunc) {
	switch v := .Value(correlationsKey).(type) {
	case correlationsData:
		return context.WithValue(, correlationsKey, .m), .setHook, .getHook
	default:
		return , nil, nil
	}
}
ContextWithMap returns a context with the Map entered into it.
func ( context.Context,  Map) context.Context {
	switch v := .Value(correlationsKey).(type) {
	case correlationsData:
		.m = 
		 = context.WithValue(, correlationsKey, )
		if .setHook != nil {
			 = .setHook()
		}
		return 
	default:
		return context.WithValue(, correlationsKey, )
	}
}
NewContext returns a context with the map from passed context updated with the passed key-value pairs.
func ( context.Context,  ...label.KeyValue) context.Context {
	return ContextWithMap(, MapFromContext().Apply(MapUpdate{
		MultiKV: ,
	}))
}
MapFromContext gets the current Map from a Context.
func ( context.Context) Map {
	switch v := .Value(correlationsKey).(type) {
	case correlationsData:
		if .getHook != nil {
			return .getHook(, .m)
		}
		return .m
	case Map:
		return 
	default:
		return NewEmptyMap()
	}