field

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 13 Imported by: 4

README

Field

Go Reference License Release Go Test Go Report Card

provides key-value list for instant json generation, which drives data collecting feature in log and errors.

Usage

When observing or debug with complicated transaction with changeable data, we always need on-site data for metric or analysing purpose.

With this repo, we can describe named data as field and append id to log or error

field

consists of a string name and arbitrary type content, it can be encoded as json bytes or write to a buffer:

type Field struct {
	Key string // name
	Content    // wrapped value
}

func (f Field) EncodeJSON(buffer Buffer) error { ... }

func (f Field) MarshalJSON() ([]byte, error) { ... }

We made different functions for common data types, so you need care noting about wrapping value

func Bool(key string, val bool) Field

func Bools(key string, valArr []bool) Field

func Error(key string, err error) Field

func Float64(key string, val float64) Field

// ... refer to go doc for more impl

Now, we can add field to logger or error (refer to their repo for more info):

// data for logger
logger.With(field.String("traceId", id)).Debug("data created")

// data for error
errors.Note(err, field.String("traceId", id))

fields

is list of field, which provides simple map-like operation and can be encoded to json object

type Fields []Field

func (f Fields) Unique() []Field 
func (f Fields) Has(key string) bool
func (f Fields) Get(key string) (Field, bool) 
func (f Fields) Export() map[string]any 
func (f Fields) EncodeJSON(buf Buffer) (err error) 
func (f Fields) MarshalJSON() (dst []byte, err error) 

Testing

All types of field are supposed to be finely tested in field_test.go. you ca run test with command:

go test ./...

Github Action is also enabled for main branch, every release shall pass all tests.

Contributing

For convenience of PM, please commit all issue to Document Repo.

License

This project is licensed under the Apache License Version 2.0.

Use and contributions signify your agreement to honor the terms of this LICENSE.

Commercial support or licensing is conditionally available through organization email.

Documentation

Index

Constants

