datatype

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2018 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package datatype contains necessary logic to sanitise a JSON object coming from a reader. This package is subjected to change.

Index

Examples

Constants

View Source
const (
	// Byte amount is the same as is read.
	Byte = 1.0
	// KiloByte divides the amount to kilobytes to show smaller value.
	KiloByte = 1024 * Byte
	// MegaByte divides the amount to megabytes to show smaller value.
	MegaByte = 1024 * KiloByte
)

Variables

View Source
var ErrUnidentifiedJason = errors.New("unidentified jason value")

ErrUnidentifiedJason is an error when the value is not identified. It happens when the value is not a string or a float64 types, or the container ends up empty.

View Source
var TimeStampFormat = "2006-01-02T15:04:05.999999-07:00"

TimeStampFormat specifies the format that all timestamps are formatted with.

Functions

func FloatInSlice added in v0.9.0

func FloatInSlice(niddle float64, haystack []float64) bool

FloatInSlice returns true if niddle is found in the haystack.

func IsByte added in v0.10.0

func IsByte(m string) bool

IsByte checks the string string to determine if it is a Byte value.

func IsKiloByte added in v0.10.0

func IsKiloByte(m string) bool

IsKiloByte checks the string string to determine if it is a KiloByte value.

func IsMegaByte added in v0.10.0

func IsMegaByte(m string) bool

IsMegaByte checks the string string to determine if it is a MegaByte value.

func Uint64InSlice added in v0.9.0

func Uint64InSlice(niddle uint64, haystack []uint64) bool

Uint64InSlice returns true if niddle is found in the haystack.

Types

type ByteType

type ByteType struct {
	Key   string
	Value float64
	// contains filtered or unexported fields
}

ByteType represents a pair of key values in which the value represents bytes. It converts the value to MB.

func NewByteType added in v0.9.2

func NewByteType(key string, value float64) *ByteType

NewByteType returns a new ByteType object.

func (ByteType) Equal added in v0.3.0

func (bt ByteType) Equal(other DataType) bool

Equal compares both keys and values and returns true if they are equal.

func (*ByteType) Read added in v0.9.2

func (r *ByteType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*ByteType) Reset added in v0.9.2

