x

package module
Version: v0.0.0-...-5181091 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2022 License: Apache-2.0 Imports: 16 Imported by: 3

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

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

This section is empty.

Functions

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 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.

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.

Types

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 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