View Source
const (
	TypeNull = iota
	TypeBool
	TypeInt
	TypeUint
	TypeUintptr
	TypeFloat
	TypeComplex
	TypeString
	TypeStringer
	TypeBinary
	TypeTime
	TypeError
	TypeJSON
	TypeAny
	TypeArray = 0x80
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnyTypeInterceptor

type AnyTypeInterceptor interface {
	Priority() uint
	Handle(reflectedType reflect.Type, val any) (Content, bool)
}

type ArrayContent

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

func (ArrayContent) Data

func (f ArrayContent) Data() any

func (ArrayContent) EncodeJSON

func (f ArrayContent) EncodeJSON(buffer Buffer) (err error)

func (ArrayContent) Raw

func (f ArrayContent) Raw() []Content

func (ArrayContent) Type

func (f ArrayContent) Type() Type

type BinaryContent

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

func (BinaryContent) Data

func (f BinaryContent) Data() any

func (BinaryContent) EncodeJSON

func (f BinaryContent) EncodeJSON(buffer Buffer) (err error)

func (BinaryContent) Raw

func (f BinaryContent) Raw() json.RawMessage

func (BinaryContent) String

func (f BinaryContent) String() string

func (BinaryContent) Type

func (f BinaryContent) Type() Type

type BoolContent

type BoolContent bool

func (BoolContent) Data

func (f BoolContent) Data() any

func (BoolContent) EncodeJSON

func (f BoolContent) EncodeJSON(buffer Buffer) error

func (BoolContent) Raw

func (f BoolContent) Raw() bool

func (BoolContent) Type

func (f BoolContent) Type() Type

type Buffer

type Buffer interface {
	Write(p []byte) (n int, err error)
	WriteString(s string) (n int, err error)
	WriteByte(c byte) error
	WriteRune(r rune) (n int, err error)
}

type Complex128Content

type Complex128Content complex128

func (Complex128Content) Data

func (f Complex128Content) Data() any

func (Complex128Content) EncodeJSON

func (f Complex128Content) EncodeJSON(buffer Buffer) (err error)

func (Complex128Content) Raw

func (f Complex128Content) Raw() complex128

func (Complex128Content) Type

func (f Complex128Content) Type() Type

type Complex64Content

type Complex64Content complex64

func (Complex64Content) Data

func (f Complex64Content) Data() any

func (Complex64Content) EncodeJSON

func (f Complex64Content) EncodeJSON(buffer Buffer) (err error)

func (Complex64Content) Raw

func (f Complex64Content) Raw() complex64

func (Complex64Content) Type

func (f Complex64Content) Type() Type

type Content

type Content interface {
	Type() Type
	Data() any
	EncodeJSON(buffer Buffer) error
}

func NewBinaryContent

func NewBinaryContent(val []byte) Content

func NewBoolField

func NewBoolField(val bool) Content

func NewByteStringContent

func NewByteStringContent(val []byte) Content

func NewComplex128Content

func NewComplex128Content(val complex128) Content

func NewComplex64Content

func NewComplex64Content(val complex64) Content

func NewErrorContent

func NewErrorContent(val error) Content

func NewFloat32Content

func NewFloat32Content(val float32) Content

func NewFloat64Content

func NewFloat64Content(val float64) Content

func NewIntContent

func NewIntContent[T int | int8 | int16 | int32 | int64](val T) Content

func NewJSONContent

func NewJSONContent(val []byte) Content

func NewNilContent

func NewNilContent() Content

func NewStringContent

func NewStringContent(val string) Content

func NewStringerContent

func NewStringerContent[T fmt.Stringer](val T) Content

func NewTimeContent

func NewTimeContent(val time.Time) Content

func NewUintContent

func NewUintContent[T uint | uint8 | uint16 | uint32 | uint64](val T) Content

func NewUintptrContent

func NewUintptrContent(val uintptr) Content

type ErrorContent

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

func (ErrorContent) Data

func (f ErrorContent) Data() any

func (ErrorContent) EncodeJSON

func (f ErrorContent) EncodeJSON(buffer Buffer) error

func (ErrorContent) Raw

func (f ErrorContent) Raw() error

func (ErrorContent) Type

func (f ErrorContent) Type() Type

type Field

type Field struct {
	Key string
	Content
}

func Any

func Any(key string, val any) Field

func Binary

func Binary(key string, val []byte) Field

func Binarys

func Binarys(key string, valArr [][]byte) Field

func Bool

func Bool(key string, val bool) Field

func Bools

func Bools(key string, valArr []bool) Field

func ByteString

func ByteString(key string, val []byte) Field

func ByteStrings

func ByteStrings(key string, valArr [][]byte) Field

func Complex128

func Complex128(key string, val complex128) Field

func Complex128s

func Complex128s(key string, nums []complex128) Field

func Complex64

func Complex64(key string, val complex64) Field

func Complex64s

func Complex64s(key string, nums []complex64) Field

func Duration

func Duration(key string, val time.Duration) Field

func Durations

func Durations(key string, valArr []time.Duration) Field

func Error

func Error(key string, err error) Field

func Errors

func Errors(key string, errs []error) Field

func Float32

func Float32(key string, val float32) Field

func Float32s

func Float32s(key string, nums []float32) Field

func Float64

func Float64(key string, val float64) Field

func Float64s

func Float64s(key string, nums []float64) Field

func Int

func Int[T int | int8 | int16 | int32 | int64](key string, val T) Field

func Int16

func Int16(key string, val int16) Field

func Int16s

func Int16s(key string, nums []int16) Field

func Int32

func Int32(key string, val int32) Field

func Int32s

func Int32s(key string, nums []int32) Field

func Int64

func Int64(key string, val int64) Field

func Int64s

func Int64s(key string, nums []int64) Field

func Int8

func Int8(key string, val int8) Field

func Int8s

func Int8s(key string, nums []int8) Field

func Ints

func Ints[T int | int8 | int16 | int32 | int64](key string, nums []T) Field

func JsonRawMessage

func JsonRawMessage(key string, val json.RawMessage) Field

func Nil

func Nil(key string) Field

func String

func String(key string, val string) Field

func Stringer

func Stringer(key string, val fmt.Stringer) Field

func Stringers

func Stringers[T fmt.Stringer](key string, valArr []T) Field

func Strings

func Strings(key string, valArr []string) Field

func Time

func Time(key string, val time.Time) Field

func Times

func Times(key string, valArr []time.Time) Field

func Uint

func Uint[T uint | uint8 | uint16 | uint32 | uint64](key string, val T) Field

func Uint16

func Uint16(key string, val uint16) Field

func Uint16s

func Uint16s(key string, nums []uint16) Field

func Uint32

func Uint32(key string, val uint32) Field

func Uint32s

func Uint32s(key string, nums []uint32) Field

func Uint64

func Uint64(key string, val uint64) Field

func Uint64s

func Uint64s(key string, nums []uint64) Field

func Uint8

func Uint8(key string, val uint8) Field

func Uint8s

func Uint8s(key string, nums []uint8) Field

func Uintptr

func Uintptr(key string, val uintptr) Field

func Uintptrs

func Uintptrs(key string, us []uintptr) Field

func Uints

func Uints[T uint | uint8 | uint16 | uint32 | uint64](key string, nums []T) Field

func (Field) EncodeJSON

func (f Field) EncodeJSON(buffer Buffer) (err error)

func (Field) MarshalJSON

func (f Field) MarshalJSON() (_ []byte, err error)

type Fields

type Fields []Field

func (Fields) EncodeJSON

func (f Fields) EncodeJSON(buf Buffer) (err error)

func (Fields) Export

func (f Fields) Export() map[string]any

func (Fields) Get

func (f Fields) Get(key string) (Field, bool)

func (Fields) Has

func (f Fields) Has(key string) bool

func (Fields) MarshalJSON

func (f Fields) MarshalJSON() (dst []byte, err error)

func (Fields) Unique

func (f Fields) Unique() []Field

type Float32Content

type Float32Content float32

func (Float32Content) Data

func (f Float32Content) Data() any

func (Float32Content) EncodeJSON

func (f Float32Content) EncodeJSON(buffer Buffer) (err error)

func (Float32Content) Raw

func (f Float32Content) Raw() float32

func (Float32Content) Type

func (f Float32Content) Type() Type

type Float64Content

type Float64Content float64

func (Float64Content) Data

func (f Float64Content) Data() any

func (Float64Content) EncodeJSON

func (f Float64Content) EncodeJSON(buffer Buffer) (err error)

func (Float64Content) Raw

func (f Float64Content) Raw() float64

func (Float64Content) Type

func (f Float64Content) Type() Type

type IntContent

type IntContent[T int | int8 | int16 | int32 | int64] struct {
	// contains filtered or unexported fields
}

func (IntContent[T]) Data

func (f IntContent[T]) Data() any

func (IntContent[T]) EncodeJSON

func (f IntContent[T]) EncodeJSON(buffer Buffer) (err error)

func (IntContent[T]) Raw

func (f IntContent[T]) Raw() T

func (IntContent[T]) Type

func (f IntContent[T]) Type() Type

type JSONContent

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

func (JSONContent) Data

func (f JSONContent) Data() any

func (JSONContent) EncodeJSON

func (f JSONContent) EncodeJSON(buffer Buffer) (err error)

func (JSONContent) Raw

func (f JSONContent) Raw() json.RawMessage

func (JSONContent) Type

func (f JSONContent) Type() Type

type NilContent

type NilContent struct{}

func (NilContent) Data

func (n NilContent) Data() any

func (NilContent) EncodeJSON

func (n NilContent) EncodeJSON(buffer Buffer) error

func (NilContent) Type

func (n NilContent) Type() Type

type StringContent

type StringContent string

func (StringContent) Data

func (f StringContent) Data() any

func (StringContent) EncodeJSON

func (f StringContent) EncodeJSON(buffer Buffer) error

func (StringContent) Raw

func (f StringContent) Raw() string

func (StringContent) Type

func (f StringContent) Type() Type

type StringerContent

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

func (StringerContent) Data

func (f StringerContent) Data() any

func (StringerContent) EncodeJSON

func (f StringerContent) EncodeJSON(buffer Buffer) (err error)

func (StringerContent) Raw

func (f StringerContent) Raw() fmt.Stringer

func (StringerContent) Type

func (f StringerContent) Type() Type

type TimeContent

type TimeContent time.Time

func (TimeContent) Data

func (f TimeContent) Data() any

func (TimeContent) EncodeJSON

func (f TimeContent) EncodeJSON(buffer Buffer) error

func (TimeContent) Raw

func (f TimeContent) Raw() time.Time

func (TimeContent) Type

func (f TimeContent) Type() Type

type Type

type Type uint8

type UintContent

type UintContent[T uint | uint8 | uint16 | uint32 | uint64] struct {
	// contains filtered or unexported fields
}

func (UintContent[T]) Data

func (f UintContent[T]) Data() any

func (UintContent[T]) EncodeJSON

func (f UintContent[T]) EncodeJSON(buffer Buffer) (err error)

func (UintContent[T]) Raw

func (f UintContent[T]) Raw() T

func (UintContent[T]) Type

func (f UintContent[T]) Type() Type

type UintptrContent

type UintptrContent uintptr

func (UintptrContent) Data

func (f UintptrContent) Data() any

func (UintptrContent) EncodeJSON

func (f UintptrContent) EncodeJSON(buffer Buffer) (err error)

func (UintptrContent) Raw

func (f UintptrContent) Raw() uintptr

func (UintptrContent) Type

func (f UintptrContent) Type() Type

Jump to

Keyboard shortcuts

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