You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
3.2 KiB

// =============================================================================
// Active-GO Framework
// Copyright (c) 2025 ActiveThing (https://activething.com)
// Author: Juan V. Navarro juanvnl@activething.com
// =============================================================================
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n// THE SOFTWARE.\n//\n// =============================================================================\n// Date Create 16/05/2025\n// =============================================================================
package errs
type (
// Meta represents a single piece of metadata associated with an error.
// It consists of a name (key), a value, and a type.
Meta struct {
name string // The name or key of the metadata (e.g., "userId", "fileName")
value any // The value associated with the metadata (can be of any type)
mType MetaType // The type of the metadata value
}
)
// CreateMeta creates a new Meta instance.
// It validates the metadata name and type.
// Panics if the name or type are invalid.
func CreateMeta(name string, value any, metatype MetaType) Meta {
// The use of panic here indicates that the construction of these structures must be guaranteed
// before the program runs. An error in the name or type is indicative of a failure in the
// logic or static definition, so it doesn't make sense to handle it with an error
// that could be ignored or postponed.
if !IsValidMetaName(name) {
panic(E.InvalidMetaName)
}
if !IsValidMetaType(metatype) {
panic(E.InvalidMetaType)
}
return Meta{name, value, metatype}
}
// Name returns the name (key) of the metadata.
func (m Meta) Name() string { return m.name }
// Value returns the value of the metadata.
func (m Meta) Value() any { return m.value }
// Type returns the type of the metadata value.
func (m Meta) Type() MetaType { return m.mType }
// IsValidMetaName checks if a string is a valid metadata name according to the rules defined by metaValidator.
func IsValidMetaName(meta string) bool {
return len(meta) > 0 && g.metaValidator.MatchString(meta)
}
// IsValidMetaType checks if a MetaType value is valid.
func IsValidMetaType(metatype MetaType) bool { return metatype < MetaTypeInvalid }

Powered by TurnKey Linux.