Catalogs - craterdog/go-component-framework GitHub Wiki

Overview

A catalog is a collection of key-value pairs called associations that preserves the order of the associations as they are added and manipulated. Catalogs can be sorted by key using either the default (natural order) ranking function or a custom ranking function that is passed into the sort request.

UML Diagram

A Quick Example

To whet your appetite, here is some short example code that demonstrates the use of catalogs.

package main

import (
	fmt "fmt"
	fra "github.com/craterdog/go-component-framework/v7"
)

func main() {
	// Create a new catalog from a Go map using the module constructor shortcut.
	var catalog = fra.CatalogFromMap[string, int64](
		map[string]int64{
			"diamonds": 1,
			"hearts":   2,
			"spades":   3,
		},
	)
	// NOTE: Since the order of the associations in a Go map is indeterminate the
	// catalog constructor first sorts the map using the natural ordering of its
	// keys.
	fmt.Println(catalog)

	// Add a new association to the catalog.
	catalog.SetValue("clubs", 4)
	fmt.Println(catalog)

	// Sort the associations in the catalog by key.
	catalog.SortValues()
	fmt.Println(catalog)

	// Retrieve the keys for the catalog.
	var keys = catalog.GetKeys()
	fmt.Println(keys)

	// Retrieve a value from the catalog.
	var value = catalog.GetValue("clubs")
	fmt.Println(value)

	// Remove a value from the catalog.
	catalog.RemoveValue("diamonds")
	fmt.Println(catalog)

	// Change an existing value in the catalog.
	catalog.SetValue("hearts", 5)
	fmt.Println(catalog)
}

NOTE: Because the intrinsic Go map data type is nondeterministic, the key-value pairs in a map were sorted before being added to the initial catalog.

Try it out in the Go Playground...