utils

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package utils provides general-purpose utilities for JSON handling and panic recovery.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecoveredPanic is returned when a panic is recovered and converted to an error.
	ErrRecoveredPanic = errors.New("recovered panic")

	// ErrCouldNotRepairJSON is returned when malformed JSON cannot be automatically fixed.
	ErrCouldNotRepairJSON = errors.New("malformed JSON could not be repaired")

	// ErrInvalidJSONSchema is returned when the provided JSON schema is invalid.
	ErrInvalidJSONSchema = errors.New("schema structure or keywords do not conform to the JSON Schema standard")

	// ErrJSONSchemaValidation is returned when JSON schema validation fails.
	ErrJSONSchemaValidation = errors.New("data format does not meet the defined schema requirements")
)
View Source
var ErrInvalidStringSetValue = errors.New("invalid string-set value")

ErrInvalidStringSetValue indicates invalid StringSet definition.

View Source
var ErrInvalidValueSetValue = errors.New("invalid set value")

ErrInvalidValueSetValue indicates invalid ValueSet definition.

Functions

func ConvertIntPtr added in v0.10.1

func ConvertIntPtr[From constraints.Integer, To constraints.Integer](ptr *From) *To

ConvertIntPtr converts a pointer of any integer type to a pointer of another integer type. If the input pointer is nil, returns nil. Otherwise, returns a pointer to the converted value.

Warning: Conversions between different integer types may result in data loss or overflow. The conversion follows Go's standard truncation behavior (e.g., uint64(2^32) -> int32(0)).

func JSONFromMarkdown

func JSONFromMarkdown(content string) string

JSONFromMarkdown extracts JSON content from a given Markdown string. If a JSON block is found, it returns the JSON content as a string. If no JSON block is found, it returns the original content.

func NoPanic

func NoPanic(fn func() error) (err error)

NoPanic executes the provided function and recovers from any panic by converting it to error if that occurs.

func RepairTextJSON

func RepairTextJSON(content string) (string, error)

RepairTextJSON attempts to repair common issues with plain-text JSON generated by LLMs. If the input is already valid JSON, it is returned as-is.

func SortedKeys added in v0.9.0

func SortedKeys[K cmp.Ordered, V any](m map[K]V) (keys []K)

SortedKeys returns a sorted slice of map keys.

func SplitLines added in v0.6.1

func SplitLines(text string) []string

SplitLines splits text by newlines supporting both \n and \r\n and preserves empty lines. Returns an empty slice for empty input.

func ToLines added in v0.9.0

func ToLines(value interface{}) []string

ToLines converts a value to []string for display. For string values, splits them into lines. For objects, converts to pretty-printed JSON and splits into lines.

func ToString added in v0.9.0

func ToString(value interface{}) string

ToString converts a value to a string for display purposes. For string values, returns as-is. For objects, converts to pretty-printed JSON with indentation.

func ValidateAgainstSchema added in v0.9.0

func ValidateAgainstSchema(schema map[string]interface{}, values ...interface{}) error

ValidateAgainstSchema validates that one or more values conform to the given JSON schema. The schema is compiled and validated exactly once.

Types

type StringSet added in v0.2.0

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

StringSet represents a set of unique string values.

func NewStringSet added in v0.2.0

func NewStringSet(items ...string) StringSet

NewStringSet creates a new StringSet from the given items, discarding duplicates.

func (StringSet) Any added in v0.2.0

func (s StringSet) Any(condition func(string) bool) bool

Any returns true if any value in the set satisfies the given condition.

func (StringSet) Map added in v0.2.0

func (s StringSet) Map(f func(string) string) StringSet

Map returns a new StringSet with f applied to each value, discarding duplicates.

func (StringSet) MarshalYAML added in v0.2.0

func (s StringSet) MarshalYAML() (interface{}, error)

MarshalYAML implements YAML marshaling for StringSet.

func (*StringSet) UnmarshalYAML added in v0.2.0

func (s *StringSet) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML allows StringSet to be loaded from either a string or a list of strings.

func (StringSet) Values added in v0.2.0

func (s StringSet) Values() []string

Values returns a copy of the set's values.

type ValueSet added in v0.9.0

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

ValueSet represents a set of unique values. The type is generic and can handle both string values and object values.

func NewValueSet added in v0.9.0

func NewValueSet(items ...interface{}) ValueSet

NewValueSet creates a new ValueSet from the given items, discarding duplicates.

func (ValueSet) Any added in v0.9.0

func (v ValueSet) Any(condition func(interface{}) bool) bool

Any returns true if any value in the set satisfies the given condition.

func (ValueSet) AsStringSet added in v0.9.0

func (v ValueSet) AsStringSet() (StringSet, bool)

AsStringSet returns the values as a StringSet if they are all strings. Returns (values, true) if all values are strings, (empty StringSet, false) otherwise.

func (ValueSet) Map added in v0.9.0

func (v ValueSet) Map(transform func(interface{}) interface{}) ValueSet

Map applies a transformation function to all values and returns a new ValueSet with duplicates removed.

func (ValueSet) MarshalYAML added in v0.9.0

func (v ValueSet) MarshalYAML() (interface{}, error)

MarshalYAML implements custom YAML marshaling for ValueSet.

func (*ValueSet) UnmarshalYAML added in v0.9.0

func (v *ValueSet) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements custom YAML unmarshaling for ValueSet.

func (ValueSet) Values added in v0.9.0

func (v ValueSet) Values() []interface{}

Values returns a copy of the set's values.

Jump to

Keyboard shortcuts

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