meta

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2020 License: ISC Imports: 3 Imported by: 3

Documentation

Overview

Package meta provides data types for collectd meta data.

Meta data can be associated with value lists (api.ValueList) and notifications (not yet implemented in the collectd Go API).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data map[string]Entry

Data is a map of meta data values. No setter and getter methods are implemented for this, callers are expected to add and remove entries as they would from a normal map.

Example
package main

import (
	"collectd.org/meta"
)

func main() {
	// Allocate new meta.Data object.
	m := meta.Data{
		// Add interger named "answer":
		"answer": meta.Int64(42),
		// Add bool named "panic":
		"panic": meta.Bool(false),
	}

	// Add string named "required":
	m["required"] = meta.String("towel")

	// Remove the "panic" value:
	delete(m, "panic")
}
Output:

Example (Exists)
package main

import (
	"fmt"

	"collectd.org/meta"
)

func main() {
	m := meta.Data{
		"answer":   meta.Int64(42),
		"panic":    meta.Bool(false),
		"required": meta.String("towel"),
	}

	for _, k := range []string{"answer", "question"} {
		_, ok := m[k]
		fmt.Println(k, "exists:", ok)
	}

}
Output:

answer exists: true
question exists: false
Example (Keys)

This example demonstrates how to get a list of keys from meta.Data.

package main

import (
	"fmt"
	"sort"

	"collectd.org/meta"
)

func main() {
	m := meta.Data{
		"answer":   meta.Int64(42),
		"panic":    meta.Bool(false),
		"required": meta.String("towel"),
	}

	var keys []string
	for k := range m {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	fmt.Println(keys)

}
Output:

[answer panic required]

func (Data) Clone

func (d Data) Clone() Data

Clone returns a copy of d.

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry is an entry in the metadata set. The typed value may be bool, float64, int64, uint64, or string.

Example
package main

import (
	"fmt"
	"log"

	"collectd.org/meta"
)

func main() {
	// Allocate an int64 Entry.
	answer := meta.Int64(42)

	// Read back the "answer" value and ensure it is in fact an int64.
	a, ok := answer.Int64()
	if !ok {
		log.Fatal("Answer is not an int64")
	}
	fmt.Printf("The answer is between %d and %d\n", a-1, a+1)

	// Allocate a string Entry.
	required := meta.String("towel")

	// String is a bit different, because Entry.String() does not return a boolean.
	// Check that "required" is a string and read it into a variable.
	if !required.IsString() {
		log.Fatal("required is not a string")
	}
	fmt.Println("You need a " + required.String())

	// The fmt.Stringer interface is implemented for all value types. To
	// print a string with default formatting, rely on the String() method:
	p := meta.Bool(false)
	fmt.Printf("Should I panic? %v\n", p)

}
Output:

The answer is between 41 and 43
You need a towel
Should I panic? false

func Bool

func Bool(b bool) Entry

Bool returns a new bool Entry.

func Float64

func Float64(f float64) Entry

Float64 returns a new float64 Entry.

func Int64

func Int64(i int64) Entry

Int64 returns a new int64 Entry.

func String

func String(s string) Entry

String returns a new string Entry.

func UInt64

func UInt64(u uint64) Entry

UInt64 returns a new uint64 Entry.

func (Entry) Bool

func (e Entry) Bool() (value, ok bool)

Bool returns the bool value of e.

func (Entry) Float64

func (e Entry) Float64() (float64, bool)

Float64 returns the float64 value of e.

func (Entry) Int64

func (e Entry) Int64() (int64, bool)

Int64 returns the int64 value of e.

func (Entry) Interface

func (e Entry) Interface() interface{}

Interface returns e's value. It is intended to be used with type switches and when printing an entry's type with the "%T" formatting.

Example
package main

import (
	"fmt"
	"log"
	"math/rand"
	"time"

	"collectd.org/meta"
)

func main() {
	rand.Seed(time.Now().UnixNano())
	m := meta.Data{}

	// Create a value with unknown type. "key" is either a string,
	// or an int64.
	switch rand.Intn(2) {
	case 0:
		m["key"] = meta.String("value")
	case 1:
		m["key"] = meta.Int64(42)
	}

	// Scenario 0: A specific type is expected. Report an error that
	// includes the actual type in the error message, if the value is of a
	// different type.
	if _, ok := m["key"].Int64(); !ok {
		err := fmt.Errorf("key is a %T, want an int64", m["key"].Interface())
		fmt.Println(err) // prints "key is a string, want an int64"
	}

	// Scenario 1: Multiple or all types need to be handled, for example to
	// encode the meta data values. The most elegant solution for that is a
	// type switch.
	switch v := m["key"].Interface().(type) {
	case string:
		// string-specific code here
	case int64:
		// The above code skipped printing this, so print it here so
		// this example produces the same output every time, despite
		// the randomness.
		fmt.Println("key is a string, want an int64")
	default:
		// Report the actual type if "key" is an unexpected type.
		err := fmt.Errorf("unexpected type %T", v)
		log.Fatal(err)
	}

}
Output:

key is a string, want an int64

func (Entry) IsString

func (e Entry) IsString() bool

IsString returns true if e is a string value.

func (Entry) MarshalJSON

func (e Entry) MarshalJSON() ([]byte, error)

MarshalJSON implements the "encoding/json".Marshaller interface.

func (Entry) String

func (e Entry) String() string

String returns a string representation of e.

func (Entry) UInt64

func (e Entry) UInt64() (uint64, bool)

UInt64 returns the uint64 value of e.

func (*Entry) UnmarshalJSON

func (e *Entry) UnmarshalJSON(raw []byte) error

UnmarshalJSON implements the "encoding/json".Unmarshaller interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL