fields

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package fields provides types and functions to work with key-value pairs.

The package offers three primary abstractions:

  • Field: A key-value pair where the key is a string and the value can be any type.
  • Dict: A map-based collection of unique fields, providing efficient key-based lookup.
  • List: An ordered collection of fields that preserves insertion order.

Fields can be created using the F constructor, and both Dict and List provide conversion methods between the two collection types. All types implement String() for consistent string representation.

Example usage:

// Create fields
f1 := fields.F("status", "success")
f2 := fields.F("code", 200)

// Working with a List (ordered collection)
var list fields.List
list.Add(f1, f2)
fmt.Println(list) // "(status=success, code=200)"

// Working with a Dict (unique key collection)
dict := fields.Dict{}
dict.Add(f1, f2, fields.F("status", "updated")) // overwrites "status"
fmt.Println(dict) // "(status=updated, code=200)" (order may vary)

// Converting between types
list2 := dict.ToList() // order unspecified
dict2 := list.ToDict() // last occurrence of each key wins

Package fields provides types and functions for working with key-value fields.

Index

Constants

View Source
const CollectionSep = ", "

Variables

This section is empty.

Functions

func WriteTo added in v0.4.0

func WriteTo(b *strings.Builder, seq iter.Seq2[string, any])

WriteTo writes key-value pairs from an iter.Seq2[string, any] to the builder in the format "(key1=val1, key2=val2)". If no fields are present, nothing is written.

Types

type Dict

type Dict map[string]any

Dict is a map-based collection of unique fields, keyed by string. It provides efficient lookup and overwrites duplicate keys.

func (Dict) Add

func (d Dict) Add(fields ...Field)

Add inserts or updates fields in the Dict, overwriting existing keys if present.

Example:

d := fields.Dict{"foo": "bar"}
d.Add(fields.F("baz", 42), fields.F("foo", "qux")) // d["foo"] == "qux"

func (Dict) All added in v0.4.0

func (d Dict) All() iter.Seq2[string, any]

All returns an iterator over all key-value pairs in the Dict as iter.Seq2[string, any].

Example:

for k, v := range d.All() {
    fmt.Println(k, v)
}

func (Dict) String

func (d Dict) String() string

String returns the Dict as a string in the format "(key1=val1, key2=val2)". Returns an empty string if the Dict is empty. The order of fields is unspecified.

func (Dict) ToList

func (d Dict) ToList() List

ToList converts the Dict to a List, with order unspecified. Each key-value pair becomes a Field in the resulting List.

func (Dict) WriteTo

func (d Dict) WriteTo(b *strings.Builder)

WriteTo writes the Dict as a string in the format "(key1=val1, key2=val2)" to the provided builder. If the Dict is empty, nothing is written. The order of fields is unspecified.

type Field

type Field struct {
	// Key of the field
	K string
	// Value of the field
	V any
}

Field represents a key-value pair, where the key is a string and the value can be any type.

func F

func F(key string, value any) Field

F creates a new Field with the given key and value.

Example:

f := fields.F("user", "alice")

func (Field) String

func (f Field) String() string

String returns the Field as a string in the format "key=value".

func (Field) WriteTo

func (f Field) WriteTo(b *strings.Builder)

WriteTo writes the Field as a string in the format "key=value" to the provided builder.

type List

type List []Field //nolint:recvcheck //we need Add to be a pointer receiver to modify original value.

List is an ordered collection of Field values, preserving insertion order. Such collection do not check for duplicate keys.

func (*List) Add

func (l *List) Add(fields ...Field)

Add one or more fields to the List, modifying it.

Example:

var l fields.List
l.Add(fields.F("foo", "bar"), fields.F("baz", 42))

func (List) All added in v0.4.0

func (l List) All() iter.Seq2[string, any]

All returns an iterator over all key-value pairs in the List as iter.Seq2[string, any].

Example:

for k, v := range l.All() {
    fmt.Println(k, v)
}

func (List) String

func (l List) String() string

String returns the List as a string in the format "(key1=val1, key2=val2)". Returns an empty string if the List is empty.

func (List) ToDict

func (l List) ToDict() Dict

ToDict converts the List to a Dict, overwriting duplicate keys with the last occurrence.

Example:

l := fields.List{fields.F("foo", 1), fields.F("foo", 2)}
d := l.ToDict() // d["foo"] == 2

func (List) WriteTo

func (l List) WriteTo(b *strings.Builder)

WriteTo writes the List as a string in the format "(key1=val1, key2=val2)" to the provided builder. If the List is empty, nothing is written.

Jump to

Keyboard shortcuts

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