netcontext

package module
v0.0.0-...-d945414 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Net Context

License

Copyright 2024 Hayo van Loon

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.

Documentation

Index

Constants

View Source
const DefaultHeaderPrefix = "X-Go-Context-"

DefaultHeaderPrefix is the default prefix for HTTP headers and gRPC metadata keys.

Variables

View Source
var TimeFormat = time.RFC3339Nano

TimeFormat used for time.Time context values.

Functions

func DefaultToString

func DefaultToString(a any) string

DefaultToString is a convenience wrapper around fmt.Sprintf.

func GRPCMetadataPrefix

func GRPCMetadataPrefix() string

GRPCMetadataPrefix returns the prefix for gRPC metadata keys.

func HTTPHeaderPrefix

func HTTPHeaderPrefix() string

HTTPHeaderPrefix returns the prefix for HTTP headers.

func Int

func Int(ctxKey any, stringKey string)

Int adds an Entry for an int context value.

func Int32

func Int32(ctxKey any, stringKey string)

Int32 adds an Entry for an int32 context value.

func Int64

func Int64(ctxKey any, stringKey string)

Int64 adds an Entry for an int64 context value.

func Log

func Log(format string, as ...any)

Log logs a message.

func NoStandardDeadLine

func NoStandardDeadLine()

NoStandardDeadLine will disable propagation of the standard Go context deadline. By default, it is enabled.

func Reset

func Reset()

Reset resets the configuration to its default state. It is mainly intended for unit tests. Normal code should have no reason to call this function.

func Set

func Set(ctxKey any, stringKey string, parse ParseFunc, toString StringFunc)

Set adds an Entry with the given parameters. The parser function is required. If the stringer function is not provided, DefaultToString will be used.

func SetGRPCMetadataPrefix

func SetGRPCMetadataPrefix(prefix string)

SetGRPCMetadataPrefix sets the prefix for gRPC metadata keys.

func SetHTTPHeaderPrefix

func SetHTTPHeaderPrefix(prefix string)

SetHTTPHeaderPrefix sets the prefix for HTTP headers.

func SetLogger

func SetLogger(logger LogFunc)

SetLogger sets the log function. Setting it to nil will disable logging.

func SetPrefixes

func SetPrefixes(prefix string)

SetPrefixes sets the same header/metadata prefix for both HTTP/gRPC.

func String

func String(ctxKey any, stringKey string)

String adds an Entry for a string context value.

func Time

func Time(ctxKey any, stringKey string)

Time adds an Entry for a time.Time context value.

Types

type Config

type Config struct {
	HTTPHeaderPrefix   string
	GrpcMetadataPrefix string
	Entries            []Entry
	NoDeadline         bool
	Log                LogFunc
}

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

An Entry describes how to handle the serialisation and deserialisation of a context value.

func Deadline

func Deadline() (Entry, bool)

Deadline returns the Entry to be used for propagating the standard Go context.

func Entries

func Entries() []Entry

func (Entry) CtxKey

func (e Entry) CtxKey() any

CtxKey returns the context key.

func (Entry) Marshal

func (e Entry) Marshal(a any) string

Marshal marshals a value into a string.

func (Entry) StringKey

func (e Entry) StringKey() string

StringKey returns a string representation of a context key.

func (Entry) Unmarshal

func (e Entry) Unmarshal(s string, a any) error

Unmarshal unmarshalls a value into 'a'. Returns an error if 'a' is not a pointer.

type LogFunc

type LogFunc func(format string, as ...any)

type ParseFunc

type ParseFunc func(s string) (any, error)

type StringFunc

type StringFunc func(a any) string

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL