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 ¶
- type Data
- type Entry
- func (e Entry) Bool() (value, ok bool)
- func (e Entry) Float64() (float64, bool)
- func (e Entry) Int64() (int64, bool)
- func (e Entry) Interface() interface{}
- func (e Entry) IsString() bool
- func (e Entry) MarshalJSON() ([]byte, error)
- func (e Entry) String() string
- func (e Entry) UInt64() (uint64, bool)
- func (e *Entry) UnmarshalJSON(raw []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Data ¶
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")
}
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]
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 (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) MarshalJSON ¶
MarshalJSON implements the "encoding/json".Marshaller interface.
func (*Entry) UnmarshalJSON ¶
UnmarshalJSON implements the "encoding/json".Unmarshaller interface.