beaut

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

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

Go to latest
Published: Jun 10, 2024 License: Apache-2.0 Imports: 8 Imported by: 3

README

beaut: Convenient handling for validated types

Go Reference Sourcegraph

In certain situations, you've already done some validation on a type. For example, you may have checked that a path is an absolute path, or that a string is valid UTF-8. However, since there is no standard library support for representing such types, maybe you continue passing that value around as a string or a []byte. This has a few downsides:

  • Reduced dev velocity: It is not clear to the reader which operations are safe to perform, and editing the code becomes harder as it is unclear which invariants must be upheld.
  • Poor debugging experience: Failures may appear much further downstream in case validation was not done on all code paths.
  • Performance cost: Out of defensiveness, different parts of the code may perform the same validation redundantly.

Following the Parse, Don't Validate mantra, it is useful to have a dedicated type to represent the validation, so that it is clearer to the reader which operations are safe to perform and which operations require extra care.

This library exposes a few types such as UTF8String, UTF8Bytes, AbsolutePath and RelativePath to represent common validations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbsolutePath

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

func NewAbsolutePath

func NewAbsolutePath(rawValue string) (_ AbsolutePath, ok bool)

func NewAbsolutePathUnchecked

func NewAbsolutePathUnchecked(rawValue string, _ knownwf.AbsPathReason) AbsolutePath

func (*AbsolutePath) Combine

func (ap *AbsolutePath) Combine(other RelativePath) AbsolutePath

func (*AbsolutePath) Join

func (ap *AbsolutePath) Join(others ...RelativePath) AbsolutePath

func (*AbsolutePath) MarshalJSON

func (ap *AbsolutePath) MarshalJSON() ([]byte, error)

func (*AbsolutePath) RawValue

func (ap *AbsolutePath) RawValue() string

func (*AbsolutePath) String

func (ap *AbsolutePath) String() string

func (*AbsolutePath) UnmarshalJSON

func (ap *AbsolutePath) UnmarshalJSON(bytes []byte) error

type ByteIndex

type ByteIndex int

type InvalidCodepointStartIndexError

type InvalidCodepointStartIndexError struct {
	Byte  byte
	Index ByteIndex
}

func (InvalidCodepointStartIndexError) Error

type NotAbsolutePathError

type NotAbsolutePathError struct{ Data string }

func (NotAbsolutePathError) Error

func (n NotAbsolutePathError) Error() string

type NotRelativePathError

type NotRelativePathError struct{ Data string }

func (NotRelativePathError) Error

func (n NotRelativePathError) Error() string

type RelativePath

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

func NewRelativePath

func NewRelativePath(rawValue string) (_ RelativePath, ok bool)

func NewRelativePathUnchecked

func NewRelativePathUnchecked(rawValue string, _ knownwf.RelPathReason) RelativePath

func (*RelativePath) Combine

func (rp *RelativePath) Combine(other RelativePath) RelativePath

func (*RelativePath) Join

func (rp *RelativePath) Join(others ...RelativePath) RelativePath

func (*RelativePath) MarshalJSON

func (rp *RelativePath) MarshalJSON() ([]byte, error)

func (*RelativePath) RawValue

func (rp *RelativePath) RawValue() string

func (*RelativePath) String

func (rp *RelativePath) String() string

func (*RelativePath) UnmarshalJSON

func (rp *RelativePath) UnmarshalJSON(bytes []byte) error

type UTF8Bytes

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

func NewUTF8Bytes

func NewUTF8Bytes(rawValue []byte) (_ UTF8Bytes, ok bool)

func NewUTF8BytesUnchecked

func NewUTF8BytesUnchecked(rawValue []byte, _ knownwf.UTF8Reason) UTF8Bytes

func (*UTF8Bytes) ByteAt

func (s *UTF8Bytes) ByteAt(i ByteIndex) byte

func (*UTF8Bytes) Combine

func (s *UTF8Bytes) Combine(other UTF8Bytes) UTF8Bytes

Combine concatenates the given UTF-8 byte slices into a single UTF-8 byte slice.

