kind

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package kind encapsulates the notion of data "kind": that is, it is an abstraction over data types across implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hash added in v0.41.0

func Hash(detectors []*Detector) (h string, err error)

Hash generates a hash from the kinds returned by the detectors. The detectors should already have sampled data.

func MungeEmptyStringAsNil added in v0.32.0

func MungeEmptyStringAsNil(v any) (any, error)

MungeEmptyStringAsNil munges v to nil if v is an empty string.

Types

type Detector

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

Detector is used to detect the kind of a stream of values. The caller adds values via Sample and then invokes Detect.

func NewDetector

func NewDetector() *Detector

NewDetector returns a new instance.

func (*Detector) Detect

func (d *Detector) Detect() (kind Kind, mungeFn MungeFunc, err error)

Detect returns the detected Kind. If ambiguous, Text is returned, unless all sampled values were nil, in which case Null is returned. If the returned mungeFn is non-nil, it can be used to convert input values to their canonical form. For example, for Datetime the MungeFunc would accept string "2020-06-11T02:50:54Z" and return a time.Time, while for Date, the MungeFunc would accept "1970-01-01" or "01 Jan 1970" and always return a string in the canonicalized form "1970-01-01".

func (*Detector) Sample

func (d *Detector) Sample(v any)

Sample adds a sample to the detector.

type Kind

type Kind int

Kind models a generic data kind, which ultimately maps to some more specific implementation data type, such as a SQL VARCHAR or JSON boolean.

const (
	// Unknown indicates an unknown kind.
	Unknown Kind = iota

	// Null indicates a NULL kind.
	Null

	// Text indicates a text kind.
	Text

	// Int indicates an integer kind.
	Int

	// Float indicates a float kind.
	Float

	// Decimal indicates a decimal kind.
	Decimal

	// Bool indicates a boolean kind.
	Bool

	// Bytes indicates a bytes or blob kind.
	Bytes

	// Datetime indicates a date-time kind.
	Datetime

	// Date indicates a date-only kind. For example "2022-12-31".
	Date

	// Time indicates a time-only kind.
	Time
)

func (Kind) MarshalJSON

func (k Kind) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Kind) MarshalText

func (k Kind) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Kind) String

func (k Kind) String() string

String returns a log/debug-friendly representation.

func (*Kind) UnmarshalText

func (k *Kind) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type MungeFunc

type MungeFunc func(any) (any, error)

MungeFunc is a function that accepts a value and returns a munged value with the appropriate Kind. For example, a Datetime MungeFunc would accept string "2020-06-11T02:50:54Z" and return a time.Time.

Jump to

Keyboard shortcuts

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