Documentation
¶
Overview ¶
Package utils provides general-purpose utilities for JSON handling and panic recovery.
Index ¶
- Variables
- func ConvertIntPtr[From constraints.Integer, To constraints.Integer](ptr *From) *To
- func JSONFromMarkdown(content string) string
- func NoPanic(fn func() error) (err error)
- func RepairTextJSON(content string) (string, error)
- func SortedKeys[K cmp.Ordered, V any](m map[K]V) (keys []K)
- func SplitLines(text string) []string
- func ToLines(value interface{}) []string
- func ToString(value interface{}) string
- func ValidateAgainstSchema(schema map[string]interface{}, values ...interface{}) error
- type StringSet
- type ValueSet
- func (v ValueSet) Any(condition func(interface{}) bool) bool
- func (v ValueSet) AsStringSet() (StringSet, bool)
- func (v ValueSet) Map(transform func(interface{}) interface{}) ValueSet
- func (v ValueSet) MarshalYAML() (interface{}, error)
- func (v *ValueSet) UnmarshalYAML(value *yaml.Node) error
- func (v ValueSet) Values() []interface{}
Constants ¶
This section is empty.
Variables ¶
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") )
var ErrInvalidStringSetValue = errors.New("invalid string-set value")
ErrInvalidStringSetValue indicates invalid StringSet definition.
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 ¶
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 ¶
NoPanic executes the provided function and recovers from any panic by converting it to error if that occurs.
func RepairTextJSON ¶
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
SortedKeys returns a sorted slice of map keys.
func SplitLines ¶ added in v0.6.1
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
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
NewStringSet creates a new StringSet from the given items, discarding duplicates.
func (StringSet) Any ¶ added in v0.2.0
Any returns true if any value in the set satisfies the given condition.
func (StringSet) Map ¶ added in v0.2.0
Map returns a new StringSet with f applied to each value, discarding duplicates.
func (StringSet) MarshalYAML ¶ added in v0.2.0
MarshalYAML implements YAML marshaling for StringSet.
func (*StringSet) UnmarshalYAML ¶ added in v0.2.0
UnmarshalYAML allows StringSet to be loaded from either a string or a list of strings.
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
Any returns true if any value in the set satisfies the given condition.
func (ValueSet) AsStringSet ¶ added in v0.9.0
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
Map applies a transformation function to all values and returns a new ValueSet with duplicates removed.
func (ValueSet) MarshalYAML ¶ added in v0.9.0
MarshalYAML implements custom YAML marshaling for ValueSet.
func (*ValueSet) UnmarshalYAML ¶ added in v0.9.0
UnmarshalYAML implements custom YAML unmarshaling for ValueSet.