func (*UTF8Bytes) IsEmpty

func (s *UTF8Bytes) IsEmpty() bool

func (*UTF8Bytes) Join

func (s *UTF8Bytes) Join(others ...UTF8Bytes) UTF8Bytes

Join concatenates the given UTF-8 byte slices into a single UTF-8 byte slice.

func (*UTF8Bytes) Len

func (s *UTF8Bytes) Len() int

func (*UTF8Bytes) MustAppendASCIIByte

func (s *UTF8Bytes) MustAppendASCIIByte(b byte)

func (*UTF8Bytes) MustAppendRune

func (s *UTF8Bytes) MustAppendRune(r rune)

func (*UTF8Bytes) MustSliceUTF8

func (s *UTF8Bytes) MustSliceUTF8(start ByteIndex, end ByteIndex) UTF8Bytes

func (*UTF8Bytes) RawValueCopy

func (s *UTF8Bytes) RawValueCopy() []byte

RawValueCopy returns a copy of the underlying byte slice.

func (*UTF8Bytes) RawValueNoCopy

func (s *UTF8Bytes) RawValueNoCopy() []byte

RawValueNoCopy returns the raw UTF-8 encoded bytes of the string.

SAFETY: The caller MUST NOT modify the returned value in a way that would break the type's invariants.

func (*UTF8Bytes) RuneAt

func (s *UTF8Bytes) RuneAt(i ByteIndex) (_ rune, runeLength int32)

RuneAt returns the rune at the given byte index.

If the byte index is not a valid UTF-8 code point start index, returns -1 for the length.

func (*UTF8Bytes) SliceUTF8

func (s *UTF8Bytes) SliceUTF8(start ByteIndex, end ByteIndex) (UTF8Bytes, error)

func (*UTF8Bytes) String

func (s *UTF8Bytes) String() string

func (*UTF8Bytes) ToUTF8String

func (s *UTF8Bytes) ToUTF8String() UTF8String

ToUTF8String copies the underlying buffer into a new string.

type UTF8String

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

func NewUTF8String

func NewUTF8String(rawValue string) (_ UTF8String, ok bool)

func NewUTF8StringUnchecked

func NewUTF8StringUnchecked(rawValue string, _ knownwf.UTF8Reason) UTF8String

func (UTF8String) ByteAt

func (s UTF8String) ByteAt(i ByteIndex) byte

func (UTF8String) Combine

func (s UTF8String) Combine(other UTF8String) UTF8String

func (UTF8String) IsEmpty

func (s UTF8String) IsEmpty() bool

func (UTF8String) Join

func (s UTF8String) Join(others ...UTF8String) UTF8String

func (UTF8String) Len

func (s UTF8String) Len() int

func (UTF8String) MustSliceUTF8

func (s UTF8String) MustSliceUTF8(start ByteIndex, end ByteIndex) UTF8String

func (UTF8String) RawValue

func (s UTF8String) RawValue() string

func (UTF8String) RuneAt

func (s UTF8String) RuneAt(i ByteIndex) (_ rune, runeLength int32)

RuneAt returns the rune at the given byte index.

If the byte index is not a valid UTF-8 code point start index, returns -1 for the length.

func (UTF8String) Slice

func (s UTF8String) Slice(start ByteIndex, end ByteIndex) string

func (UTF8String) SliceUTF8

func (s UTF8String) SliceUTF8(start ByteIndex, end ByteIndex) (UTF8String, error)

func (UTF8String) String

func (s UTF8String) String() string

func (UTF8String) ToUTF8Bytes

func (s UTF8String) ToUTF8Bytes() UTF8Bytes

ToUTF8Bytes copies the underlying buffer into a new byte slice.

Directories

Path Synopsis
lib
knownwf
Package knownwf contains some types for documenting why one of the *Unchecked functions was used/why the input is well-formed.
Package knownwf contains some types for documenting why one of the *Unchecked functions was used/why the input is well-formed.

Jump to

Keyboard shortcuts

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