func (r *ByteType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type Container

type Container struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Container holds a list of DataTypes. It satisfies the DataContainer.

func New added in v0.7.0

func New(list []DataType) *Container

New returns a new container and populates it with the given list.

func (*Container) Add

func (c *Container) Add(d ...DataType)

Add adds d to the list. You can pass it as many items you need to.

Example
package main

import (
	"fmt"

	"github.com/arsham/expipe/datatype"
)

func main() {
	c := datatype.New(nil)
	firstElm := &datatype.FloatType{}

	c.Add(firstElm)
	fmt.Println(c.Len())

	c.Add(&datatype.FloatType{}, &datatype.FloatType{})
	fmt.Println(c.Len())
}
Output:

1
3

func (*Container) Generate added in v0.9.0

func (c *Container) Generate(p io.Writer, timestamp time.Time) (int, error)

Generate prepends a timestamp pair and value to the list, and generates a json object suitable for recording into a document store.

Example (Add)
package main

import (
	"bytes"
	"fmt"
	"time"

	"github.com/arsham/expipe/datatype"
)

func main() {
	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

	c := datatype.Container{}
	elm := datatype.NewFloatType("key", 66.6)
	c.Add(elm)

	w := new(bytes.Buffer)
	_, err := c.Generate(w, t)
	fmt.Println("With new element:", w.String())
	fmt.Println("Error:", err)

}
Output:

With new element: {"@timestamp":"2009-11-10T23:00:00+00:00","key":66.600000}
Error: <nil>
Example (Errors)
t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

c := datatype.Container{}
c.Add(new(badDataType))
w := new(bytes.Buffer)
_, err := c.Generate(w, t)
fmt.Println(errors.Cause(err))
Output:

DataType Error
Example (New)
package main

import (
	"bytes"
	"fmt"
	"time"

	"github.com/arsham/expipe/datatype"
)

func main() {
	t := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

	c := datatype.Container{}
	w := new(bytes.Buffer)
	n, err := c.Generate(w, t)
	fmt.Println(err, n)
	fmt.Println("Contents:", w.String())
}
Output:

<nil> 42
Contents: {"@timestamp":"2009-11-10T23:00:00+00:00"}

func (*Container) Len

func (c *Container) Len() int

Len returns the length of the data.

Example
package main

import (
	"fmt"

	"github.com/arsham/expipe/datatype"
)

func main() {
	l := []datatype.DataType{&datatype.FloatType{}, &datatype.FloatType{}}
	c := datatype.New(l)
	fmt.Println(c.Len())
}
Output:

2

func (*Container) List

func (c *Container) List() []DataType

List returns the data.

type DataContainer

type DataContainer interface {
	List() []DataType
	Len() int
	Generate(p io.Writer, timestamp time.Time) (n int, err error)
}

DataContainer is an interface for holding a list of DataType. I'm aware of the container/list package, which is awesome, but I needed a simple interface to do this job. You should not update the list returning from List as it is a shared list and anyone can read from it. If you append to this list, there is a chance you are not referring to the same underlying array in memory. Generate writes to w applying the timestamp. It returns the amount of bytes it's written and an error if any of its contents report.

func JobResultDataTypes

func JobResultDataTypes(b []byte, mapper Mapper) (DataContainer, error)

JobResultDataTypes generates a list of DataType and puts them inside the DataContainer. It returns errors if unmarshaling is unsuccessful or ErrUnidentifiedJason when the container ends up empty.

type DataType

type DataType interface {
	Read(p []byte) (n int, err error)
	Equal(other DataType) bool
	Reset()
}

DataType represents a single paired data. The key of the json value is mapped to Key, and the value is to Value. Read includes both Key and Value. The Equal method comparison of the value is not ordered. Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes.

type FloatListType

type FloatListType struct {
	Key   string
	Value []float64
	// contains filtered or unexported fields
}

FloatListType represents a pair of key values. The value is a list of floats.

func NewFloatListType added in v0.9.2

func NewFloatListType(key string, value []float64) *FloatListType

NewFloatListType returns a new FloatListType object.

func (FloatListType) Equal added in v0.3.0

func (f FloatListType) Equal(other DataType) bool

Equal compares both keys and all values and returns true if they are equal. The values are checked in an unordered fashion.

func (*FloatListType) Read added in v0.9.2

func (r *FloatListType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*FloatListType) Reset added in v0.9.2

func (r *FloatListType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type FloatType

type FloatType struct {
	Key   string
	Value float64
	// contains filtered or unexported fields
}

FloatType represents a pair of key values that the value is a float64.

func NewFloatType added in v0.9.2

func NewFloatType(key string, value float64) *FloatType

NewFloatType returns a new FloadType object.

func (FloatType) Equal added in v0.3.0

func (f FloatType) Equal(other DataType) bool

Equal compares both keys and values and returns true if they are equal.

func (*FloatType) Read added in v0.9.2

func (r *FloatType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*FloatType) Reset added in v0.9.2

func (r *FloatType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type GCListType

type GCListType struct {
	Key   string
	Value []uint64
	// contains filtered or unexported fields
}

GCListType represents a pair of key values of GC list info.

func NewGCListType added in v0.9.2

func NewGCListType(key string, value []uint64) *GCListType

NewGCListType returns a new FloatListType object.

func (GCListType) Equal added in v0.3.0

func (g GCListType) Equal(other DataType) bool

Equal is not implemented. You should iterate and check yourself. Equal compares both keys and values and returns true if they are equal.

func (*GCListType) Read added in v0.9.2

func (r *GCListType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*GCListType) Reset added in v0.9.2

func (r *GCListType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type KiloByteType added in v0.3.0

type KiloByteType struct {
	Key   string
	Value float64
	// contains filtered or unexported fields
}

KiloByteType represents a pair of key values in which the value represents bytes. It converts the value to KB.

func NewKiloByteType added in v0.9.2

func NewKiloByteType(key string, value float64) *KiloByteType

NewKiloByteType returns a new KiloByteType object.

func (KiloByteType) Equal added in v0.3.0

func (k KiloByteType) Equal(other DataType) bool

Equal compares both keys and values and returns true if they are equal.

func (*KiloByteType) Read added in v0.9.2

func (r *KiloByteType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*KiloByteType) Reset added in v0.9.2

func (r *KiloByteType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type MapConvert added in v0.3.0

type MapConvert struct {
	GCTypes     []string
	MemoryTypes map[string]string
}

MapConvert can produce output from GC string list and memory type input.

func DefaultMapper added in v0.3.0

func DefaultMapper() *MapConvert

DefaultMapper returns a MapConvert object that is populated by the default mappings. The data is hard coded in the program, but you can provide your mapping file in your configuration file.

func MapsFromViper added in v0.3.0

func MapsFromViper(v treeReader) *MapConvert

MapsFromViper reads from the map file and produces functions for conversion used in type decoder. It first reads from the default settings defined in the maps.yml in the same folder, then overrides with the user specified mappings.

func (*MapConvert) Copy added in v0.7.0

func (m *MapConvert) Copy() Mapper

Copy returns a new copy of the Mapper.

func (*MapConvert) Values added in v0.3.0

func (m *MapConvert) Values(prefix string, values map[string]*jason.Value) []DataType

Values returns a slice of DataTypes based on the given name/value inputs. It flattens the float list values, therefore you will get multiple values per input. If the name is found in memory_bytes map, it will return one of those, otherwise it will return a FloatType or StringType if can convert. It will return nil if the value is not one of above.

type MapConvertMock added in v0.3.0

type MapConvertMock struct {
	GCTypes     []string
	MemoryTypes map[string]string
	ValuesFunc  func(prefix string, values map[string]*jason.Value) []DataType

	DefaultCovertor Mapper
	// contains filtered or unexported fields
}

MapConvertMock is the mocked version of MapConvert.

func (*MapConvertMock) Copy added in v0.7.0

func (m *MapConvertMock) Copy() Mapper

Copy returns a new copy of the Mapper.

func (*MapConvertMock) Values added in v0.3.0

func (m *MapConvertMock) Values(prefix string, values map[string]*jason.Value) []DataType

Values calls the ValuesFunc if exists, otherwise returns nil.

type Mapper added in v0.3.0

type Mapper interface {
	Values(prefix string, values map[string]*jason.Value) []DataType
	Copy() Mapper
}

Mapper generates DataTypes based on the given name/value inputs. Values closes the channel once all input has been exhausted. You should always copy the mapper if you are using it concurrently.

type MegaByteType added in v0.3.0

type MegaByteType struct {
	Key   string
	Value float64
	// contains filtered or unexported fields
}

MegaByteType represents a pair of key values in which the value represents bytes. It converts the value to MB.

func NewMegaByteType added in v0.9.2

func NewMegaByteType(key string, value float64) *MegaByteType

NewMegaByteType returns a new MegaByteType object.

func (MegaByteType) Equal added in v0.3.0

func (m MegaByteType) Equal(other DataType) bool

Equal compares both keys and values and returns true if they are equal.

func (*MegaByteType) Read added in v0.9.2

func (r *MegaByteType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*MegaByteType) Reset added in v0.9.2

func (r *MegaByteType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

type StringType

type StringType struct {
	Key   string
	Value string
	// contains filtered or unexported fields
}

StringType represents a pair of key values that the value is a string.

func NewStringType added in v0.9.2

func NewStringType(key, value string) *StringType

NewStringType returns a new StringType object.

func (StringType) Equal added in v0.3.0

func (s StringType) Equal(other DataType) bool

Equal compares both keys and values and returns true if they are equal.

func (*StringType) Read added in v0.9.2

func (r *StringType) Read(b []byte) (int, error)

Read includes both Key and Value.

func (*StringType) Reset added in v0.9.2

func (r *StringType) Reset()

Reset resets the content to be empty, but it retains the underlying storage for use by future writes.

Jump to

Keyboard shortcuts

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