rtype

package
v0.0.0-...-2cda445 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RTypeString = &BaseType{
		Tag: "string",
		Parse: func(in string) (any, error) {
			return strings.Clone(in), nil
		},
	}

	RTypeInt64 = &BaseType{
		Tag: "int64",
		Parse: func(in string) (any, error) {
			if in == "" {
				return nil, ErrNoInput
			}

			n, err := strconv.ParseInt(in, 10, 64)
			if err != nil {
				numErr := err.(*strconv.NumError)
				if errors.Is(numErr.Err, strconv.ErrSyntax) {
					return nil, ErrMalformed
				}
				if errors.Is(numErr.Err, strconv.ErrRange) {
					return nil, ErrOutOfRange
				}
				panic("unexpected error from strconv.ParseInt()")
			}
			return n, nil
		},
	}

	RTypeFloat64 = &BaseType{
		Tag: "float64",
		Parse: func(in string) (any, error) {
			if in == "" {
				return nil, ErrNoInput
			}

			n, err := strconv.ParseFloat(in, 64)
			if err != nil {
				numErr := err.(*strconv.NumError)
				if errors.Is(numErr.Err, strconv.ErrSyntax) {
					return nil, ErrMalformed
				}
				if errors.Is(numErr.Err, strconv.ErrRange) {
					return nil, ErrOutOfRange
				}
				panic("unexpected error from strconv.ParseInt()")
			}
			return n, nil
		},
	}

	RTypeBool = &BaseType{
		Tag: "boolean",
		Parse: func(in string) (any, error) {
			switch strings.ToLower(in) {
			case "":
				return nil, ErrNoInput
			case "true":
				return true, nil
			case "false":
				return false, nil
			default:
				return nil, fmt.Errorf("expected true|false: %w", ErrMalformed)
			}
		},
	}

	RTypeUUID = &BaseType{
		Tag: "uuid",
		Parse: func(in string) (any, error) {
			if in == "" {
				return nil, ErrNoInput
			}
			uuid, err := uuid.FromString(in)
			if err != nil {
				return nil, errors.Join(err, ErrMalformed)
			}
			return uuid, nil
		},
	}

	RTypeULID = &BaseType{
		Tag: "ulid",
		Parse: func(in string) (any, error) {
			if in == "" {
				return nil, ErrNoInput
			}
			ulid, err := ulid.Parse(in)
			if err != nil {
				return nil, errors.Join(err, ErrMalformed)
			}
			return ulid, nil
		},
	}

	RTypeType = &BaseType{
		Tag: "type",
		Parse: func(in string) (any, error) {
			if in == "" {
				return nil, ErrNoInput
			}
			return Parse(in)
		},
	}

	RTypeNull = nullType{}

	RTypeListGen = &GenericType{
		Tag: "list",
		Parameters: []TypeParameter{
			{
				Name: "elem",
				Type: RTypeType,
			},
		},
		Instantiate: func(params map[string]any) (ValueParser, error) {
			elem := params["elem"].(ConcreteType)
			return NewRTypeList(elem), nil
		},
	}

	RTypeDecimalGen = &GenericType{
		Tag: "decimal",
		Parameters: []TypeParameter{
			{
				Name: "precision",
				Type: RTypeInt64,
			},
			{
				Name:         "scale",
				Type:         RTypeInt64,
				DefaultValue: int64(0),
			},
		},
		Instantiate: func(params map[string]any) (ValueParser, error) {
			precision := params["precision"].(int64)
			scale := params["scale"].(int64)
			if precision < 0 || precision > math.MaxUint8 {
				return nil, fmt.Errorf("decimal.precision out of range (0, %d): %d", math.MaxUint8, precision)
			}

			if scale < 0 || scale > math.MaxUint8 {
				return nil, fmt.Errorf("decimal.scale out of range (0, %d): %d", math.MaxUint8, scale)
			}

			return &RTypeDecimal{
				precision: uint8(precision),
				scale:     uint8(scale),
			}, nil
		},
	}
)
View Source
var (
	ErrNoInput          = errors.New("no input provided")
	ErrMalformed        = errors.New("input malformed")
	ErrOutOfRange       = errors.New("input out of range")
	ErrValidationFailed = errors.New("input validation failed")
)
View Source
var (
	RTypeIRI = NewValidatedType("iri", RTypeString, validateIRI)
)

Functions

func Encode

func Encode(ct ConcreteType) string

func MustRegister

func MustRegister(t ConcreteType)

func MustRegisterGeneric

func MustRegisterGeneric(t *GenericType)

func NewAliasType

func NewAliasType(tag string, inner ConcreteType) *aliasType

func NewBooleanLiteral

func NewBooleanLiteral(val bool) *booleanLiteral

func NewFloat64Literal

