helpers

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeAs added in v0.1.0

func DecodeAs[T any](value any) (T, bool)

DecodeAs attempts to cast a value of type any to a specific target type T.

Returns the value and true if successful, or the zero value of T and false otherwise.

func MustRaw

func MustRaw(v any) cbor.RawMessage

MustRaw serializes the given value into CBOR format and returns it as a RawMessage.

This function panics if marshaling fails. Suitable for internal use where inputs are trusted.

func WrapHandler added in v0.1.0

func WrapHandler[In any, Out any](fn func(In) (Out, error)) func(any) (any, error)

WrapHandler adapts a strongly-typed handler function into a generic handler that can be registered in PlugKit's message system.

It expects input in the form of an `any` value, typically representing CBOR-decoded data. If the input is not in the expected strongly-typed form, it is re-encoded to CBOR and then decoded into the expected type. This provides compatibility with inputs that were decoded generically (e.g., into map[interface{}]interface{} or map[string]any).

This pattern ensures type safety on the handler side, while remaining flexible for various decoding strategies used within the runtime.

This function performs an indirect CBOR remarshal to transform dynamically-typed data into a statically typed value. While effective and safe, the mechanism is not idiomatic Go. It is recommended for internal use in PlugKit only, to simplify handler registration. Example:

func HandlePing(p shared.Ping) (bool, error) { ... }
client.HandleMessageType("ping", WrapHandler(HandlePing))

Types

This section is empty.

Jump to

Keyboard shortcuts

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