Documentation
¶
Index ¶
- func AtomsToGob(atoms []AtomInterface) ([]byte, error)
- func AtomsToJSON(atoms []AtomInterface) (string, error)
- func AtomsToMap(atoms []AtomInterface) []map[string]any
- type Atom
- func (a *Atom) ChildAdd(child AtomInterface) AtomInterface
- func (a *Atom) ChildDeleteByID(id string) AtomInterface
- func (a *Atom) ChildFindByID(id string) AtomInterface
- func (a *Atom) ChildrenAdd(children []AtomInterface) AtomInterface
- func (a *Atom) ChildrenFindByType(atomType string) []AtomInterface
- func (a *Atom) ChildrenGet() []AtomInterface
- func (a *Atom) ChildrenLength() int
- func (a *Atom) ChildrenSet(children []AtomInterface) AtomInterface
- func (a *Atom) FromGob(data []byte) error
- func (a *Atom) Get(key string) string
- func (a *Atom) GetAll() map[string]string
- func (a *Atom) GetID() string
- func (a *Atom) GetType() string
- func (a *Atom) GobDecode(data []byte) error
- func (a *Atom) GobEncode() ([]byte, error)
- func (a *Atom) Has(key string) bool
- func (a *Atom) MemoryUsage() int
- func (a *Atom) Remove(key string) AtomInterface
- func (a *Atom) Set(key, value string) AtomInterface
- func (a *Atom) SetAll(properties map[string]string) AtomInterface
- func (a *Atom) SetID(id string) AtomInterface
- func (a *Atom) SetType(atomType string) AtomInterface
- func (a *Atom) ToGob() ([]byte, error)
- func (a *Atom) ToJSON() (string, error)
- func (a *Atom) ToJSONPretty() (string, error)
- func (a *Atom) ToMap() map[string]interface{}
- type AtomInterface
- func FindAtomByID(root AtomInterface, id string) AtomInterface
- func FindAtomsByType(root AtomInterface, atomType string) []AtomInterface
- func FindFirstAtomByType(root AtomInterface, atomType string) AtomInterface
- func GobToAtom(data []byte) (AtomInterface, error)
- func GobToAtoms(data []byte) ([]AtomInterface, error)
- func JSONToAtom(jsonStr string) (AtomInterface, error)
- func JSONToAtoms(atomsJson string) ([]AtomInterface, error)
- func MapToAtom(atomMap map[string]any) (AtomInterface, error)
- func MapToAtoms(atoms []map[string]any) []AtomInterface
- func NewAtom(atomType string, opts ...AtomOption) AtomInterface
- func NewAtomFromGob(data []byte) (AtomInterface, error)
- func NewAtomFromJSON(jsonStr string) (AtomInterface, error)
- func NewAtomFromMap(atomMap map[string]any) (AtomInterface, error)
- type AtomOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtomsToGob ¶ added in v0.3.0
func AtomsToGob(atoms []AtomInterface) ([]byte, error)
AtomsToGob encodes a slice of AtomInterface to binary data using the gob package. It encodes each atom using its ToGob method and collects the results.
Business logic: - Handles nil or empty input by returning an empty byte slice - Encodes each atom using its ToGob method - Collects the encoded data for all atoms
Parameters:
- atoms: slice of AtomInterface to encode
Returns:
- []byte: gob-encoded binary data
- error: if encoding fails
func AtomsToJSON ¶ added in v0.3.0
func AtomsToJSON(atoms []AtomInterface) (string, error)
AtomsToJSON converts a slice of AtomInterface to a JSON string.
Business logic: - Handles nil input by returning an empty array JSON string - Converts each non-nil atom to a JSON object using ToMap() - Returns a JSON array of atom objects
Returns: - string: JSON-encoded array of atoms - error: if marshaling to JSON fails
func AtomsToMap ¶ added in v0.3.0
func AtomsToMap(atoms []AtomInterface) []map[string]any
AtomsToMap converts a slice of AtomInterface to a slice of maps.
Business logic: - Handles nil input by returning nil - Skips nil atoms in the input - Converts each atom to a map using ToMap() - Only includes non-nil results in the output
Parameters:
- atoms: slice of AtomInterface to convert
Returns:
- []map[string]any: slice of atom maps (never contains nils)
Types ¶
type Atom ¶
type Atom struct {
// contains filtered or unexported fields
}
Atom is the main implementation of AtomInterface using map[string]string for properties. All properties are stored as strings, and children are stored as a slice of AtomInterface.
There are two special properties: - "id": the unique identifier of the atom - "type": the type of the atom
func FromGob ¶ added in v0.4.0
FromGob decodes an Atom from gob-encoded data. This is a helper function that creates a new Atom and calls FromGob on it.
func (*Atom) ChildAdd ¶ added in v0.4.0
func (a *Atom) ChildAdd(child AtomInterface) AtomInterface
ChildAdd adds a child atom. If child is nil, it's a no-op.
func (*Atom) ChildDeleteByID ¶ added in v0.4.0
func (a *Atom) ChildDeleteByID(id string) AtomInterface
ChildDeleteByID removes a child atom by its ID.
func (*Atom) ChildFindByID ¶ added in v0.5.0
func (a *Atom) ChildFindByID(id string) AtomInterface
ChildFindByID returns the first immediate child with the given ID, or nil if not found.
func (*Atom) ChildrenAdd ¶ added in v0.4.0
func (a *Atom) ChildrenAdd(children []AtomInterface) AtomInterface
ChildrenAdd adds multiple child atoms.
func (*Atom) ChildrenFindByType ¶ added in v0.5.0
func (a *Atom) ChildrenFindByType(atomType string) []AtomInterface
ChildrenFindByType returns all immediate children that match the provided type.
func (*Atom) ChildrenGet ¶ added in v0.4.0
func (a *Atom) ChildrenGet() []AtomInterface
ChildrenGet returns a copy of the children slice.
func (*Atom) ChildrenLength ¶ added in v0.4.0
ChildrenLength returns the number of children.
func (*Atom) ChildrenSet ¶ added in v0.4.0
func (a *Atom) ChildrenSet(children []AtomInterface) AtomInterface
ChildrenSet replaces all children with the given slice. Nil children in the input slice will be filtered out.
func (*Atom) FromGob ¶ added in v0.4.0
FromGob decodes the atom from gob-encoded data. This method satisfies the AtomInterface requirement.
func (*Atom) GobDecode ¶ added in v0.4.0
GobDecode implements the gob.GobDecoder interface. This is a wrapper around FromGob for compatibility with the gob package.
func (*Atom) GobEncode ¶ added in v0.4.0
GobEncode implements the gob.GobEncoder interface. This is a wrapper around ToGob for compatibility with the gob package.
func (*Atom) MemoryUsage ¶ added in v0.4.0
MemoryUsage returns the estimated memory usage of the atom in bytes, including all its properties and recursively all its children. This is useful for memory profiling and monitoring. Note: This is an approximation and doesn't account for all memory used by the Go runtime.
func (*Atom) Remove ¶ added in v0.4.0
func (a *Atom) Remove(key string) AtomInterface
Remove removes the value for the given key.
func (*Atom) Set ¶ added in v0.4.0
func (a *Atom) Set(key, value string) AtomInterface
Set sets the value for the given key.
func (*Atom) SetAll ¶ added in v0.4.0
func (a *Atom) SetAll(properties map[string]string) AtomInterface
SetAll sets all properties of the atom.
func (*Atom) SetType ¶
func (a *Atom) SetType(atomType string) AtomInterface
SetType sets the atom's type.
func (*Atom) ToGob ¶ added in v0.3.0
ToGob encodes the atom to a gob-encoded byte slice. This is the primary method for gob encoding that satisfies the AtomInterface.
func (*Atom) ToJSONPretty ¶ added in v0.4.0
ToJSONPretty converts the atom to a nicely indented JSON string.
type AtomInterface ¶
type AtomInterface interface { // ID returns the unique identifier of the atom GetID() string SetID(id string) AtomInterface // Type returns the type of the atom GetType() string SetType(atomType string) AtomInterface // Property access Get(key string) string Has(key string) bool Remove(key string) AtomInterface Set(key, value string) AtomInterface GetAll() map[string]string SetAll(properties map[string]string) AtomInterface // Children management ChildAdd(child AtomInterface) AtomInterface ChildDeleteByID(id string) AtomInterface ChildFindByID(id string) AtomInterface ChildrenAdd(children []AtomInterface) AtomInterface ChildrenFindByType(atomType string) []AtomInterface ChildrenGet() []AtomInterface ChildrenSet(children []AtomInterface) AtomInterface ChildrenLength() int // Serialization ToMap() map[string]any ToJSON() (string, error) ToJSONPretty() (string, error) ToGob() ([]byte, error) // MemoryUsage returns the estimated memory usage of the atom in bytes, // including all its properties and recursively all its children. // This is useful for memory profiling and monitoring. MemoryUsage() int }
AtomInterface is the universal interface that all composable primitives must satisfy. It defines the methods necessary for the system to understand and process any atom, regardless of its specific type.
func FindAtomByID ¶ added in v0.5.0
func FindAtomByID(root AtomInterface, id string) AtomInterface
FindAtomByID recursively finds an atom by ID in a tree. It performs a pre-order traversal: checks the current node first, then descends into its children in order.
func FindAtomsByType ¶ added in v0.5.0
func FindAtomsByType(root AtomInterface, atomType string) []AtomInterface
FindAtomsByType recursively finds all atoms of a specific type in a tree. It performs a pre-order traversal and returns matches in that order.
func FindFirstAtomByType ¶ added in v0.5.0
func FindFirstAtomByType(root AtomInterface, atomType string) AtomInterface
FindAtomByType recursively finds the first atom with the given type in a tree. It performs a pre-order traversal: checks the current node first, then its children in order.
func GobToAtom ¶ added in v0.3.0
func GobToAtom(data []byte) (AtomInterface, error)
GobToAtom decodes an atom from gob-encoded data.
Business logic: - Decodes the binary data into a temporary struct - Creates a new atom with the decoded type, id, and properties - Recursively decodes and adds child atoms - Returns an error if the data is invalid
Parameters:
- data: binary data containing the gob-encoded atom
Returns:
- AtomInterface: the decoded atom
- error: if decoding fails
func GobToAtoms ¶ added in v0.3.0
func GobToAtoms(data []byte) ([]AtomInterface, error)
GobToAtoms decodes multiple atoms from binary data encoded with the gob package. It decodes the data in the format written by AtomsToGob.
Business logic: - Handles empty or nil input by returning an empty slice - Validates the input data structure - Decodes the count of atoms first - Then decodes each atom's data and validates it before conversion - Preserves the order of atoms from the encoded data
Parameters:
- data: binary data containing gob-encoded atoms
Returns:
- []AtomInterface: slice of decoded atoms
- error: if the data cannot be decoded or is invalid
func JSONToAtom ¶ added in v0.4.0
func JSONToAtom(jsonStr string) (AtomInterface, error)
JSONToAtom converts a JSON string to a single Atom.
Business logic: - Handles empty input by returning an error - Validates JSON structure before processing - Converts JSON object to an Atom using MapToAtom
Parameters:
- jsonStr: JSON string containing a single atom's data
Returns:
- AtomInterface: the parsed atom
- error: if JSON is invalid or missing required fields
func JSONToAtoms ¶ added in v0.3.0
func JSONToAtoms(atomsJson string) ([]AtomInterface, error)
JSONToAtoms converts a JSON string to a slice of AtomInterface.
Business logic: - Handles empty input by returning an empty slice - Supports both array of atoms and single atom object - Validates JSON structure before processing - Converts each JSON object to an Atom using MapToAtom
Parameters:
- atomsJson: JSON string containing atom data
Returns:
- []AtomInterface: slice of parsed atoms
- error: if JSON is invalid or missing required fields
func MapToAtom ¶ added in v0.3.0
func MapToAtom(atomMap map[string]any) (AtomInterface, error)
MapToAtom converts a map to an AtomInterface.
The map must represent a valid atom with at least "id" and "type" fields. Properties should be in a nested "properties" map, and children in a "children" slice. For backward compatibility, top-level properties are also supported but not recommended.
Parameters:
- atomMap: map containing the atom data
Returns:
- AtomInterface: the converted atom
- error: if the map is not a valid atom
func MapToAtoms ¶ added in v0.3.0
func MapToAtoms(atoms []map[string]any) []AtomInterface
MapToAtoms converts a slice of atom maps to a slice of AtomInterface.
Business logic: - Handles nil input by returning nil - Preserves nil elements in the output for nil inputs - Silently ignores errors from NewAtomFromMap (for backward compatibility) - Maintains the order of atoms from input to output
Note: This function is provided for convenience and backward compatibility. For better error handling, consider using NewAtomFromMap directly.
Parameters:
- atoms: slice of maps containing atom data
Returns:
- []AtomInterface: slice of converted atoms (may contain nils)
func NewAtom ¶
func NewAtom(atomType string, opts ...AtomOption) AtomInterface
NewAtom creates a new Atom with the given type and applies the provided options. If no ID is provided via options, a human-readable UID will be generated. Returns an AtomInterface to maintain consistency with other constructors.
func NewAtomFromGob ¶ added in v0.3.0
func NewAtomFromGob(data []byte) (AtomInterface, error)
NewAtomFromGob creates a new Atom from binary data encoded with the gob package. This is a convenience function that delegates to GobToAtom.
Example:
data, _ := atom.ToGob() newAtom, err := NewAtomFromGob(data)
Parameters:
- data: binary data containing the gob-encoded atom
Returns:
- AtomInterface: the decoded atom
- error: if the data is invalid or cannot be decoded
func NewAtomFromJSON ¶ added in v0.3.0
func NewAtomFromJSON(jsonStr string) (AtomInterface, error)
NewAtomFromJSON creates a new Atom from a JSON string. This is a convenience function that delegates to JSONToAtom.
The JSON should be an object with at least "id" and "type" fields. Properties should be in a "properties" map, and children in a "children" array.
Parameters:
- jsonStr: JSON string containing the atom data
Returns:
- AtomInterface: the parsed atom
- error: if the JSON is invalid or missing required fields
Example:
jsonStr := `{"id":"atom1","type":"test","properties":{"key":"value"}}` atom, err := NewAtomFromJSON(jsonStr)
Note: For parsing multiple atoms from a JSON array, use JSONToAtoms instead.
func NewAtomFromMap ¶ added in v0.3.0
func NewAtomFromMap(atomMap map[string]any) (AtomInterface, error)
NewAtomFromMap creates a new Atom from a map. This is a convenience function that delegates to MapToAtom.
The map should contain at least "id" and "type" fields. Properties should be in a "properties" map, and children in a "children" slice.
Parameters:
- atomMap: map containing the atom data
Returns:
- AtomInterface: the created atom
- error: if the map is missing required fields or is invalid
Example:
atomMap := map[string]any{ "id": "atom1", "type": "test", "properties": map[string]string{"key": "value"}, } atom, err := NewAtomFromMap(atomMap)
type AtomOption ¶ added in v0.3.0
type AtomOption func(*Atom)
AtomOption configures an Atom.
func WithChildren ¶ added in v0.3.0
func WithChildren(children ...AtomInterface) AtomOption
WithChildren adds child atoms to the Atom.
func WithData ¶ added in v0.4.0
func WithData(data map[string]string) AtomOption
WithData adds initial data to the Atom. This is a convenience function for setting multiple key-value pairs at once.
func WithProperties ¶ added in v0.3.0
func WithProperties(properties map[string]string) AtomOption
WithProperties adds properties to the Atom. Note: This will not set 'id' or 'type' as they are now direct fields.
func WithType ¶ added in v0.4.0
func WithType(atomType string) AtomOption
WithType sets the type of the Atom.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
advanced
command
Advanced example demonstrating concurrent access and property management
|
Advanced example demonstrating concurrent access and property management |
basic
command
Basic example demonstrating the core functionality of the omni package.
|
Basic example demonstrating the core functionality of the omni package. |
book
command
Book example demonstrating a hierarchical book structure with pages and content.
|
Book example demonstrating a hierarchical book structure with pages and content. |
website
command
Website example demonstrating a simple website structure with pages and content.
|
Website example demonstrating a simple website structure with pages and content. |