typ

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

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

Go to latest
Published: Jul 6, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

README

typ

Imagine a *string type: Does this type correspond to a nullable or an undefinable string?

Insecurities about types like the one above are eliminated by using the types of this package. Distinction between undefined, null and concrete values is as important as handling the same data types equally, over the whole application.

Build your own Undefinable[Foo], Nullable[Foo] or even UndefinableNullable[Foo] type with the provided generic types of this package. Even union types are supported by using the AnyOf[AnyOfRuler] generic type.

Example:

import (
	"fmt"
	"github.com/inkognitro/typ"
)

id1 := typ.NewUuidV4()
id2 := typ.UndefinableUuid{}

// Try to convert an undefinable value to a defined one:
if _, ok := id2.ToUuid(); !ok {
    fmt.Println("could not convert id2 to uuid, because the underlying value is undefined")
}

// Comparison between two values:
if id1.ToUndefinableUuid().Equals(id2) {
    fmt.Println("what the heck is going on here")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyOf

type AnyOf[T AnyOfRuler] struct {
	// contains filtered or unexported fields
}

func NewAnyOf

func NewAnyOf[T AnyOfRuler](value any) AnyOf[T]

func (AnyOf[T]) MarshalJSON

func (t AnyOf[T]) MarshalJSON() ([]byte, error)

func (AnyOf[T]) Ruler

func (t AnyOf[T]) Ruler() AnyOfRuler

func (*AnyOf[T]) UnmarshalJSON

func (t *AnyOf[T]) UnmarshalJSON(data []byte) error

func (AnyOf[T]) Value

func (t AnyOf[T]) Value() any

type AnyOfRuler

type AnyOfRuler interface {
	Discriminator() string
	SupportedValuesByDiscriminatorValue() map[string]any
}

type AnyOfSupporter

type AnyOfSupporter interface {
	Ruler() AnyOfRuler
	// contains filtered or unexported methods
}

type Boolean

type Boolean bool

func NewBoolean

func NewBoolean(content bool) Boolean

func (Boolean) IsSame

func (t Boolean) IsSame(compT Boolean) bool

func (Boolean) MarshalJSON

func (t Boolean) MarshalJSON() ([]byte, error)

func (Boolean) String

func (t Boolean) String() string

func (Boolean) ToBool

func (t Boolean) ToBool() bool

func (Boolean) ToUndefinableBoolean

func (t Boolean) ToUndefinableBoolean() UndefinableBoolean

func (*Boolean) UnmarshalJSON

func (t *Boolean) UnmarshalJSON(data []byte) error

type Float

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

func NewFloat

func NewFloat(value float64) Float

func (Float) Equals

func (t Float) Equals(compT Float) bool

func (Float) MarshalJSON

func (t Float) MarshalJSON() ([]byte, error)

func (Float) Precision

func (t Float) Precision() int

func (Float) SetPrecision

func (t Float) SetPrecision(precision int) Float

func (Float) String

func (t Float) String() string

func (Float) ToFloat64

func (t Float) ToFloat64() float64

func (Float) ToNullableFloat

func (t Float) ToNullableFloat() NullableFloat

func (Float) ToUndefinableFloat

func (t Float) ToUndefinableFloat() UndefinableFloat

func (*Float) UnmarshalJSON

func (t *Float) UnmarshalJSON(data []byte) error

type Int

type Int int64

func (Int) Equals

func (t Int) Equals(compT Int) bool

func (Int) MarshalJSON

func (t Int) MarshalJSON() ([]byte, error)

func (Int) String

func (t Int) String() string

func (Int) ToInt64

func (t Int) ToInt64() int64

func (Int) ToNullableInt

func (t Int) ToNullableInt() NullableInt

func (Int) ToUndefinableInt

func (t Int) ToUndefinableInt() UndefinableInt

func (*Int) UnmarshalJSON

func (t *Int) UnmarshalJSON(data []byte) error

type MicroTime

type MicroTime time.Time

func NewUtcMicroTimeFromTime

func NewUtcMicroTimeFromTime(t time.Time) MicroTime

func NewUtcMicroTimeNow

func NewUtcMicroTimeNow() MicroTime

func (MicroTime) IsPast

func (t MicroTime) IsPast() bool

func (MicroTime) IsSame

func (t MicroTime) IsSame(compT MicroTime) bool

func (MicroTime) MarshalJSON

func (t MicroTime) MarshalJSON() ([]byte, error)

func (MicroTime) String

func (t MicroTime) String() string

func (MicroTime) ToNullableMicroTime

func (t MicroTime) ToNullableMicroTime() NullableMicroTime

func (MicroTime) ToSecondsFromNow

func (t MicroTime) ToSecondsFromNow() int

func (MicroTime) ToTime

func (t MicroTime) ToTime() time.Time

func (MicroTime) ToUndefinableMicroTime

func (t MicroTime) ToUndefinableMicroTime() UndefinableMicroTime

func (MicroTime) ToUnix

func (t MicroTime) ToUnix() int64

func (MicroTime) ToUtcString

func (t MicroTime) ToUtcString() string

func (MicroTime) TruncatedToSeconds

func (t MicroTime) TruncatedToSeconds() MicroTime

func (*MicroTime) UnmarshalJSON

func (t *MicroTime) UnmarshalJSON(data []byte) error

func (MicroTime) WithAddedDays

func (t MicroTime) WithAddedDays(days int) MicroTime

type Nullable

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

func NewNullable

func NewNullable[T any](content T) Nullable[T]

func (Nullable[T]) Equals

func (u Nullable[T]) Equals(typeToCompare Nullable[T], compare compareFunc[T]) bool

func (Nullable[T]) IsNull

func (u Nullable[T]) IsNull() bool

func (Nullable[T]) MarshalJSON

func (u Nullable[T]) MarshalJSON() ([]byte, error)

func (Nullable[T]) String

func (u Nullable[T]) String() string

func (*Nullable[T]) UnmarshalJSON

func (u *Nullable[T]) UnmarshalJSON(data []byte) error

func (Nullable[T]) Value

func (u Nullable[T]) Value() (T, bool)

type NullableFloat

type NullableFloat struct {
	Nullable[Float]
}

func (NullableFloat) Equals

func (t NullableFloat) Equals(compT NullableFloat) bool

func (NullableFloat) ToFloatType

func (t NullableFloat) ToFloatType() (Float, bool)

func (NullableFloat) ToUndefinableNullableFloat

func (t NullableFloat) ToUndefinableNullableFloat() UndefinableNullableFloat

type NullableInt

type NullableInt struct {
	Nullable[Int]
}

func (NullableInt) Equals

func (t NullableInt) Equals(compT NullableInt) bool

func (NullableInt) ToUndefinableNullableInt

func (t NullableInt) ToUndefinableNullableInt() UndefinableNullableInt

type NullableMicroTime

type NullableMicroTime struct {
	Nullable[MicroTime]
}

func (NullableMicroTime) IsSame

func (t NullableMicroTime) IsSame(compT NullableMicroTime) bool

func (NullableMicroTime) ToUndefinableNullableMicroTime

func (t NullableMicroTime) ToUndefinableNullableMicroTime() UndefinableNullableMicroTime

type NullableString

type NullableString struct {
	Nullable[String]
}

func (NullableString) EqualsCaseSensitive

func (t NullableString) EqualsCaseSensitive(compT NullableString) bool

func (NullableString) ToUndefinableNullableString

func (t NullableString) ToUndefinableNullableString() UndefinableNullableString

func (NullableString) Value

func (t NullableString) Value() (String, bool)

type NullableSupporter

type NullableSupporter interface {
	// contains filtered or unexported methods
}

type NullableUuid

type NullableUuid struct {
	Nullable[Uuid]
}

func NewNullableUuid

func NewNullableUuid(uuid Uuid) NullableUuid

func (NullableUuid) Equals

func (t NullableUuid) Equals(compT NullableUuid) bool

func (NullableUuid) ToUndefinableNullableUuid

func (t NullableUuid) ToUndefinableNullableUuid() UndefinableNullableUuid

type PasswordHash

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

func NewPasswordHash

func NewPasswordHash(passwordHash string) PasswordHash

func NewPasswordHashFromStringType

func NewPasswordHashFromStringType(password String, cost int) (PasswordHash, error)

func (PasswordHash) FindPasswordsUtf8RuneCount

func (t PasswordHash) FindPasswordsUtf8RuneCount() (int, bool)

func (PasswordHash) IsSame

func (t PasswordHash) IsSame(compHash PasswordHash) bool

func (PasswordHash) MarshalJSON

func (t PasswordHash) MarshalJSON() ([]byte, error)

func (PasswordHash) String

func (t PasswordHash) String() string

func (PasswordHash) ToString

func (t PasswordHash) ToString() string

func (PasswordHash) ToUndefinablePasswordHash

func (t PasswordHash) ToUndefinablePasswordHash() UndefinablePasswordHash

func (*PasswordHash) UnmarshalJSON

func (t *PasswordHash) UnmarshalJSON(data []byte) error

func (PasswordHash) Validate

func (t PasswordHash) Validate(password String) bool

type RelativeDayTime

type RelativeDayTime string

func NewRelativeDayTime

func NewRelativeDayTime(content string) (RelativeDayTime, error)

func (RelativeDayTime) Equals

func (t RelativeDayTime) Equals(compT RelativeDayTime) bool

func (RelativeDayTime) IsBefore

func (t RelativeDayTime) IsBefore(compT RelativeDayTime) bool

func (RelativeDayTime) IsValid

func (t RelativeDayTime) IsValid() bool

func (RelativeDayTime) MarshalJSON

func (t RelativeDayTime) MarshalJSON() ([]byte, error)

func (RelativeDayTime) String

func (t RelativeDayTime) String() string

func (RelativeDayTime) ToString

func (t RelativeDayTime) ToString() string

func (RelativeDayTime) ToTime

func (t RelativeDayTime) ToTime(ianaTimezone string) (time.Time, error)

func (RelativeDayTime) ToTimeOrPanic

func (t RelativeDayTime) ToTimeOrPanic(ianaTimezone string) time.Time

func (*RelativeDayTime) UnmarshalJSON

func (t *RelativeDayTime) UnmarshalJSON(data []byte) error

type RelativeTime

type RelativeTime string

func NewRelativeTime

func NewRelativeTime(content string) (RelativeTime, error)

func (RelativeTime) Equals

func (t RelativeTime) Equals(compT RelativeTime) bool

func (RelativeTime) IsBefore

func (t RelativeTime) IsBefore(compT RelativeTime) bool

func (RelativeTime) IsValid

func (t RelativeTime) IsValid() bool

func (RelativeTime) MarshalJSON

func (t RelativeTime) MarshalJSON() ([]byte, error)

func (RelativeTime) String

func (t RelativeTime) String() string

func (RelativeTime) ToString

func (t RelativeTime) ToString() string

func (RelativeTime) ToTime

func (t RelativeTime) ToTime(ianaTimezone string) (time.Time, error)

func (RelativeTime) ToTimeOrPanic

func (t RelativeTime) ToTimeOrPanic(ianaTimezone string) time.Time

func (*RelativeTime) UnmarshalJSON

func (t *RelativeTime) UnmarshalJSON(data []byte) error

type String

type String string

func NewSecureRandomAlphaNumericString

func NewSecureRandomAlphaNumericString(length int) String

func NewString

func NewString(content string) String

func (String) EqualsCaseInSensitive

func (t String) EqualsCaseInSensitive(compT String) bool

func (String) EqualsCaseSensitive

func (t String) EqualsCaseSensitive(compT String) bool

func (String) GetUtf8RuneCount

func (t String) GetUtf8RuneCount() int

func (String) HasEmailAddressFormat

func (t String) HasEmailAddressFormat() bool

func (String) HasUrlFormat

func (t String) HasUrlFormat() bool

func (String) MarshalJSON

func (t String) MarshalJSON() ([]byte, error)

func (String) String

func (t String) String() string

func (String) ToLowerCaseString

func (t String) ToLowerCaseString() string

func (String) ToNullableString

func (t String) ToNullableString() NullableString

func (String) ToPasswordHash

func (t String) ToPasswordHash(cost int) (PasswordHash, error)

func (String) ToString

func (t String) ToString() string

func (String) ToStrings

func (t String) ToStrings() Strings

func (String) ToUndefinableNullableString

func (t String) ToUndefinableNullableString() UndefinableNullableString

func (String) ToUndefinableString

func (t String) ToUndefinableString() UndefinableString

func (*String) UnmarshalJSON

func (t *String) UnmarshalJSON(data []byte) error

type Strings

type Strings []String

func (Strings) Add

func (t Strings) Add(content String) Strings

func (Strings) Count

func (t Strings) Count() int

func (Strings) EqualsCaseSensitive

func (t Strings) EqualsCaseSensitive(compT Strings) bool

func (Strings) MarshalJSON

func (t Strings) MarshalJSON() ([]byte, error)

func (Strings) String

func (t Strings) String() string

func (Strings) ToStrings

func (t Strings) ToStrings() []string

func (Strings) ToUndefinableStrings

func (t Strings) ToUndefinableStrings() UndefinableStrings

func (*Strings) UnmarshalJSON

func (t *Strings) UnmarshalJSON(data []byte) error

type Undefinable

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

func NewUndefinable

func NewUndefinable[T any](content T) Undefinable[T]

func (Undefinable[T]) Equals

func (u Undefinable[T]) Equals(typeToCompare Undefinable[T], compare compareFunc[T]) bool

func (Undefinable[T]) IsUndefined

func (u Undefinable[T]) IsUndefined() bool

func (Undefinable[T]) MarshalJSON

func (u Undefinable[T]) MarshalJSON() ([]byte, error)

func (Undefinable[T]) String

func (u Undefinable[T]) String() string

func (*Undefinable[T]) UnmarshalJSON

func (u *Undefinable[T]) UnmarshalJSON(data []byte) error

func (Undefinable[T]) Value

func (u Undefinable[T]) Value() (T, bool)

type UndefinableBoolean

type UndefinableBoolean struct {
	Undefinable[Boolean]
}

func (UndefinableBoolean) ToBool

func (t UndefinableBoolean) ToBool() (bool, bool)

type UndefinableFloat

type UndefinableFloat struct {
	Undefinable[Float]
}

func (UndefinableFloat) String

func (t UndefinableFloat) String() string

type UndefinableInt

type UndefinableInt struct {
	Undefinable[Int]
}

func (UndefinableInt) String

func (t UndefinableInt) String() string

func (UndefinableInt) ToIntType

func (t UndefinableInt) ToIntType() (Int, bool)

type UndefinableMicroTime

type UndefinableMicroTime struct {
	Undefinable[MicroTime]
}

func (UndefinableMicroTime) IsSame

type UndefinableNullableFloat

type UndefinableNullableFloat struct {
	Undefinable[NullableFloat]
}

func (UndefinableNullableFloat) Equals

func (UndefinableNullableFloat) ToNullableFloat

func (t UndefinableNullableFloat) ToNullableFloat() (NullableFloat, bool)

type UndefinableNullableInt

type UndefinableNullableInt struct {
	Undefinable[NullableInt]
}

func (UndefinableNullableInt) Equals

type UndefinableNullableMicroTime

type UndefinableNullableMicroTime struct {
	Undefinable[NullableMicroTime]
}

func (UndefinableNullableMicroTime) IsSame

type UndefinableNullableString

type UndefinableNullableString struct {
	Undefinable[NullableString]
}

func (UndefinableNullableString) EqualsCaseSensitive

func (t UndefinableNullableString) EqualsCaseSensitive(compT UndefinableNullableString) bool

type UndefinableNullableUuid

type UndefinableNullableUuid struct {
	Undefinable[NullableUuid]
}

func (UndefinableNullableUuid) Equals

type UndefinablePasswordHash

type UndefinablePasswordHash struct {
	Undefinable[PasswordHash]
}

type UndefinableString

type UndefinableString struct {
	Undefinable[String]
}

func (UndefinableString) ToString

func (t UndefinableString) ToString() (string, bool)

func (UndefinableString) ToStringType

func (t UndefinableString) ToStringType() (String, bool)

type UndefinableStrings

type UndefinableStrings struct {
	Undefinable[Strings]
}

type UndefinableSupporter

type UndefinableSupporter interface {
	// contains filtered or unexported methods
}

type UndefinableUuid

type UndefinableUuid struct {
	Undefinable[Uuid]
}

func NewUndefinableUuid

func NewUndefinableUuid(uuid Uuid) (UndefinableUuid, error)

func NewUndefinableUuidOrPanic

func NewUndefinableUuidOrPanic(uuid Uuid) UndefinableUuid

func (UndefinableUuid) Equals

func (t UndefinableUuid) Equals(compT UndefinableUuid) bool

type UndefinableUuids

type UndefinableUuids struct {
	Undefinable[Uuids]
}

type Uuid

type Uuid string

func NewUuid

func NewUuid(value string) (Uuid, error)

func NewUuidOrPanic

func NewUuidOrPanic(value string) Uuid

func NewUuidV4OrPanic

func NewUuidV4OrPanic() Uuid

func (Uuid) Equals

func (t Uuid) Equals(compT Uuid) bool

func (Uuid) IsValid

func (t Uuid) IsValid() bool

func (Uuid) MarshalJSON

func (t Uuid) MarshalJSON() ([]byte, error)

func (Uuid) PanicWhenInvalid

func (t Uuid) PanicWhenInvalid()

func (Uuid) String

func (t Uuid) String() string

func (Uuid) ToLowerCaseUuidString

func (t Uuid) ToLowerCaseUuidString() string

func (Uuid) ToNullableUuid

func (t Uuid) ToNullableUuid() NullableUuid

func (Uuid) ToUndefinableNullableUuid

func (t Uuid) ToUndefinableNullableUuid() UndefinableNullableUuid

func (Uuid) ToUndefinableUuid

func (t Uuid) ToUndefinableUuid() UndefinableUuid

func (Uuid) ToUuids

func (t Uuid) ToUuids() Uuids

func (*Uuid) UnmarshalJSON

func (t *Uuid) UnmarshalJSON(data []byte) error

type Uuids

type Uuids []Uuid

func (Uuids) Contains

func (t Uuids) Contains(uuidToFind Uuid) bool

func (Uuids) Count

func (t Uuids) Count() int

func (Uuids) Equals

func (t Uuids) Equals(compT Uuids) bool

func (Uuids) MarshalJSON

func (t Uuids) MarshalJSON() ([]byte, error)

func (Uuids) String

func (t Uuids) String() string

func (Uuids) ToLowercaseUuidStrings

func (t Uuids) ToLowercaseUuidStrings() []string

func (Uuids) ToUndefinableUuids

func (t Uuids) ToUndefinableUuids() UndefinableUuids

func (*Uuids) UnmarshalJSON

func (t *Uuids) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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