utils

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SerializationMethodToContentType = map[string]string{
		"json":      "application/json",
		"form":      "application/x-www-form-urlencoded",
		"multipart": "multipart/form-data",
		"raw":       "application/octet-stream",
		"string":    "text/plain",
	}
)

Functions

func AsSecuritySource

func AsSecuritySource(security interface{}) func(context.Context) (interface{}, error)

func CalculateJSONSize added in v0.11.1

func CalculateJSONSize(v interface{}) int

CalculateJSONSize returns the byte size of the JSON representation of a value. This is used to determine which union type variant has the most data.

func ConsumeRawBody

func ConsumeRawBody(res *http.Response) ([]byte, error)

func Contains

func Contains(slice []string, item string) bool

func CountFields added in v0.11.1

func CountFields(v interface{}) int

CountFields recursively counts the number of valid (non-nil, non-zero) fields set in a value. This is used as the primary discriminator for union types, with JSON size as a tiebreaker.

func GenerateURL

func GenerateURL(_ context.Context, serverURL, path string, pathParams interface{}, globals interface{}) (string, error)

func GetEnv

func GetEnv(name, defaultValue string) string

GetEnv returns the value of the environment variable named by the key or the defaultValue if the environment variable is not set.

func MarshalJSON

func MarshalJSON(v interface{}, tag reflect.StructTag, topLevel bool) ([]byte, error)

func MatchContentType

func MatchContentType(contentType string, pattern string) bool

func MatchStatusCodes

func MatchStatusCodes(expectedCodes []string, statusCode int) bool

func PopulateHeaders

func PopulateHeaders(_ context.Context, req *http.Request, headers interface{}, globals interface{})

func PopulateQueryParams

func PopulateQueryParams(_ context.Context, req *http.Request, queryParams interface{}, globals interface{}) error

func PopulateSecurity

func PopulateSecurity(ctx context.Context, req *http.Request, securitySource func(context.Context) (interface{}, error)) error

func PopulateSecurityFromEnv

func PopulateSecurityFromEnv(security interface{}) bool

func ReplaceParameters

func ReplaceParameters(stringWithParams string, params map[string]string) string

func Retry

func Retry(ctx context.Context, r Retries, operation func() (*http.Response, error)) (*http.Response, error)

func SerializeRequestBody

func SerializeRequestBody(_ context.Context, request interface{}, nullable, optional bool, requestFieldName, serializationMethod, tag string) (io.Reader, string, error)

func UnmarshalJSON

func UnmarshalJSON(b []byte, v interface{}, tag reflect.StructTag, topLevel bool, requiredFields []string) error

func UnmarshalJsonFromResponseBody

func UnmarshalJsonFromResponseBody(body io.Reader, out interface{}, tag string) error

func ValueFromEnvVar

func ValueFromEnvVar(envVar string, field interface{}) interface{}

Types

type BackoffStrategy deprecated

type BackoffStrategy = retry.BackoffStrategy

Deprecated: Use retry.BackoffStrategy instead.

type Retries

type Retries struct {
	Config      *retry.Config
	StatusCodes []string
}

type RetryConfig deprecated

type RetryConfig = retry.Config

Deprecated: Use retry.Config instead.

type Timer

type Timer interface {
	Start(duration time.Duration)
	Stop()
	C() <-chan time.Time
}

type UnionCandidate added in v0.11.1

type UnionCandidate struct {
	FieldCount int
	Size       int
	Type       any // The union type enum value
	Value      any // The unmarshaled value
}

UnionCandidate represents a candidate type during union deserialization

func PickBestCandidate added in v0.11.1

func PickBestCandidate(candidates []UnionCandidate) *UnionCandidate

PickBestCandidate selects the best union type candidate using a multi-stage filtering approach: 1. If multiple candidates, filter by field count (keep only those with max field count) 2. If still multiple, filter by JSON size (keep only those with max size) 3. Return the first remaining candidate

Jump to

Keyboard shortcuts

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