util

package
v0.0.0-...-48cda87 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: UPL-1.0 Imports: 20 Imported by: 0

Documentation

Overview

Copyright (c) 2025, Oracle and/or its affiliates. Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

Copyright (c) 2024, Oracle and/or its affiliates. Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolPtr

func BoolPtr(v bool) *bool

func CompareVersions

func CompareVersions(v1 string, v2 string) (int, error)

CompareVersions compares two version strings that roughly conform to the Semantic Version specification.

func CreateUploadCertificateKey

func CreateUploadCertificateKey() (string, error)

func EnsureNestedMap

func EnsureNestedMap(mapRoot map[string]interface{}, dotPath string) map[string]interface{}

EnsureNestedMap ensures that a nested map exists. The map path is specified using dot notation. For example "a.b.c" will ensure the following exists. The inner-most map is returned

map[string]interface{}{
	"a": map[string]interface{}{
	   "b": map[string]interface{}{
	       "c": map[string]interface{}

func ExponentialRetry

func ExponentialRetry(ftor retryFunc, arg interface{}) (interface{}, bool, error)

ExponentialRetry executes a functor at some interval until is either succeeds, fails in a non-recoverable way, or a timeout is reached. The given functor is called with the given arguments each interval.

func ExponentialRetryImpl

func ExponentialRetryImpl(ftor retryFunc, arg interface{}, start time.Duration, max time.Duration, factor time.Duration, timeout time.Duration) (interface{}, bool, error)

ExponentialRetryImpl executes a functor at some interval until is either succeeds, fails in a non-recoverable way, or a timeout is reached. The given functor is called with the given arguments each interval. 'start' is the first duration to wait. 'max' is the maximum duration. 'factor' is the amount to increase the wait each iteration. 'timeout' is the last time to start a request. Due to the fact that a functor call may be long, this function may take longer than the given timeout.

func ExponentialRetryTimeout

func ExponentialRetryTimeout(ftor retryFunc, arg interface{}, timeout time.Duration) (interface{}, bool, error)

ExponentialRetryTimeout executes a functor at some interval until is either succeeds, fails in a non-recoverable way, or a timeout is reached. The given functor is called with the given arguments each interval.

func FileIsTTY

func FileIsTTY(f *os.File) (bool, error)

FileIsTTY gives a best guess that a given file represents a TTY or PTY. The current best guess is that the file is a character device.

While it is possible for character devices not to be TTYs, it is unlikely to be true in the context of this code. A common counterexample is something like a disk. If someone is trying to do something like pipe a disk to stdin, then they are being silly and some odd behavior can be tolerated.

func FilesFromPath

func FilesFromPath(path string) ([]string, error)

func GetURIAddress

func GetURIAddress(addr string) string

GetURIAddress turns an IP address or hostname into the format required to use in a URI.

func HumanReadableSize

func HumanReadableSize(size uint64) string

func InMemoryFile

func InMemoryFile(name string) (string, error)

InMemoryFile returns the filename of a file that exists only in memory. It can be used with all typical file operations except for deletion.

func IncrementCount

func IncrementCount(in string, delim string) string

IncrementCount adds one to a string that has a number at the end or appends a 1 to a string that does not. Empty strings are returned unmodified.

func Int32Ptr

func Int32Ptr(v int32) *int32

func Int64Ptr

func Int64Ptr(v int64) *int64

func IntPtr

func IntPtr(v int) *int

func IsNil

func IsNil(v interface{}) bool

IsNil is a mostly complete way of checking if a value is nil. It still misses some esoteric types of values.

See: https://go.dev/doc/faq#nil_error

func LinearRetry

func LinearRetry(ftor retryFunc, arg interface{}) (interface{}, bool, error)

LinearRetry executes a functor every 'wait' until it either succeeds, fails in a way that should not be retries, or until the timeout of 10 seconds is reached. If the functor succeeds, the function returns false with no error. If the functor fails in a way that should not be retried, the function returns true with an error. If the function times out, it returns false as well as the last error from the functor.

func LinearRetryImpl

func LinearRetryImpl(ftor retryFunc, arg interface{}, wait time.Duration, timeout time.Duration) (interface{}, bool, error)

LinearRetryImpl executes a functor every 'wait' until it either succeeds, fails in a way that should not be retries, or until the timeout is reached. If the functor succeeds, the function returns false with no error. If the functor fails in a way that should not be retried, the function returns true with an error. If the function times out, it returns false as well as the last error from the functor.

func LinearRetryTimeout

func LinearRetryTimeout(ftor retryFunc, arg interface{}, timeout time.Duration) (interface{}, bool, error)

LinearRetryTimeout executes a functor every 'wait' until it either succeeds, fails in a way that should not be retries, or until the timeout is reached. If the functor succeeds, the function returns false with no error. If the functor fails in a way that should not be retried, the function returns true with an error. If the function times out, it returns false as well as the last error from the functor.

func ResolveURIToIP

func ResolveURIToIP(uri *url.URL) (string, bool, error)

ResolveURIToIP extracts a reasonable candidate for an IP address from the given URI. If the URI can reasonably be said to point to the host, the second return value is "true", otherwise it is "false".

func StrPtr

func StrPtr(v string) *string

func TemplateToString

func TemplateToString(templateString string, contents interface{}) (string, error)

func TemplateToStringWithFuncs

func TemplateToStringWithFuncs(templateString string, contents interface{}, funcs map[string]any) (string, error)

Types

type JsonPatch

type JsonPatch struct {
	Op    string      `json:"op"`
	Path  string      `json:"path"`
	Value interface{} `json:"value"`
}

type JsonPatches

type JsonPatches struct {
	Patches []*JsonPatch
}

func (*JsonPatches) Add

func (jp *JsonPatches) Add(path []string, value interface{}) *JsonPatches

func (*JsonPatches) AddPatch

func (jp *JsonPatches) AddPatch(op string, path []string, value interface{}) *JsonPatches

func (*JsonPatches) Merge

func (jp *JsonPatches) Merge(toMerge *JsonPatches) *JsonPatches

func (*JsonPatches) Replace

func (jp *JsonPatches) Replace(path []string, value interface{}) *JsonPatches

func (*JsonPatches) String

func (jp *JsonPatches) String() string

type MessageDispatcher

type MessageDispatcher struct {
	StartPattern   *regexp.Regexp
	Handler        MessageHandler
	CurrentMessage []string
}

MessageDispatcher is a simple object for taking lines from a scanner and collecting them into multi-line messages based on a regex that defines the start of a new message.

func NewMessageDispatcher

func NewMessageDispatcher(startPattern string, handler MessageHandler) (*MessageDispatcher, error)

NewMessageDispatcher creates a MessageDispatcher

func (*MessageDispatcher) Dispatch

func (md *MessageDispatcher) Dispatch(in string)

Dispatch implements the ScanDispatcher interface

type MessageHandler

type MessageHandler interface {
	Handle([]string)
}

MessageHandler is used to handle multi-line messages

type ScanCloser

type ScanCloser struct {
	Closed bool
	Mutex  sync.Mutex
}

ScanCloser is a token object that is used to close a scanning routine

func Scan

func Scan(reader io.Reader, sd ScanDispatcher) *ScanCloser

func (*ScanCloser) Close

func (sc *ScanCloser) Close()

Close closes a scanner

type ScanDispatcher

type ScanDispatcher interface {
	Dispatch(string)
}

ScanDispatcher is used to process lines from a scanner

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a generic set implementation using Go generics. It supports fast membership tests and population from slices and map keys.

func NewSetFromMapKeys

func NewSetFromMapKeys[T comparable, V any](m map[T]V) *Set[T]

NewSetFromMapKeys creates a set populated from the keys of the given map.

func NewSetFromSlice

func NewSetFromSlice[T comparable](elems []T) *Set[T]

NewSetFromSlice creates a set populated from the elements of a slice.

func (*Set[T]) Add

func (s *Set[T]) Add(elem T)

Add inserts an element into the set.

func (*Set[T]) Contains

func (s *Set[T]) Contains(elem T) bool

Contains tests if an element is in the set.

func (*Set[T]) Remove

func (s *Set[T]) Remove(elem T)

Remove deletes an element from the set.

func (*Set[T]) Size

func (s *Set[T]) Size() int

Size returns the number of elements in the set.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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