field

package
v0.113.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddMapToContext

func AddMapToContext(ctx context.Context, m M) context.Context

AddMapToContext returns a context that has ctx as its parent context and has fields populated from the keys and values in m.

Example
package main

import (
	"context"
	"fmt"

	"github.com/kanisterio/kanister/pkg/field"
)

type M = field.M

func main() {
	ctx := field.AddMapToContext(context.Background(), M{"foo": "bar"})
	fmt.Print(field.FromContext(ctx))
}
Output:

["foo":"bar"]
Example (Multiple)
package main

import (
	"context"
	"fmt"

	"github.com/kanisterio/kanister/pkg/field"
)

type M = field.M

func main() {
	ctx := field.AddMapToContext(context.Background(), M{"foo": "bar", "x": "y"})
	// Output is not specified because the order of the fields in 'M' is non-deterministic
	fmt.Print(field.FromContext(ctx))
}
Output:

func Context

func Context(ctx context.Context, key string, v interface{}) context.Context

Context returns a new context that has ctx as its parent context and has a Field with the given key and value.

Example
package main

import (
	"context"
	"fmt"

	"github.com/kanisterio/kanister/pkg/field"
)

func main() {
	ctx := field.Context(context.Background(), "foo", "bar")
	fmt.Print(field.FromContext(ctx))
}
Output:

["foo":"bar"]

Types

type Field

type Field interface {
	Key() string
	Value() interface{}
}

Field is a tuple with a string key and a value of any type

type Fields

type Fields = Fieldser

Fields is an alias for Fieldser to make the interface friendlier. It seems easier to talk about fields than fieldser(s)

func Add

func Add(s Fields, key string, value interface{}) Fields

Add returns a collection with all the fields in s plus a new field with the given key and value. Duplicates are not eliminated.

Example
package main

import (
	"fmt"

	"github.com/kanisterio/kanister/pkg/field"
)

func main() {
	f := field.New("foo", "bar")
	f = field.Add(f, "baz", "x")
	fmt.Print(f)
}
Output:

["foo":"bar","baz":"x"]

func FromContext

func FromContext(ctx context.Context) Fields

FromContext returns the fields present in ctx if any

func New

func New(key string, value interface{}) Fields

New creates a new field collection with given key and value

Example
package main

import (
	"fmt"

	"github.com/kanisterio/kanister/pkg/field"
)

func main() {
	f := field.New("foo", "bar")
	fmt.Print(f)
}
Output:

["foo":"bar"]

type Fieldser

type Fieldser interface {
	Fields() []Field
}

Fieldser is a collection of fields

type M

type M = map[string]interface{}

M contains fields with unique keys. Used to facilitate adding multiple "fields" to a Fields collection

Jump to

Keyboard shortcuts

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