Catalogs - bali-nebula/go-component-framework GitHub Wiki
Overview
A catalog is a collection of key-value pairs that preserves the order of the pairs as they are added and manipulated. Each key is a primitive type and each value is a component. The keys in a catalog must be unique but the values need not be. The key-value pairs in a catalog may be sorted into their natural ordering based on their keys.
A Quick Example
To whet your appetite, here is some short example code that demonstrates the behavior of catalogs.
package main
import (
fmt "fmt"
bal "github.com/bali-nebula/go-component-framework/v2/bali"
)
func main() {
// Create a new catalog from a string containing Bali Document Notation™.
var catalog = bal.Catalog(`[
$alpha: 1
$beta: 2
$gamma: 3
]`)
fmt.Println("The initial catalog:", bal.FormatEntity(catalog))
// Add some more values to the catalog.
catalog.SetValue(bal.Symbol(`$delta`), bal.Component(4))
catalog.SetValue(bal.Symbol(`$epsilon`), bal.Component(5))
fmt.Println("The augmented catalog:", bal.FormatEntity(catalog))
// Change a value in the catalog.
catalog.SetValue(bal.Symbol(`$delta`), bal.Component(6))
fmt.Println("The updated catalog:", bal.FormatEntity(catalog))
// Sort the values in the catalog.
catalog.SortValues()
fmt.Println("The sorted catalog:", bal.FormatEntity(catalog))
// Randomly shuffle the values in the catalog.
catalog.ShuffleValues()
fmt.Println("The shuffled catalog:", bal.FormatEntity(catalog))
// Remove a value from the catalog.
catalog.RemoveValue(bal.Symbol(`$delta`))
fmt.Println("The shortened catalog:", bal.FormatEntity(catalog))
// Remove all values from the catalog.
catalog.RemoveAll()
fmt.Println("The empty catalog:", bal.FormatEntity(catalog))
}