missinggo

package
Version: v0.0.0-...-add6754 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2016 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package missinggo contains miscellaneous helpers used in many of anacrolix' projects.

Index

Examples

Constants

View Source
const O_ACCMODE = os.O_RDONLY | os.O_WRONLY | os.O_RDWR

Variables

This section is empty.

Functions

func AddrIP

func AddrIP(addr net.Addr) net.IP

func AddrPort

func AddrPort(addr net.Addr) int

Extracts the port as an integer from an address string.

func ConvertToSliceOfEmptyInterface

func ConvertToSliceOfEmptyInterface(slice interface{}) (ret []interface{})

func CopyExact

func CopyExact(dest interface{}, src interface{})

func CryHeard

func CryHeard() bool

Calls CryHeard() on a Wolf that is unique to the callers program counter. i.e. every CryHeard() expression has its own Wolf.

func Dispatch

func Dispatch(dest, source interface{}) bool

Obtains the desired value pointed to by dest, from source. If it's not found in source, it will walk source's inheritance values until either blocked by visibility, an implementation is found, or there are no further bases. TODO: If an implementation is obtained from a base class that contains some methods present on a derived class, this might break encapsulation. Maybe there should be a check to ensure this isn't possible.

func Fatal

func Fatal(msg interface{})

func FileInfoAccessTime

func FileInfoAccessTime(fi os.FileInfo) time.Time

Extracts the access time from the FileInfo internals.

func FilePathExists

func FilePathExists(p string) bool

func GzipHTTPHandler

func GzipHTTPHandler(h http.Handler) http.Handler

Gzips response body if the request says it'll allow it.

func HTTPQuotedString

func HTTPQuotedString(s string) string

Performs quoted-string from http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html

func IsEmptyValue

func IsEmptyValue(v reflect.Value) bool

Returns whether the value represents the empty value for its type. Used for example to determine if complex types satisfy the common "omitempty" tag option for marshalling. Taken from http://stackoverflow.com/a/23555352/149482.

func NewSelfSignedCertificate

func NewSelfSignedCertificate() (cert tls.Certificate, err error)

Creates a self-signed certificate in memory for use with tls.Config.

func PathSplitExt

func PathSplitExt(p string) (ret struct {
	Root, Ext string
})

Splits the pathname p into Root and Ext, such that Root+Ext==p.

Example
fmt.Printf("%q\n", PathSplitExt(".cshrc"))
fmt.Printf("%q\n", PathSplitExt("dir/a.ext"))
fmt.Printf("%q\n", PathSplitExt("dir/.rc"))
fmt.Printf("%q\n", PathSplitExt("home/.secret/file"))
Output:

{"" ".cshrc"}
{"dir/a" ".ext"}
{"dir/" ".rc"}
{"home/.secret/file" ""}

func Unchomp

func Unchomp(s string) string

func WriteStack

func WriteStack(w io.Writer, stack []uintptr)

Types

type HTTPBytesContentRange

type HTTPBytesContentRange struct {
	First, Last, Length int64
}

func ParseHTTPBytesContentRange

func ParseHTTPBytesContentRange(s string) (ret HTTPBytesContentRange, ok bool)

type HTTPBytesRange

type HTTPBytesRange struct {
	First, Last int64
}

func ParseHTTPBytesRange

func ParseHTTPBytesRange(s string) (ret HTTPBytesRange, ok bool)

type HostMaybePort

type HostMaybePort struct {
	Host   string
	Port   int
	NoPort bool
}

func SplitHostPort

func SplitHostPort(hostport string) (ret HostMaybePort)

func (HostMaybePort) String

func (me HostMaybePort) String() string

type HostPort

type HostPort struct {
	Host string // Just the host, with no port.
	Port string // May be empty if no port was given.
	Err  error  // The error returned from net.SplitHostPort.
}

func ParseHostPort

func ParseHostPort(hostPort string) (ret HostPort)

Parse a "hostport" string, a concept that floats around the stdlib a lot and is painful to work with. If no port is present, what's usually present is just the host.

func (*HostPort) Join

func (me *HostPort) Join() string

type IndentMap

type IndentMap struct {
	expvar.Map
}

func NewExpvarIndentMap

func NewExpvarIndentMap(name string) *IndentMap

func (*IndentMap) String

func (v *IndentMap) String() string

type Inheriter

type Inheriter interface {
	// Return the base-class object value.
	Base() interface{}
	// Return a pointer to an interface containing the methods retrievable
	// from the base.
	Visible() interface{}
}

A sort-of implementation of single dispatch polymorphic inheritance.

type RWLocker

type RWLocker interface {
	sync.Locker
	RLock()
	RUnlock()
}

type RunLengthEncoder

type RunLengthEncoder interface {
	// Add a series of identical elements to the stream.
	Append(element interface{}, count uint64)
	// Emit the current element and its count if non-zero without waiting for
	// the element to change.
	Flush()
}

A RunLengthEncoder counts successive duplicate elements and emits the element and the run length when the element changes or the encoder is flushed.

func NewRunLengthEncoder

func NewRunLengthEncoder(eachRun func(element interface{}, count uint64)) RunLengthEncoder

Creates a new RunLengthEncoder. eachRun is called when an element and its count is emitted, per the RunLengthEncoder interface.

Example
package main

import (
	"fmt"

	"github.com/anacrolix/missinggo"
)

func main() {
	var s string
	rle := missinggo.NewRunLengthEncoder(func(e interface{}, count uint64) {
		s += fmt.Sprintf("%d%c", count, e)
	})
	for _, e := range "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW" {
		rle.Append(e, 1)
	}
	rle.Flush()
	fmt.Println(s)
}
Output:

12W1B12W3B24W1B14W

type SingleFlight

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

func (*SingleFlight) Lock

func (me *SingleFlight) Lock(id string)

func (*SingleFlight) Unlock

func (me *SingleFlight) Unlock(id string)

type StatWriter

type StatWriter struct {
	Written int64
	// contains filtered or unexported fields
}

func NewStatWriter

func NewStatWriter(w io.Writer) *StatWriter

func (*StatWriter) Write

func (me *StatWriter) Write(b []byte) (n int, err error)

type StatusResponseWriter

type StatusResponseWriter struct {
	RW           http.ResponseWriter
	Code         int
	BytesWritten int64
	// contains filtered or unexported fields
}

A http.ResponseWriter that tracks the status of the response. The status code, and number of bytes written for example.

func (*StatusResponseWriter) Base

func (me *StatusResponseWriter) Base() interface{}

func (*StatusResponseWriter) Header

func (me *StatusResponseWriter) Header() http.Header

func (*StatusResponseWriter) Visible

func (me *StatusResponseWriter) Visible() interface{}

func (*StatusResponseWriter) Write

func (me *StatusResponseWriter) Write(b []byte) (n int, err error)

func (*StatusResponseWriter) WriteHeader

func (me *StatusResponseWriter) WriteHeader(code int)

type Wolf

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

A Wolf represents some event that becomes less and less interesting as it occurs. Call CryHeard to see if we should pay attention this time.

func (*Wolf) CryHeard

func (me *Wolf) CryHeard() bool

Returns true less and less often. Convenient for exponentially decreasing the amount of noise due to errors.

type ZeroReader

type ZeroReader struct{}

func (ZeroReader) Read

func (me ZeroReader) Read(b []byte) (n int, err error)

Jump to

Keyboard shortcuts

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