func NewFloat64Literal(val float64) *float64Literal

func NewInt64Literal

func NewInt64Literal(val int64) *int64Literal

func NewStringLiteral

func NewStringLiteral(val string) *stringLiteral

func NewValidatedType

func NewValidatedType(tag string, inner ConcreteType, validate func(any) error) *validatedType

func Register

func Register(t ConcreteType) error

func RegisterGeneric

func RegisterGeneric(t *GenericType) error

Types

type BaseType

type BaseType struct {
	Tag   string
	Parse func(string) (any, error)
}

BaseType is a ConcreteType that does not have any type parameters.

func (BaseType) ParseString

func (t BaseType) ParseString(in string) (any, error)

func (BaseType) TypeTag

func (t BaseType) TypeTag() string

type ConcreteType

type ConcreteType interface {
	ValueParser
	TypeTag() string
	// contains filtered or unexported methods
}

ConcreteType is a "useable" type. That is, it represents a BaseType or a parameterized type that has been instantiated from a generic type.

func Lookup

func Lookup(tag string) (ConcreteType, bool)

func MustParse

func MustParse(typeStr string) ConcreteType

func Parse

func Parse(typeStr string) (ConcreteType, error)

func RootType

func RootType(t ConcreteType) ConcreteType

func Unwrap

func Unwrap(ct ConcreteType) ConcreteType

type GenericType

type GenericType struct {
	// Tag is the tag used to register this type in a Registry. It must match
	// the Tag() of concrete types produced by calling Parameterize().
	Tag string

	// Instantiate gets a concrete type given a map of validated type
	// parameters that have been validated according to Parameters.
	Instantiate func(params map[string]any) (ValueParser, error)

	// Parameters describes the parameter that should be passed to Instantiate().
	Parameters []TypeParameter
}

func LookupGeneric

func LookupGeneric(tag string) (*GenericType, bool)

type ParameterizedType

type ParameterizedType struct {
	ValueParser
	// contains filtered or unexported fields
}

ParameterizedType is a ConcreteType that was constructed by a GenericType with specific parameters. This type is private because it is designed to only be constructed using generic types

func InstantiateParameterized

func InstantiateParameterized(g *GenericType, params map[string]any) (*ParameterizedType, error)

func (*ParameterizedType) GetParams

func (t *ParameterizedType) GetParams() map[string]any

func (*ParameterizedType) TypeTag

func (t *ParameterizedType) TypeTag() string

type RTypeContainer

type RTypeContainer struct {
	ConcreteType
}

RTypeContainer is a concrete type that wraps the ConcreteType interface type to provide a concrete type that implements a number of interfaces for marshaling to and from other representations.

func MustNewRTypeContainer

func MustNewRTypeContainer(str string) RTypeContainer

func RTypeContainerFrom

func RTypeContainerFrom(ct ConcreteType) *RTypeContainer

func (RTypeContainer) MarshalText

func (rt RTypeContainer) MarshalText() ([]byte, error)

func (*RTypeContainer) Scan

func (rt *RTypeContainer) Scan(src any) error

func (*RTypeContainer) String

func (rt *RTypeContainer) String() string

func (*RTypeContainer) UnmarshalText

func (rt *RTypeContainer) UnmarshalText(data []byte) error

func (*RTypeContainer) Value

func (rt *RTypeContainer) Value() (driver.Value, error)

type RTypeDecimal

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

func (RTypeDecimal) ParseString

func (t RTypeDecimal) ParseString(in string) (any, error)

type RTypeList

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

func NewRTypeList

func NewRTypeList(elem ConcreteType) *RTypeList

func (RTypeList) ParseString

func (t RTypeList) ParseString(in string) (any, error)

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) Lookup

func (r *Registry) Lookup(tag string) (ConcreteType, bool)

func (*Registry) LookupGeneric

func (r *Registry) LookupGeneric(tag string) (*GenericType, bool)

func (*Registry) Register

func (r *Registry) Register(t ConcreteType) error

func (*Registry) RegisterGeneric

func (r *Registry) RegisterGeneric(t *GenericType) error

type TypeParameter

type TypeParameter struct {
	Name         string
	Type         ConcreteType
	DefaultValue any
}

type UnionType

type UnionType struct {
	Variants []ConcreteType
}

UnionType is a list of types that a particular value could take on.

func NewUnionType

func NewUnionType(variants ...ConcreteType) *UnionType

func (UnionType) ParseString

func (t UnionType) ParseString(in string) (any, error)

func (UnionType) TypeTag

func (t UnionType) TypeTag() string

type ValueParser

type ValueParser interface {
	// ParseString parses an input string and produces a typed value or a parse
	// error.
	ParseString(in string) (any, error)
}

Jump to

Keyboard shortcuts

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