genericsutil

package
v0.85.0-pre.1 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: BSD-3-Clause Imports: 0 Imported by: 0

README

kit/genericsutil

github.com/vormadev/vorma/kit/genericsutil

Generic helpers for zero values, fallback assertions, and type-erased generic pipelines.

Import

import "github.com/vormadev/vorma/kit/genericsutil"

Common Usage

Safe type assertion with fallback
v := any("42")
n := genericsutil.AssertOrZero[int](v) // 0 (assertion failed)

Use this when fallback-to-zero is intentional.
Avoid it when assertion failure should be explicit (otherwise bugs can be hidden).

Defaulting zero values
port := genericsutil.OrDefault(cfg.Port, 8080)
Generic zero helper for erased APIs
type Convert[I any, O any] struct {
	genericsutil.ZeroHelper[I, O]
}

var z genericsutil.AnyZeroHelper = Convert[int, string]{}
_ = z.I()    // any(0)
_ = z.O()    // any("")
_ = z.IPtr() // *int
_ = z.OPtr() // *string

None and IsNone

None is an alias of struct{}:

type Ack = genericsutil.None

IsNone returns true only for:

  • struct{}
  • *struct{}

Named empty-struct types are not considered None.

API Reference

  • type AnyZeroHelper
  • type None
  • type ZeroHelper[I any, O any]
  • func Zero[T any]() T
  • func AssertOrZero[T any](v any) T
  • func OrDefault[F comparable](field F, defaultVal F) F
  • func IsNone(v any) bool
  • func (ZeroHelper[I, O]) I() any
  • func (ZeroHelper[I, O]) O() any
  • func (ZeroHelper[I, O]) IPtr() any
  • func (ZeroHelper[I, O]) OPtr() any

Documentation

Overview

Package genericsutil provides helpers for implementing type erasure patterns

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertOrZero

func AssertOrZero[T any](v any) T

Returns v cast as type T if possible, otherwise returns the zero value of T

func IsNone

func IsNone(v any) bool

Returns true if v is either an empty struct or a pointer to an empty struct

func OrDefault

func OrDefault[F comparable](field F, defaultVal F) F

Returns field if it is not the zero value for its type, otherwise returns defaultVal

func Zero

func Zero[T any]() T

Returns the zero value of type T

Types

type AnyZeroHelper

type AnyZeroHelper interface {
	I() any    // returns direct I zero val
	O() any    // returns direct O zero val
	IPtr() any // returns `new(I)` (pointer to I)
	OPtr() any // returns `new(O)` (pointer to O)
}

type None

type None = struct{}

Simple alias for an empty struct

type ZeroHelper

type ZeroHelper[I any, O any] struct{}

func (ZeroHelper[I, O]) I

func (ZeroHelper[I, O]) I() any

func (ZeroHelper[I, O]) IPtr

func (ZeroHelper[I, O]) IPtr() any

func (ZeroHelper[I, O]) O

func (ZeroHelper[I, O]) O() any

func (ZeroHelper[I, O]) OPtr

func (ZeroHelper[I, O]) OPtr() any

Jump to

Keyboard shortcuts

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