x

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

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 23 Imported by: 14

README

Assorted utility functions in Go

pkg.go.dev Go Report Card pipeline status coverage report

There is no guarantee of API stability. Pin the package to a particular commit.

Installation

This is a Go package. You can add it to your project using go get:

go get gitlab.com/tozd/go/x

It requires Go 1.21 or newer.

GitHub mirror

There is also a read-only GitHub mirror available, if you need to fork the project there.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOpenGitRepository = errors.Base("cannot open git repository")
	ErrObtainGitRemote   = errors.Base("cannot obtain git remote")
	ErrParseGitRemoteURL = errors.Base("cannot parse git remote URL")
)
View Source
var (
	ErrResponseClosed               = errors.Base("response already closed")
	ErrResponseReadBeyondEnd        = errors.Base("read beyond the expected end of the response body")
	ErrResponseBadStatus            = errors.Base("bad response status")
	ErrResponseMissingContentLength = errors.Base("missing Content-Length header in response")
	ErrResponseLengthMismatch       = errors.Base("content after retry has different length than before")
)
View Source
var ErrJSONUnmarshalExtraData = errors.Base("invalid data after top-level value")
View Source
var ErrSyncVarAlreadyStored = errors.Base("already stored")

Functions

func CreateTempCertificateFiles

func CreateTempCertificateFiles(certPath, keyPath string, domains []string) errors.E

CreateTempCertificateFiles creates a pair of files for given domains. It generates a ECDSA private key.

func DecodeJSON

func DecodeJSON(reader io.Reader, v interface{}) errors.E

DecodeJSON reads one JSON object from reader and unmarshals it into v. It errors if there is more data trailing after the object.

func DecodeJSONWithoutUnknownFields

func DecodeJSONWithoutUnknownFields(reader io.Reader, v interface{}) errors.E

DecodeJSONWithoutUnknownFields reads one JSON object from reader and unmarshals it into v. It errors if there is more data trailing after the object. It returns an error if there is any unknown field present in JSON.

func InferGitLabProjectID

func InferGitLabProjectID(path string) (string, errors.E)

InferGitLabProjectID infers a GitLab project ID from "origin" remote of a git repository at path.

func Marshal

func Marshal(v interface{}) ([]byte, errors.E)

Marshal is a standard JSON marshal, just that it returns an error with a stack trace.

func MarshalWithoutEscapeHTML

func MarshalWithoutEscapeHTML(v interface{}) ([]byte, errors.E)

MarshalWithoutEscapeHTML is a standard JSON marshal, just that it does not escape HTML characters.

func RatPrecision

func RatPrecision(rat *big.Rat) (int, int)

RatPrecision computes for rat the number of non-repeating digits on the right of the decimal point and the number of repeating digits which cyclicly follow.

It can be used with Rat.FloatString to convert a number to full precision representation, when there are no repeating digits.

This is similar to Rat.FloatPrec but returns also the number of repeating digits following the non-repeating digits. Rat.FloatPrec is also much faster.

func Unmarshal

func Unmarshal(data []byte, v interface{}) errors.E

Unmarshal is a standard JSON unmarshal, just that it returns an error with a stack trace and adds JSON data as an error detail.

func UnmarshalWithoutUnknownFields

func UnmarshalWithoutUnknownFields(data []byte, v interface{}) errors.E

UnmarshalWithoutUnknownFields is a standard JSON unmarshal, just that it returns an error if there is any unknown field present in JSON. It also adds JSON data to any error as an error detail.

Types

type Counter

type Counter int64

Counter is an int64 value which implements counter interface.

func (*Counter) Add

func (c *Counter) Add(n int64)

Increment increases counter by n.

func (*Counter) Count

func (c *Counter) Count() int64

Count implements counter interface for Counter.

func (*Counter) Increment

func (c *Counter) Increment()

Increment increases counter by 1.

type CountingReader

type CountingReader struct {
	Reader io.Reader
	// contains filtered or unexported fields
}

CountingReader is an io.Reader proxy which counts the number of bytes it read and passed on.

func NewCountingReader

func NewCountingReader(reader io.Reader) *CountingReader

NewCountingReader returns a new CountingReader which reads from the reader and counts the bytes.

func (*CountingReader) Count

func (c *CountingReader) Count() int64

Count implements counter interface for CountingReader.

It returns the number of bytes read until now.

func (*CountingReader) Read

func (c *CountingReader) Read(p []byte) (int, error)

Read implements io.Reader interface for CountingReader.

type Progress

type Progress struct {
	Count   int64
	Size    int64
	Started time.Time
	Current time.Time
	Elapsed time.Duration
	// contains filtered or unexported fields
}

Progress describes current progress as reported by the counter.

func (Progress) Estimated

func (p Progress) Estimated() time.Time

func (Progress) Percent

func (p Progress) Percent() float64

func (Progress) Remaining

func (p Progress) Remaining() time.Duration

type RetryableResponse

type RetryableResponse struct {
	*http.Response
	// contains filtered or unexported fields
}

RetryableResponse reads the response body until it is completely read.

If reading fails before full contents have been read (based on the Content-Length header), it transparently retries the request using Range request header and continues reading the new response body.

It embeds the current response (so you can access response headers, etc.) but the current response can change when the request is retried.

func NewRetryableResponse

func NewRetryableResponse(client *retryablehttp.Client, req *retryablehttp.Request) (*RetryableResponse, errors.E)

NewRetryableResponse returns a RetryableResponse given the client and request to do (and potentially retry).

func (*RetryableResponse) Close

func (d *RetryableResponse) Close() error

Close implements io.Closer interface for RetryableResponse.

It closes the underlying response body.

func (*RetryableResponse) Count

func (d *RetryableResponse) Count() int64

Count implements counter interface for RetryableResponse.

It returns the number of bytes read until now.

func (*RetryableResponse) Read

func (d *RetryableResponse) Read(p []byte) (int, error)

Read implements io.Reader for RetryableResponse.

Use this to read the response body and not RetryableResponse.Response.Body.Read.

func (*RetryableResponse) Size

func (d *RetryableResponse) Size() int64

Size returns the expected number of bytes to read.

type SyncVar

type SyncVar[T any] struct {
	// contains filtered or unexported fields
}

SyncVar allows multiple goroutines to wait for a value to be available while one other goroutine stored the value.

It is useful if you do not know in advance which goroutine will be the one (and only one) which stores the value while all goroutines need the value.

The zero value for a SyncVar is not usable. Use NewSyncVar.

func NewSyncVar

func NewSyncVar[T any]() *SyncVar[T]

NewSyncVar creates a new SyncVar.

func (*SyncVar[T]) Load

func (w *SyncVar[T]) Load() T

Load returns the value stored in the SyncVar. It blocks until the value is stored.

func (*SyncVar[T]) LoadContext

func (w *SyncVar[T]) LoadContext(ctx context.Context) (T, errors.E)

LoadContext is similar to Load, but it stops waiting if ctx gets cancelled, returning an error in that case.

func (*SyncVar[T]) Store

func (w *SyncVar[T]) Store(v T) errors.E

Store stores the value in the SyncVar and unblocks any prior calls to Load. It can be called only once.

type Ticker

type Ticker struct {
	C <-chan Progress
	// contains filtered or unexported fields
}

func NewTicker

func NewTicker(ctx context.Context, c counter, size int64, interval time.Duration) *Ticker

NewTicker creates a new Ticker which at regular interval reports the progress as reported by the counter c.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop stops the ticker and frees resources.

Jump to

Keyboard shortcuts

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